Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: isConnectable Property for Android #823 #993

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ the [ble-central-advertisements](https://github.com/jospete/ble-central-advertis
"id": "00:1A:7D:DA:71:13",
"advertising": ArrayBuffer,
"rssi": -37
"connectable":"true" /*Only on Android >= API Level 26*/
}

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).
Expand Down
23 changes: 16 additions & 7 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,13 @@ private void onBluetoothStateChange(Intent intent) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
sendBluetoothStateChange(state);
if (state == BluetoothAdapter.STATE_OFF) {
// #894 When Bluetooth is physically turned off the whole process might die, so the normal
// #894 When Bluetooth is physically turned off the whole process might die, so the normal
// onConnectionStateChange callbacks won't be invoked

BluetoothManager bluetoothManager = (BluetoothManager) cordova.getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
for(Peripheral peripheral : peripherals.values()) {
if (!peripheral.isConnected()) continue;

int connectedState = bluetoothManager.getConnectionState(peripheral.getDevice(), BluetoothProfile.GATT);
if (connectedState == BluetoothProfile.STATE_DISCONNECTED) {
peripheral.peripheralDisconnected("Bluetooth Disabled");
Expand Down Expand Up @@ -1140,7 +1140,12 @@ public void onScanResult(int callbackType, ScanResult result) {

if (!alreadyReported) {

Peripheral peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes());
Peripheral peripheral = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes(),result.isConnectable());
}else{
peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes());
}
peripherals.put(device.getAddress(), peripheral);

if (discoverCallback != null) {
Expand All @@ -1152,7 +1157,11 @@ public void onScanResult(int callbackType, ScanResult result) {
} else {
Peripheral peripheral = peripherals.get(address);
if (peripheral != null) {
peripheral.update(result.getRssi(), result.getScanRecord().getBytes());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
peripheral.update(result.getRssi(), result.getScanRecord().getBytes(),result.isConnectable());
}else{
peripheral.update(result.getRssi(), result.getScanRecord().getBytes());
}
if (reportDuplicates && discoverCallback != null) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, peripheral.asJSONObject());
pluginResult.setKeepCallback(true);
Expand Down Expand Up @@ -1279,7 +1288,7 @@ private void stopScan() {
LOG.d(TAG, "Stopping Scan");
try {
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
if (bluetoothLeScanner != null)
if (bluetoothLeScanner != null)
bluetoothLeScanner.stopScan(leScanCallback);
} catch (Exception e) {
LOG.e(TAG, "Exception stopping scan", e);
Expand Down Expand Up @@ -1468,7 +1477,7 @@ public void onReceive(Context context, Intent intent) {
if (ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Peripheral peripheral = peripherals.get(device.getAddress());

if (peripheral != null) {
int bondState = intent.getIntExtra(EXTRA_BOND_STATE, BluetoothDevice.ERROR);
int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
Expand Down
22 changes: 21 additions & 1 deletion src/android/Peripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class Peripheral extends BluetoothGattCallback {

private BluetoothDevice device;
private byte[] advertisingData;
private boolean isConnectable = true;
private int advertisingRSSI;
private boolean autoconnect = false;
private boolean connected = false;
Expand Down Expand Up @@ -83,6 +84,13 @@ public Peripheral(BluetoothDevice device) {

}

public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, boolean isConnectable) {
this.device = device;
this.advertisingRSSI = advertisingRSSI;
this.advertisingData = scanRecord;
this.isConnectable = isConnectable;
}

public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord) {

this.device = device;
Expand Down Expand Up @@ -280,6 +288,10 @@ public JSONObject asJSONObject() {
if (advertisingRSSI != FAKE_PERIPHERAL_RSSI) {
json.put("rssi", advertisingRSSI);
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
json.put("connectable", this.isConnectable);
}
} catch (JSONException e) { // this shouldn't happen
e.printStackTrace();
}
Expand Down Expand Up @@ -515,11 +527,19 @@ public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
}

// Update rssi and scanRecord.
public void update(int rssi, byte[] scanRecord, boolean isConnectable) {
this.advertisingRSSI = rssi;
this.advertisingData = scanRecord;
this.isConnectable = isConnectable;
}


public void update(int rssi, byte[] scanRecord) {
this.advertisingRSSI = rssi;
this.advertisingData = scanRecord;
}


public void updateRssi(int rssi) {
advertisingRSSI = rssi;
}
Expand Down Expand Up @@ -1024,7 +1044,7 @@ public void bond(CallbackContext callbackContext, BluetoothAdapter bluetoothAdap
}
}
}

@RequiresPermission("android.permission.BLUETOOTH_CONNECT")
public void unbond(CallbackContext callbackContext) {
final int bondState = device.getBondState();
Expand Down