From f03b2c1655dafb0b149fc9bba09814f0df24117a Mon Sep 17 00:00:00 2001 From: Andrii Kuts Date: Mon, 6 Aug 2018 11:23:21 +0300 Subject: [PATCH 1/5] Added Pairing Cancelation handling --- .../RCTBluetoothSerialModule.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index aac796a..134c964 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -44,6 +44,7 @@ public class RCTBluetoothSerialModule extends ReactContextBaseJavaModule impleme private static final String ERROR = "error"; private static final String FILE_PERCENT_LOADED = "filePercentLoaded"; private static final String FILE_LOADED = "fileLoaded"; + private static final String PAIRING_FAILED = "pairingFailed"; // Other stuff private static final int REQUEST_ENABLE_BLUETOOTH = 1; @@ -570,11 +571,15 @@ void onError (Exception e) { sendEvent(ERROR, params); } + void onPairingFailed() { + sendEvent(PAIRING_FAILED, null); + } + void onFileChunkLoaded(double percentLoaded) { WritableMap params = Arguments.createMap(); params.putDouble("percentLoaded", percentLoaded); - sendEvent(FILE_PERCENT_LOADED, params); + sendEvent(d, params); } void onFileLoaded() { @@ -720,6 +725,19 @@ public void onReceive(Context context, Intent intent) { Log.e(TAG, "Cannot unregister receiver", e); onError(e); } + } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDING){ + if (D) Log.d(TAG, "Pairing canceled"); + if (mPairDevicePromise != null) { + mPairDevicePromise.resolve(true); + mPairDevicePromise = null; + } + try { + onPairingFailed(); + mReactContext.unregisterReceiver(this); + } catch (Exception e) { + Log.e(TAG, "Pairing canceled", e); + onError(e); + } } } From 4f8ee06e3a3fb2a9a1aa9a2e81d5b0a95f83579f Mon Sep 17 00:00:00 2001 From: Andrii Kuts Date: Mon, 6 Aug 2018 17:22:17 +0300 Subject: [PATCH 2/5] Fixed issue --- .../com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index 134c964..da9829f 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -579,7 +579,7 @@ void onPairingFailed() { void onFileChunkLoaded(double percentLoaded) { WritableMap params = Arguments.createMap(); params.putDouble("percentLoaded", percentLoaded); - sendEvent(d, params); + sendEvent(FILE_PERCENT_LOADED, params); } void onFileLoaded() { From 780819ed33749da06d458e5d3a5a2894883f0301 Mon Sep 17 00:00:00 2001 From: Andrii Kuts Date: Thu, 9 Aug 2018 12:55:58 +0300 Subject: [PATCH 3/5] Added BONDING_NONE handling --- .../RCTBluetoothSerial/RCTBluetoothSerialModule.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index da9829f..473f0ee 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -572,6 +572,7 @@ void onError (Exception e) { } void onPairingFailed() { + Log.d("BT", "PAIRING FAILED"); sendEvent(PAIRING_FAILED, null); } @@ -659,6 +660,7 @@ private void pairDevice(BluetoothDevice device) { mPairDevicePromise.reject(e); mPairDevicePromise = null; } + onPairingFailed(); onError(e); } } @@ -673,6 +675,7 @@ private void unpairDevice(BluetoothDevice device) { Method m = device.getClass().getMethod("removeBond", (Class[]) null); m.invoke(device, (Object[]) null); registerDevicePairingReceiver(device.getAddress(), BluetoothDevice.BOND_NONE); + onPairingFailed(); } catch (Exception e) { Log.e(TAG, "Cannot unpair device", e); if (mPairDevicePromise != null) { @@ -739,7 +742,6 @@ public void onReceive(Context context, Intent intent) { onError(e); } } - } } }; @@ -810,6 +812,11 @@ public void onReceive(Context context, Intent intent) { if (D) Log.d(TAG, "Bluetooth was enabled"); sendEvent(BT_ENABLED, null); break; + + case BluetoothAdapter.BOND_NONE: + if (D) Log.d(TAG, "Unsuccessful device pairing"); + sendEvent(PAIRING_FAILED, null); + break; } } } @@ -817,4 +824,4 @@ public void onReceive(Context context, Intent intent) { mReactContext.registerReceiver(bluetoothStateReceiver, intentFilter); } -} \ No newline at end of file +} From 3d01cd43892c17d1c922a307c05c251b0c32850f Mon Sep 17 00:00:00 2001 From: Andrii Kuts Date: Thu, 9 Aug 2018 16:40:43 +0300 Subject: [PATCH 4/5] Updated to support pairing issues --- .../RCTBluetoothSerialModule.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index 473f0ee..c081f68 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -92,6 +92,7 @@ public RCTBluetoothSerialModule(ReactApplicationContext reactContext) { mReactContext.addActivityEventListener(this); mReactContext.addLifecycleEventListener(this); registerBluetoothStateReceiver(); + registerBondStateReceiver(); } @Override @@ -812,8 +813,30 @@ public void onReceive(Context context, Intent intent) { if (D) Log.d(TAG, "Bluetooth was enabled"); sendEvent(BT_ENABLED, null); break; + } + } + } + }; + + mReactContext.registerReceiver(bluetoothStateReceiver, intentFilter); + } + + /** + * Register receiver for bond state change + */ + private void registerBondStateReceiver() { + IntentFilter intentFilter = new IntentFilter(); + + intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - case BluetoothAdapter.BOND_NONE: + final BroadcastReceiver bondStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { + final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); + switch (state) { + case BluetoothDevice.BOND_NONE: if (D) Log.d(TAG, "Unsuccessful device pairing"); sendEvent(PAIRING_FAILED, null); break; @@ -822,6 +845,6 @@ public void onReceive(Context context, Intent intent) { } }; - mReactContext.registerReceiver(bluetoothStateReceiver, intentFilter); + mReactContext.registerReceiver(bondStateReceiver, intentFilter); } } From 48712213984c1d2b743a38541dba385c9ded44fe Mon Sep 17 00:00:00 2001 From: Andrii Kuts Date: Thu, 9 Aug 2018 16:41:50 +0300 Subject: [PATCH 5/5] Updated to support pairing issues --- .../RCTBluetoothSerialModule.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index c081f68..6fefe0a 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -729,19 +729,6 @@ public void onReceive(Context context, Intent intent) { Log.e(TAG, "Cannot unregister receiver", e); onError(e); } - } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDING){ - if (D) Log.d(TAG, "Pairing canceled"); - if (mPairDevicePromise != null) { - mPairDevicePromise.resolve(true); - mPairDevicePromise = null; - } - try { - onPairingFailed(); - mReactContext.unregisterReceiver(this); - } catch (Exception e) { - Log.e(TAG, "Pairing canceled", e); - onError(e); - } } } }