diff --git a/src/android/BLECentralPlugin.java b/src/android/BLECentralPlugin.java index 0d820c64..08953858 100644 --- a/src/android/BLECentralPlugin.java +++ b/src/android/BLECentralPlugin.java @@ -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"); @@ -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) { @@ -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); @@ -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); @@ -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); diff --git a/src/android/Peripheral.java b/src/android/Peripheral.java index 9b7b15a9..46acf821 100644 --- a/src/android/Peripheral.java +++ b/src/android/Peripheral.java @@ -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; @@ -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; @@ -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(); } @@ -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; } @@ -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();