diff --git a/src/android/com/megster/cordova/BluetoothSerial.java b/src/android/com/megster/cordova/BluetoothSerial.java index 8be5575f..16fee5cf 100644 --- a/src/android/com/megster/cordova/BluetoothSerial.java +++ b/src/android/com/megster/cordova/BluetoothSerial.java @@ -241,7 +241,7 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, discoverableDuration); cordova.getActivity().startActivity(discoverIntent); - } else { + } else { validAction = false; } @@ -337,12 +337,18 @@ private JSONObject deviceToJSON(BluetoothDevice device) throws JSONException { } private void connect(CordovaArgs args, boolean secure, CallbackContext callbackContext) throws JSONException { - String macAddress = args.getString(0); + String connectTo = args.getString(0); + String[] parts = connectTo.split("\\|"); + String macAddress = parts[0]; + String uuid = null; + if(parts.length > 1) { + uuid = parts[1]; + } BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress); if (device != null) { connectCallback = callbackContext; - bluetoothSerialService.connect(device, secure); + bluetoothSerialService.connect(device, secure, uuid); buffer.setLength(0); PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); diff --git a/src/android/com/megster/cordova/BluetoothSerialService.java b/src/android/com/megster/cordova/BluetoothSerialService.java index 63ce6c53..d0524e05 100644 --- a/src/android/com/megster/cordova/BluetoothSerialService.java +++ b/src/android/com/megster/cordova/BluetoothSerialService.java @@ -116,8 +116,9 @@ public synchronized void start() { * Start the ConnectThread to initiate a connection to a remote device. * @param device The BluetoothDevice to connect * @param secure Socket Security type - Secure (true) , Insecure (false) + * @param uuid A string representing the UUID to connect to. If null, the default is used. */ - public synchronized void connect(BluetoothDevice device, boolean secure) { + public synchronized void connect(BluetoothDevice device, boolean secure, String uuid) { if (D) Log.d(TAG, "connect to: " + device); // Cancel any thread attempting to make a connection @@ -129,7 +130,7 @@ public synchronized void connect(BluetoothDevice device, boolean secure) { if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} // Start the thread to connect with the given device - mConnectThread = new ConnectThread(device, secure); + mConnectThread = new ConnectThread(device, secure, uuid); mConnectThread.start(); setState(STATE_CONNECTING); } @@ -339,19 +340,27 @@ private class ConnectThread extends Thread { private final BluetoothDevice mmDevice; private String mSocketType; - public ConnectThread(BluetoothDevice device, boolean secure) { + public ConnectThread(BluetoothDevice device, boolean secure, String uuid) { mmDevice = device; BluetoothSocket tmp = null; mSocketType = secure ? "Secure" : "Insecure"; + + UUID remoteUUID = null; + try { + remoteUUID = UUID.fromString(uuid); + Log.v(TAG, "Connect using user-defined UUID " + uuid); + } catch(Exception e) { + Log.e(TAG, "Invalid UUID provided for connection: " + uuid + ". Fallback to default.", e); + } // Get a BluetoothSocket for a connection with the given BluetoothDevice try { if (secure) { // tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE); - tmp = device.createRfcommSocketToServiceRecord(UUID_SPP); + tmp = device.createRfcommSocketToServiceRecord((remoteUUID != null) ? remoteUUID : UUID_SPP); } else { //tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE); - tmp = device.createInsecureRfcommSocketToServiceRecord(UUID_SPP); + tmp = device.createInsecureRfcommSocketToServiceRecord((remoteUUID != null) ? remoteUUID : 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 250b8aab..1d261841 100644 --- a/www/bluetoothSerial.js +++ b/www/bluetoothSerial.js @@ -129,8 +129,7 @@ module.exports = { setDiscoverable: function (discoverableDuration) { cordova.exec(null, null, "BluetoothSerial", "setDiscoverable", [discoverableDuration]); - } - + }, };