Skip to content

Commit d7d0e43

Browse files
author
Siddaiah M
committed
Modified code to allow auto paring
- added intent and action to pair automatically with ble device - in createBond passing peripheralUUID and pin will pair with that peripheral automatically - just passing peripheralUUID will ask for pin (manual action)
1 parent dc88c6a commit d7d0e43

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

BleManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ class BleManager {
131131
});
132132
}
133133

134-
createBond(peripheralId) {
134+
createBond(peripheralId,peripheralPin=null) {
135135
return new Promise((fulfill, reject) => {
136-
bleManager.createBond(peripheralId, error => {
136+
bleManager.createBond(peripheralId,peripheralPin, error => {
137137
if (error) {
138138
reject(error);
139139
} else {

android/src/main/java/it/innove/BleManager.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ class BleManager extends ReactContextBaseJavaModule implements ActivityEventList
4444

4545
private class BondRequest {
4646
private String uuid;
47+
private String pin;
4748
private Callback callback;
4849

4950
BondRequest(String _uuid, Callback _callback) {
5051
uuid = _uuid;
5152
callback = _callback;
5253
}
54+
55+
BondRequest(String _uuid, String _pin, Callback _callback) {
56+
uuid = _uuid;
57+
pin = _pin;
58+
callback = _callback;
59+
}
5360
}
5461

5562
private BluetoothAdapter bluetoothAdapter;
@@ -124,6 +131,9 @@ public void start(ReadableMap options, Callback callback) {
124131
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
125132
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
126133
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);
127137
callback.invoke();
128138
Log.d(LOG_TAG, "BleManager initialized");
129139
}
@@ -193,7 +203,7 @@ public void stopScan(Callback callback) {
193203
}
194204

195205
@ReactMethod
196-
public void createBond(String peripheralUUID, Callback callback) {
206+
public void createBond(String peripheralUUID, String peripheralPin, Callback callback) {
197207
Log.d(LOG_TAG, "Request bond to: " + peripheralUUID);
198208

199209
Set<BluetoothDevice> deviceSet = getBluetoothAdapter().getBondedDevices();
@@ -213,7 +223,7 @@ public void createBond(String peripheralUUID, Callback callback) {
213223
return;
214224
} else if (peripheral.getDevice().createBond()) {
215225
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
217227
return;
218228
}
219229

@@ -529,6 +539,12 @@ public void onReceive(Context context, Intent intent) {
529539
removeBondRequest.callback.invoke();
530540
removeBondRequest = null;
531541
}
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+
}
532548
}
533549

534550
}

0 commit comments

Comments
 (0)