Skip to content

Commit 329b3fe

Browse files
authored
Merge pull request #190 from DimitrisRK/master
fix: BLE Scanner not working on devices with API < 21
2 parents dcc15d4 + 5ff729f commit 329b3fe

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ plugin/**/*.js
1313
plugin/**/*.d.ts
1414

1515
!plugin/platforms
16-
/plugin/platforms/android/nativescript_bluetooth.aar
16+
/plugin/platforms/android/ble.aar
1717
package-lock.json

src/bluetooth.android.ts

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,46 @@ function initLeScanCallback() {
485485
}
486486
}
487487

488-
@NativeClass
488+
/**
489+
* Do not mark this one as a native class. Not doing so will allow this class to be compiled into a JavaScript pure class.
490+
* For a strange reason, LeScanCallback will throw errors if it's compiled into a function.
491+
* That is why we want it to remain a class after compile procedure.
492+
* Also, class will work properly if implementor is given as an argument to super class since method 'onLeScan' is originally abstract.
493+
*/
489494
class LeScanCallbackImpl extends android.bluetooth.BluetoothAdapter.LeScanCallback {
490495
onPeripheralDiscovered: (data: Peripheral) => void;
491496

492497
constructor(private owner: WeakRef<Bluetooth>) {
493-
super();
498+
super(
499+
{
500+
onLeScan: function(device: android.bluetooth.BluetoothDevice, rssi: number, data: number[])
501+
{
502+
CLog(CLogTypes.info, `TNS_LeScanCallback.onLeScan ---- device: ${device}, rssi: ${rssi}, scanRecord: ${data}`);
503+
504+
let stateObject = this.owner.get().connections[device.getAddress()];
505+
if (!stateObject) {
506+
stateObject = this.owner.get().connections[device.getAddress()] = {
507+
state: 'disconnected',
508+
};
509+
const scanRecord = parseFromBytes(data);
510+
const advertismentData = new ScanAdvertisment(scanRecord);
511+
stateObject.advertismentData = advertismentData;
512+
const payload = {
513+
type: 'scanResult', // TODO or use different callback functions?
514+
UUID: device.getAddress(), // TODO consider renaming to id (and iOS as well)
515+
name: device.getName(),
516+
localName: advertismentData.localName,
517+
RSSI: rssi,
518+
state: 'disconnected',
519+
advertismentData,
520+
manufacturerId: advertismentData.manufacturerId,
521+
};
522+
CLog(CLogTypes.info, `TNS_LeScanCallback.onLeScan ---- payload: ${JSON.stringify(payload)}`);
523+
this.onPeripheralDiscovered && this.onPeripheralDiscovered(payload);
524+
this.owner.get().sendEvent(Bluetooth.device_discovered_event, payload);
525+
}
526+
}
527+
});
494528
/**
495529
* Callback reporting an LE device found during a device scan initiated by the startLeScan(BluetoothAdapter.LeScanCallback) function.
496530
* @param device [android.bluetooth.BluetoothDevice] - Identifies the remote device
@@ -499,33 +533,6 @@ function initLeScanCallback() {
499533
*/
500534
return global.__native(this);
501535
}
502-
503-
onLeScan(device: android.bluetooth.BluetoothDevice, rssi: number, data: number[]) {
504-
CLog(CLogTypes.info, `TNS_LeScanCallback.onLeScan ---- device: ${device}, rssi: ${rssi}, scanRecord: ${data}`);
505-
506-
let stateObject = this.owner.get().connections[device.getAddress()];
507-
if (!stateObject) {
508-
stateObject = this.owner.get().connections[device.getAddress()] = {
509-
state: 'disconnected',
510-
};
511-
const scanRecord = parseFromBytes(data);
512-
const advertismentData = new ScanAdvertisment(scanRecord);
513-
stateObject.advertismentData = advertismentData;
514-
const payload = {
515-
type: 'scanResult', // TODO or use different callback functions?
516-
UUID: device.getAddress(), // TODO consider renaming to id (and iOS as well)
517-
name: device.getName(),
518-
localName: advertismentData.localName,
519-
RSSI: rssi,
520-
state: 'disconnected',
521-
advertismentData,
522-
manufacturerId: advertismentData.manufacturerId,
523-
};
524-
CLog(CLogTypes.info, `TNS_LeScanCallback.onLeScan ---- payload: ${JSON.stringify(payload)}`);
525-
this.onPeripheralDiscovered && this.onPeripheralDiscovered(payload);
526-
this.owner.get().sendEvent(Bluetooth.device_discovered_event, payload);
527-
}
528-
}
529536
}
530537
LeScanCallbackVar = LeScanCallbackImpl;
531538
}

0 commit comments

Comments
 (0)