@@ -34,6 +34,7 @@ public class BluetoothSerial extends CordovaPlugin {
3434 private static final String LIST = "list" ;
3535 private static final String CONNECT = "connect" ;
3636 private static final String CONNECT_INSECURE = "connectInsecure" ;
37+ private static final String BIND = "bind" ;
3738 private static final String DISCONNECT = "disconnect" ;
3839 private static final String WRITE = "write" ;
3940 private static final String AVAILABLE = "available" ;
@@ -119,6 +120,10 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
119120 boolean secure = false ;
120121 connect (args , secure , callbackContext );
121122
123+ } else if (action .equals (BIND )) {
124+
125+ bind (args , callbackContext );
126+
122127 } else if (action .equals (DISCONNECT )) {
123128
124129 connectCallback = null ;
@@ -301,7 +306,7 @@ public void onReceive(Context context, Intent intent) {
301306 if (BluetoothDevice .ACTION_FOUND .equals (action )) {
302307 BluetoothDevice device = intent .getParcelableExtra (BluetoothDevice .EXTRA_DEVICE );
303308 try {
304- JSONObject o = deviceToJSON (device );
309+ JSONObject o = deviceToJSON (device );
305310 unpairedDevices .put (o );
306311 if (ddc != null ) {
307312 PluginResult res = new PluginResult (PluginResult .Status .OK , o );
@@ -354,28 +359,74 @@ private void connect(CordovaArgs args, boolean secure, CallbackContext callbackC
354359 }
355360 }
356361
362+ private void bind (CordovaArgs args , CallbackContext callbackContext ) throws JSONException {
363+ String macAddress = args .getString (0 );
364+ BluetoothDevice device = bluetoothAdapter .getRemoteDevice (macAddress );
365+ if (device == null ) {
366+ Log .e (TAG , "Unknown device: " + macAddress );
367+ callbackContext .error ("Could not bind with " + macAddress );
368+ return ;
369+ }
370+
371+ String Pin = args .getString (1 );
372+
373+ final BroadcastReceiver pairingReceiver = new BroadcastReceiver () {
374+
375+ public void onReceive (Context context , Intent intent ) {
376+ String action = intent .getAction ();
377+ int previousState = intent .getIntExtra (BluetoothDevice .EXTRA_PREVIOUS_BOND_STATE , 0 );
378+ int boundState = intent .getIntExtra (BluetoothDevice .EXTRA_BOND_STATE , 0 );
379+ Log .d (TAG , "Received: " + action + " previous: " + previousState + " current: " + boundState );
380+ if (BluetoothDevice .ACTION_PAIRING_REQUEST .equals (action )) {
381+ Log .d (TAG , "Setting the PIN" );
382+ BluetoothDevice device = intent .getParcelableExtra (BluetoothDevice .EXTRA_DEVICE );
383+ byte [] pinBytes = Pin .getBytes ();
384+ device .setPin (pinBytes );
385+ device .setPairingConfirmation (true );
386+ }
387+ if (bondState == BluetoothDevice .BOND_BONDED )
388+ callbackContext .success ("PAIRED correctly" );
389+ else if (bondState == BluetoothDevice .BOND_NONE )
390+ callbackContext .error ("PAIR failed because of state:" + bondState );
391+ if (bondState == BluetoothDevice .BOND_BONDED || bondState == BluetoothDevice .BOND_NONE )
392+ cordova .getActivity ().unregisterReceiver (this );
393+ }
394+ };
395+
396+ Activity activity = cordova .getActivity ();
397+ activity .registerReceiver (pairingReceiver , new IntentFilter (BluetoothDevice .ACTION_BOND_STATE_CHANGED ));
398+ activity .registerReceiver (pairingReceiver , new IntentFilter (BluetoothAdapter .ACTION_PAIRING_REQUEST ));
399+ activity .registerReceiver (pairingReceiver , new IntentFilter (BluetoothAdapter .ACTION_STATE_CHANGED ));
400+
401+ if (!device .createBound ()) {
402+ Log .e (TAG , "Somehow unable to createBound(). Already bound ?" )
403+ callbackContext .success ("PAIRED already" );
404+ }
405+
406+ }
407+
357408 // The Handler that gets information back from the BluetoothSerialService
358409 // Original code used handler for the because it was talking to the UI.
359410 // Consider replacing with normal callbacks
360411 private final Handler mHandler = new Handler () {
361412
362- public void handleMessage (Message msg ) {
363- switch (msg .what ) {
364- case MESSAGE_READ :
365- buffer .append ((String )msg .obj );
413+ public void handleMessage (Message msg ) {
414+ switch (msg .what ) {
415+ case MESSAGE_READ :
416+ buffer .append ((String ) msg .obj );
366417
367- if (dataAvailableCallback != null ) {
368- sendDataToSubscriber ();
369- }
418+ if (dataAvailableCallback != null ) {
419+ sendDataToSubscriber ();
420+ }
370421
371- break ;
372- case MESSAGE_READ_RAW :
373- if (rawDataAvailableCallback != null ) {
374- byte [] bytes = (byte []) msg .obj ;
375- sendRawDataToSubscriber (bytes );
376- }
377- break ;
378- case MESSAGE_STATE_CHANGE :
422+ break ;
423+ case MESSAGE_READ_RAW :
424+ if (rawDataAvailableCallback != null ) {
425+ byte [] bytes = (byte []) msg .obj ;
426+ sendRawDataToSubscriber (bytes );
427+ }
428+ break ;
429+ case MESSAGE_STATE_CHANGE :
379430
380431 if (D ) Log .i (TAG , "MESSAGE_STATE_CHANGE: " + msg .arg1 );
381432 switch (msg .arg1 ) {
@@ -469,22 +520,22 @@ private String readUntil(String c) {
469520 public void onRequestPermissionResult (int requestCode , String [] permissions ,
470521 int [] grantResults ) throws JSONException {
471522
472- for (int result : grantResults ) {
473- if (result == PackageManager .PERMISSION_DENIED ) {
523+ for (int result : grantResults ) {
524+ if (result == PackageManager .PERMISSION_DENIED ) {
474525 LOG .d (TAG , "User *rejected* location permission" );
475526 this .permissionCallback .sendPluginResult (new PluginResult (
476527 PluginResult .Status .ERROR ,
477528 "Location permission is required to discover unpaired devices." )
478- );
529+ );
479530 return ;
480531 }
481532 }
482533
483- switch (requestCode ) {
484- case CHECK_PERMISSIONS_REQ_CODE :
485- LOG .d (TAG , "User granted location permission" );
486- discoverUnpairedDevices (permissionCallback );
487- break ;
534+ switch (requestCode ) {
535+ case CHECK_PERMISSIONS_REQ_CODE :
536+ LOG .d (TAG , "User granted location permission" );
537+ discoverUnpairedDevices (permissionCallback );
538+ break ;
488539 }
489540 }
490541}
0 commit comments