From 45de643bb0a15ecae0e4544670d3b1e6a8a19832 Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Wed, 18 Sep 2013 00:30:30 -0400 Subject: [PATCH] Add API for Insecure Bluetooth connections. Android only. See http://goo.gl/1mFjZY Fixes #8 --- .../com/megster/cordova/BluetoothSerial.java | 14 +++++++++++--- .../megster/cordova/BluetoothSerialService.java | 3 ++- www/bluetoothSerial.js | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/android/com/megster/cordova/BluetoothSerial.java b/src/android/com/megster/cordova/BluetoothSerial.java index ead2f18e..b1a8a515 100644 --- a/src/android/com/megster/cordova/BluetoothSerial.java +++ b/src/android/com/megster/cordova/BluetoothSerial.java @@ -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"; @@ -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)) { @@ -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); diff --git a/src/android/com/megster/cordova/BluetoothSerialService.java b/src/android/com/megster/cordova/BluetoothSerialService.java index 62383824..9a549e70 100644 --- a/src/android/com/megster/cordova/BluetoothSerialService.java +++ b/src/android/com/megster/cordova/BluetoothSerialService.java @@ -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); diff --git a/www/bluetoothSerial.js b/www/bluetoothSerial.js index e7ef9da2..72485065 100644 --- a/www/bluetoothSerial.js +++ b/www/bluetoothSerial.js @@ -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", []); },