Skip to content

Commit

Permalink
Add API for Insecure Bluetooth connections.
Browse files Browse the repository at this point in the history
Android only. See  http://goo.gl/1mFjZY

Fixes #8
  • Loading branch information
don committed Sep 18, 2013
1 parent 5592d1d commit 45de643
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/android/com/megster/cordova/BluetoothSerial.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BluetoothSerial extends CordovaPlugin {
// actions
private static final String LIST = "list";
private static final String CONNECT = "connect";
private static final String CONNECT_INSECURE = "connectInsecure";
private static final String DISCONNECT = "disconnect";
private static final String WRITE = "write";
private static final String AVAILABLE = "available";
Expand Down Expand Up @@ -84,7 +85,14 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback

} else if (action.equals(CONNECT)) {

connect(args, callbackContext);
boolean secure = true;
connect(args, secure, callbackContext);

} else if (action.equals(CONNECT_INSECURE)) {

// see Android docs about Insecure RFCOMM http://goo.gl/1mFjZY
boolean secure = false;
connect(args, false, callbackContext);

} else if (action.equals(DISCONNECT)) {

Expand Down Expand Up @@ -181,13 +189,13 @@ private void listBondedDevices(CallbackContext callbackContext) throws JSONExcep
callbackContext.success(deviceList);
}

private void connect(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
private void connect(CordovaArgs args, boolean secure, CallbackContext callbackContext) throws JSONException {
String macAddress = args.getString(0);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);

if (device != null) {
connectCallback = callbackContext;
bluetoothSerialService.connect(device, true);
bluetoothSerialService.connect(device, secure);

PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
Expand Down
3 changes: 2 additions & 1 deletion src/android/com/megster/cordova/BluetoothSerialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ public ConnectThread(BluetoothDevice device, boolean secure) {
// tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
//tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
tmp = device.createInsecureRfcommSocketToServiceRecord(UUID_SPP);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
Expand Down
5 changes: 5 additions & 0 deletions www/bluetoothSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ module.exports = {
cordova.exec(success, failure, "BluetoothSerial", "connect", [macAddress]);
},

// Android only - see http://goo.gl/1mFjZY
connectInsecure: function (macAddress, success, failure) {
cordova.exec(success, failure, "BluetoothSerial", "connectInsecure", [macAddress]);
},

disconnect: function (success, failure) {
cordova.exec(success, failure, "BluetoothSerial", "disconnect", []);
},
Expand Down

0 comments on commit 45de643

Please sign in to comment.