Skip to content

Commit

Permalink
Add connectable flag to type definitions
Browse files Browse the repository at this point in the history
* Reduce version branching slightly by using nullable
  • Loading branch information
peitschie committed Nov 3, 2023
1 parent 8b826e1 commit 912ce83
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
= 1.7.2 =
Android: add isConnectable Property for API26+ (O) #823 (#993) thanks @Gargamil

= 1.7.1 =
Android: Add forceScanFilter option for Android (#989, #987) thanks younesspotmaster

Expand Down
9 changes: 4 additions & 5 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -1139,13 +1139,12 @@ public void onScanResult(int callbackType, ScanResult result) {
boolean alreadyReported = peripherals.containsKey(address) && !peripherals.get(address).isUnscanned();

if (!alreadyReported) {

Peripheral peripheral = null;
Boolean isConnectable = 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());
isConnectable = result.isConnectable();
}

Peripheral peripheral = new Peripheral(device, result.getRssi(), result.getScanRecord().getBytes(), isConnectable);
peripherals.put(device.getAddress(), peripheral);

if (discoverCallback != null) {
Expand Down
16 changes: 4 additions & 12 deletions src/android/Peripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Peripheral extends BluetoothGattCallback {

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

}

public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, boolean isConnectable) {
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;
this.advertisingRSSI = advertisingRSSI;
this.advertisingData = scanRecord;

}

private void gattConnect() {

closeGatt();
Expand Down Expand Up @@ -289,8 +281,8 @@ public JSONObject asJSONObject() {
json.put("rssi", advertisingRSSI);
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
json.put("connectable", this.isConnectable);
if (this.isConnectable != null) {
json.put("connectable", this.isConnectable.booleanValue());
}
} catch (JSONException e) { // this shouldn't happen
e.printStackTrace();
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare namespace BLECentralPlugin {
id: string;
rssi: number;
advertising: ArrayBuffer | any;
/* Android only */
connectable?: boolean;
state: PeripheralState;
}

Expand Down

0 comments on commit 912ce83

Please sign in to comment.