@@ -61,12 +61,14 @@ public class BluetoothLePlugin extends CordovaPlugin {
6161 private final int REQUEST_LOCATION_SOURCE_SETTINGS = 59629 ;
6262 private BluetoothAdapter bluetoothAdapter ;
6363 private boolean isReceiverRegistered = false ;
64+ private boolean isBondReceiverRegistered = false ;
6465
6566 //General callback variables
6667 private CallbackContext initCallbackContext ;
6768 private CallbackContext scanCallbackContext ;
6869 private CallbackContext permissionsCallback ;
6970 private CallbackContext locationCallback ;
71+ private CallbackContext bondCallbackContext ;
7072
7173 private CallbackContext initPeripheralCallback ;
7274 private BluetoothGattServer gattServer ;
@@ -145,6 +147,9 @@ public class BluetoothLePlugin extends CordovaPlugin {
145147 private final String statusRssi = "rssi" ;
146148 private final String statusConnectionPriorityRequested = "connectionPriorityRequested" ;
147149 private final String statusMtu = "mtu" ;
150+ private final String statusBonded = "bonded" ;
151+ private final String statusBonding = "bonding" ;
152+ private final String statusBondNone = "none" ;
148153
149154 //Properties
150155 private final String propertyBroadcast = "broadcast" ;
@@ -1353,6 +1358,9 @@ private void disconnectAction(JSONArray args, CallbackContext callbackContext) {
13531358 }
13541359
13551360 private void bondAction (JSONArray args , CallbackContext callbackContext ) {
1361+ //Save init callback
1362+ bondCallbackContext = callbackContext ;
1363+
13561364 if (isNotInitialized (callbackContext , true )) {
13571365 return ;
13581366 }
@@ -1362,6 +1370,14 @@ private void bondAction(JSONArray args, CallbackContext callbackContext) {
13621370 return ;
13631371 }
13641372
1373+ Activity activity = cordova .getActivity ();
1374+
1375+ if (obj != null ) {
1376+ //Add a receiver to pick up when Bluetooth state changes
1377+ activity .registerReceiver (mBondReceiver , new IntentFilter (BluetoothDevice .ACTION_BOND_STATE_CHANGED ));
1378+ isBondReceiverRegistered = true ;
1379+ }
1380+
13651381 String address = getAddress (obj );
13661382 if (isNotAddress (address , callbackContext )) {
13671383 return ;
@@ -1372,9 +1388,20 @@ private void bondAction(JSONArray args, CallbackContext callbackContext) {
13721388 return ;
13731389 }
13741390
1375- BluetoothGatt bluetoothGatt = (BluetoothGatt )connection .get (keyPeripheral );
1391+ BluetoothGatt bluetoothGatt = (BluetoothGatt ) connection .get (keyPeripheral );
13761392 BluetoothDevice device = bluetoothGatt .getDevice ();
1377- device .createBond ();
1393+
1394+ if (device .getBondState () == BluetoothDevice .BOND_BONDED ) {
1395+ JSONObject returnObj = new JSONObject ();
1396+ addProperty (returnObj , keyStatus , statusBonded );
1397+ PluginResult pluginResult = new PluginResult (PluginResult .Status .OK , returnObj );
1398+ pluginResult .setKeepCallback (true );
1399+ bondCallbackContext .sendPluginResult (pluginResult );
1400+ return ;
1401+ } else {
1402+ device .setPairingConfirmation (false );
1403+ device .createBond ();
1404+ }
13781405 }
13791406
13801407 private void closeAction (JSONArray args , CallbackContext callbackContext ) {
@@ -2456,13 +2483,15 @@ private void requestConnectionPriorityAction(JSONArray args, CallbackContext cal
24562483 }
24572484
24582485 @ Override
2459- public void onDestroy (){
2460- super .onDestroy ();
2486+ public void onDestroy () {
2487+ super .onDestroy ();
24612488
2462- if (isReceiverRegistered )
2463- {
2464- cordova .getActivity ().unregisterReceiver (mReceiver );
2465- }
2489+ if (isReceiverRegistered ) {
2490+ cordova .getActivity ().unregisterReceiver (mReceiver );
2491+ }
2492+ if (isBondReceiverRegistered ) {
2493+ cordova .getActivity ().unregisterReceiver (mBondReceiver );
2494+ }
24662495 }
24672496
24682497 private final BroadcastReceiver mReceiver = new BroadcastReceiver () {
@@ -2503,6 +2532,42 @@ public void onReceive(Context context, Intent intent) {
25032532 }
25042533 };
25052534
2535+ private final BroadcastReceiver mBondReceiver = new BroadcastReceiver () {
2536+ @ Override
2537+ public void onReceive (Context context , Intent intent ) {
2538+ if (bondCallbackContext == null ) {
2539+ return ;
2540+ }
2541+ if (intent .getAction ().equals (BluetoothDevice .ACTION_BOND_STATE_CHANGED )) {
2542+ JSONObject returnObj = new JSONObject ();
2543+ PluginResult pluginResult ;
2544+ switch (intent .getIntExtra (BluetoothDevice .EXTRA_BOND_STATE , BluetoothDevice .ERROR )){
2545+ case BluetoothDevice .BOND_BONDED :
2546+ addProperty (returnObj , keyStatus , statusBonded );
2547+
2548+ pluginResult = new PluginResult (PluginResult .Status .OK , returnObj );
2549+ pluginResult .setKeepCallback (true );
2550+ bondCallbackContext .sendPluginResult (pluginResult );
2551+ break ;
2552+ case BluetoothDevice .BOND_BONDING :
2553+ addProperty (returnObj , keyStatus , statusBonding );
2554+
2555+ pluginResult = new PluginResult (PluginResult .Status .OK , returnObj );
2556+ pluginResult .setKeepCallback (true );
2557+ bondCallbackContext .sendPluginResult (pluginResult );
2558+ break ;
2559+ case BluetoothDevice .BOND_NONE :
2560+ addProperty (returnObj , keyStatus , statusBondNone );
2561+
2562+ pluginResult = new PluginResult (PluginResult .Status .OK , returnObj );
2563+ pluginResult .setKeepCallback (true );
2564+ bondCallbackContext .sendPluginResult (pluginResult );
2565+ break ;
2566+ }
2567+ }
2568+ }
2569+ };
2570+
25062571 @ Override
25072572 public void onActivityResult (int requestCode , int resultCode , Intent intent ) {
25082573 //If this was a Bluetooth enablement request...
0 commit comments