Skip to content

Commit 8b826e1

Browse files
authored
Feature Request: isConnectable Property for Android #823 (#993)
1 parent 12e4f64 commit 8b826e1

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ the [ble-central-advertisements](https://github.com/jospete/ble-central-advertis
13081308
"id": "00:1A:7D:DA:71:13",
13091309
"advertising": ArrayBuffer,
13101310
"rssi": -37
1311+
"connectable":"true" /*Only on Android >= API Level 26*/
13111312
}
13121313

13131314
Convert the advertising info to a Uint8Array for processing. `var adData = new Uint8Array(peripheral.advertising)`. You application is responsible for parsing all the information out of the advertising ArrayBuffer using the [GAP type constants](https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile). For example to get the service data from the advertising info, I [parse the advertising info into a map](https://github.com/don/ITP-BluetoothLE/blob/887511c375b1ab2fbef3afe210d6a6b7db44cee9/phonegap/thermometer_v2/www/js/index.js#L18-L39) and then get the service data to retrieve a [characteristic value that is being broadcast](https://github.com/don/ITP-BluetoothLE/blob/887511c375b1ab2fbef3afe210d6a6b7db44cee9/phonegap/thermometer_v2/www/js/index.js#L93-L103).

src/android/BLECentralPlugin.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,13 @@ private void onBluetoothStateChange(Intent intent) {
617617
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
618618
sendBluetoothStateChange(state);
619619
if (state == BluetoothAdapter.STATE_OFF) {
620-
// #894 When Bluetooth is physically turned off the whole process might die, so the normal
620+
// #894 When Bluetooth is physically turned off the whole process might die, so the normal
621621
// onConnectionStateChange callbacks won't be invoked
622-
622+
623623
BluetoothManager bluetoothManager = (BluetoothManager) cordova.getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
624624
for(Peripheral peripheral : peripherals.values()) {
625625
if (!peripheral.isConnected()) continue;
626-
626+
627627
int connectedState = bluetoothManager.getConnectionState(peripheral.getDevice(), BluetoothProfile.GATT);
628628
if (connectedState == BluetoothProfile.STATE_DISCONNECTED) {
629629
peripheral.peripheralDisconnected("Bluetooth Disabled");
@@ -1140,7 +1140,12 @@ public void onScanResult(int callbackType, ScanResult result) {
11401140

11411141
if (!alreadyReported) {
11421142

1143-
Peripheral peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes());
1143+
Peripheral peripheral = null;
1144+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
1145+
peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes(),result.isConnectable());
1146+
}else{
1147+
peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes());
1148+
}
11441149
peripherals.put(device.getAddress(), peripheral);
11451150

11461151
if (discoverCallback != null) {
@@ -1152,7 +1157,11 @@ public void onScanResult(int callbackType, ScanResult result) {
11521157
} else {
11531158
Peripheral peripheral = peripherals.get(address);
11541159
if (peripheral != null) {
1155-
peripheral.update(result.getRssi(), result.getScanRecord().getBytes());
1160+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
1161+
peripheral.update(result.getRssi(), result.getScanRecord().getBytes(),result.isConnectable());
1162+
}else{
1163+
peripheral.update(result.getRssi(), result.getScanRecord().getBytes());
1164+
}
11561165
if (reportDuplicates && discoverCallback != null) {
11571166
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, peripheral.asJSONObject());
11581167
pluginResult.setKeepCallback(true);
@@ -1279,7 +1288,7 @@ private void stopScan() {
12791288
LOG.d(TAG, "Stopping Scan");
12801289
try {
12811290
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
1282-
if (bluetoothLeScanner != null)
1291+
if (bluetoothLeScanner != null)
12831292
bluetoothLeScanner.stopScan(leScanCallback);
12841293
} catch (Exception e) {
12851294
LOG.e(TAG, "Exception stopping scan", e);
@@ -1468,7 +1477,7 @@ public void onReceive(Context context, Intent intent) {
14681477
if (ACTION_BOND_STATE_CHANGED.equals(action)) {
14691478
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
14701479
Peripheral peripheral = peripherals.get(device.getAddress());
1471-
1480+
14721481
if (peripheral != null) {
14731482
int bondState = intent.getIntExtra(EXTRA_BOND_STATE, BluetoothDevice.ERROR);
14741483
int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);

src/android/Peripheral.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class Peripheral extends BluetoothGattCallback {
5353

5454
private BluetoothDevice device;
5555
private byte[] advertisingData;
56+
private boolean isConnectable = true;
5657
private int advertisingRSSI;
5758
private boolean autoconnect = false;
5859
private boolean connected = false;
@@ -83,6 +84,13 @@ public Peripheral(BluetoothDevice device) {
8384

8485
}
8586

87+
public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, boolean isConnectable) {
88+
this.device = device;
89+
this.advertisingRSSI = advertisingRSSI;
90+
this.advertisingData = scanRecord;
91+
this.isConnectable = isConnectable;
92+
}
93+
8694
public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord) {
8795

8896
this.device = device;
@@ -280,6 +288,10 @@ public JSONObject asJSONObject() {
280288
if (advertisingRSSI != FAKE_PERIPHERAL_RSSI) {
281289
json.put("rssi", advertisingRSSI);
282290
}
291+
292+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
293+
json.put("connectable", this.isConnectable);
294+
}
283295
} catch (JSONException e) { // this shouldn't happen
284296
e.printStackTrace();
285297
}
@@ -515,11 +527,19 @@ public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
515527
}
516528

517529
// Update rssi and scanRecord.
530+
public void update(int rssi, byte[] scanRecord, boolean isConnectable) {
531+
this.advertisingRSSI = rssi;
532+
this.advertisingData = scanRecord;
533+
this.isConnectable = isConnectable;
534+
}
535+
536+
518537
public void update(int rssi, byte[] scanRecord) {
519538
this.advertisingRSSI = rssi;
520539
this.advertisingData = scanRecord;
521540
}
522541

542+
523543
public void updateRssi(int rssi) {
524544
advertisingRSSI = rssi;
525545
}
@@ -1024,7 +1044,7 @@ public void bond(CallbackContext callbackContext, BluetoothAdapter bluetoothAdap
10241044
}
10251045
}
10261046
}
1027-
1047+
10281048
@RequiresPermission("android.permission.BLUETOOTH_CONNECT")
10291049
public void unbond(CallbackContext callbackContext) {
10301050
final int bondState = device.getBondState();

0 commit comments

Comments
 (0)