@@ -129,6 +129,7 @@ public class BluetoothLePlugin extends CordovaPlugin {
129129 private final String keyDiscoveredState = "discoveredState" ;
130130 private final String keyConnectionPriority = "connectionPriority" ;
131131 private final String keyMtu = "mtu" ;
132+ private final String keyPin = "pin" ;
132133
133134 //Write Types
134135 private final String writeTypeNoResponse = "noResponse" ;
@@ -421,6 +422,8 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
421422 respondAction (args , callbackContext );
422423 } else if ("notify" .equals (action )) {
423424 notifyAction (args , callbackContext );
425+ } else if ("setPin" .equals (action )) {
426+ setPinAction (args , callbackContext );
424427 } else {
425428 return false ;
426429 }
@@ -2767,6 +2770,66 @@ private void requestConnectionPriorityAction(JSONArray args, CallbackContext cal
27672770 }
27682771 }
27692772
2773+ private void setPinAction (JSONArray args , CallbackContext callbackContext ) {
2774+ Log .d ("BLE" ,"set pin" );
2775+ if (mPairingRequestReceiver !=null ) {
2776+ cordova .getActivity ().unregisterReceiver (mPairingRequestReceiver );
2777+ }
2778+
2779+ if (isNotInitialized (callbackContext , true )) {
2780+ return ;
2781+ }
2782+
2783+ JSONObject obj = getArgsObject (args );
2784+ if (isNotArgsObject (obj , callbackContext )) {
2785+ return ;
2786+ }
2787+
2788+ String address = getAddress (obj );
2789+ if (isNotAddress (address , callbackContext )) {
2790+ return ;
2791+ }
2792+
2793+ String pin = getPin (obj );
2794+ if (pin ==null ) {
2795+ return ;
2796+ }
2797+
2798+ Log .d ("BLE" ,"set pin " + address + " " + pin );
2799+ JSONObject returnObj = new JSONObject ();
2800+ try {
2801+ mPairingRequestReceiver = new BroadcastReceiver () {
2802+ @ Override
2803+ public void onReceive (Context context , Intent intent ) {
2804+ Log .d ("BLE" , "on receive" );
2805+ String action = intent .getAction ();
2806+ if (BluetoothDevice .ACTION_PAIRING_REQUEST .equals (action )) {
2807+ BluetoothDevice bluetoothDevice = intent .getParcelableExtra (BluetoothDevice .EXTRA_DEVICE );
2808+ if (bluetoothDevice .getAddress ().equalsIgnoreCase (address )){
2809+ int type = intent .getIntExtra (BluetoothDevice .EXTRA_PAIRING_VARIANT , BluetoothDevice .ERROR );
2810+ if (type == BluetoothDevice .PAIRING_VARIANT_PIN ) {
2811+ bluetoothDevice .setPin (pin .getBytes ());
2812+ abortBroadcast ();
2813+ }
2814+ }
2815+ }
2816+ }
2817+ };
2818+ IntentFilter intentFilter = new IntentFilter (BluetoothDevice .ACTION_PAIRING_REQUEST );
2819+ intentFilter .setPriority (IntentFilter .SYSTEM_HIGH_PRIORITY );
2820+ cordova .getActivity ().registerReceiver (mPairingRequestReceiver , intentFilter );
2821+ addProperty (returnObj , keyStatus , "pinSet" );
2822+ callbackContext .success (returnObj );
2823+ } catch (Exception e ) {
2824+ Log .d ("BLE" ,"exception " + e .getMessage ());
2825+ addProperty (returnObj , keyError , "setPin" );
2826+ addProperty (returnObj , keyMessage , "Failed to set pin" );
2827+ callbackContext .error (returnObj );
2828+ }
2829+ return ;
2830+
2831+ }
2832+
27702833 @ Override
27712834 public void onDestroy () {
27722835 super .onDestroy ();
@@ -2777,6 +2840,9 @@ public void onDestroy() {
27772840 if (isBondReceiverRegistered ) {
27782841 cordova .getActivity ().unregisterReceiver (mBondReceiver );
27792842 }
2843+ if (mPairingRequestReceiver !=null ){
2844+ cordova .getActivity ().unregisterReceiver (mPairingRequestReceiver );
2845+ }
27802846 }
27812847
27822848 private BroadcastReceiver mReceiver = new BroadcastReceiver () {
@@ -2888,6 +2954,8 @@ public void onReceive(Context context, Intent intent) {
28882954 }
28892955 }
28902956 };
2957+
2958+ private BroadcastReceiver mPairingRequestReceiver ;
28912959
28922960 @ Override
28932961 public void onActivityResult (int requestCode , int resultCode , Intent intent ) {
@@ -3903,6 +3971,12 @@ private JSONObject getPermissions(BluetoothGattDescriptor descriptor) {
39033971 return permissionsObject ;
39043972 }
39053973
3974+ private String getPin (JSONObject obj ) {
3975+ //Get the pin string from arguments
3976+ String pin = obj .optString (keyPin , null );
3977+ return pin ;
3978+ }
3979+
39063980 //Bluetooth callback for connecting, discovering, reading and writing
39073981 private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback () {
39083982 @ Override
0 commit comments