Skip to content

Commit

Permalink
isConnectable
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargamil committed Nov 1, 2023
1 parent 12e4f64 commit d6c2576
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
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

0 comments on commit d6c2576

Please sign in to comment.