@@ -44,12 +44,19 @@ class BleManager extends ReactContextBaseJavaModule implements ActivityEventList
44
44
45
45
private class BondRequest {
46
46
private String uuid ;
47
+ private String pin ;
47
48
private Callback callback ;
48
49
49
50
BondRequest (String _uuid , Callback _callback ) {
50
51
uuid = _uuid ;
51
52
callback = _callback ;
52
53
}
54
+
55
+ BondRequest (String _uuid , String _pin , Callback _callback ) {
56
+ uuid = _uuid ;
57
+ pin = _pin ;
58
+ callback = _callback ;
59
+ }
53
60
}
54
61
55
62
private BluetoothAdapter bluetoothAdapter ;
@@ -124,6 +131,9 @@ public void start(ReadableMap options, Callback callback) {
124
131
IntentFilter filter = new IntentFilter (BluetoothAdapter .ACTION_STATE_CHANGED );
125
132
filter .addAction (BluetoothDevice .ACTION_BOND_STATE_CHANGED );
126
133
context .registerReceiver (mReceiver , filter );
134
+ IntentFilter intentFilter = new IntentFilter (BluetoothDevice .ACTION_PAIRING_REQUEST );
135
+ intentFilter .setPriority (IntentFilter .SYSTEM_HIGH_PRIORITY );
136
+ context .registerReceiver (mReceiver , intentFilter );
127
137
callback .invoke ();
128
138
Log .d (LOG_TAG , "BleManager initialized" );
129
139
}
@@ -193,7 +203,7 @@ public void stopScan(Callback callback) {
193
203
}
194
204
195
205
@ ReactMethod
196
- public void createBond (String peripheralUUID , Callback callback ) {
206
+ public void createBond (String peripheralUUID , String peripheralPin , Callback callback ) {
197
207
Log .d (LOG_TAG , "Request bond to: " + peripheralUUID );
198
208
199
209
Set <BluetoothDevice > deviceSet = getBluetoothAdapter ().getBondedDevices ();
@@ -213,7 +223,7 @@ public void createBond(String peripheralUUID, Callback callback) {
213
223
return ;
214
224
} else if (peripheral .getDevice ().createBond ()) {
215
225
Log .d (LOG_TAG , "Request bond successful for: " + peripheralUUID );
216
- bondRequest = new BondRequest (peripheralUUID , callback ); // request bond success, waiting for boradcast
226
+ bondRequest = new BondRequest (peripheralUUID , peripheralPin , callback ); // request bond success, waiting for boradcast
217
227
return ;
218
228
}
219
229
@@ -529,6 +539,12 @@ public void onReceive(Context context, Intent intent) {
529
539
removeBondRequest .callback .invoke ();
530
540
removeBondRequest = null ;
531
541
}
542
+ } else if (action .equals (BluetoothDevice .ACTION_PAIRING_REQUEST )){
543
+ BluetoothDevice bluetoothDevice = intent .getParcelableExtra (BluetoothDevice .EXTRA_DEVICE );
544
+ if (bondRequest != null && bondRequest .uuid .equals (bluetoothDevice .getAddress ()) && bondRequest .pin != null ) {
545
+ bluetoothDevice .setPin (bondRequest .pin .getBytes ());
546
+ bluetoothDevice .createBond ();
547
+ }
532
548
}
533
549
534
550
}
0 commit comments