Skip to content

Commit 213e7e4

Browse files
committed
fix(android): Disconnect listeners now only accept disconnect events from matching devices.
1 parent 69ace2e commit 213e7e4

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/bluetooth.android.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ export interface BluetoothGattCallbackWithSubCallback extends android.bluetooth.
712712
addSubDelegate(delegate: SubBluetoothGattCallback);
713713
removeSubDelegate(delegate: SubBluetoothGattCallback);
714714
}
715-
export type DisconnectListener = () => void;
715+
export type DisconnectListener = (address) => void;
716716
function initBluetoothGattCallback() {
717717
if (BluetoothGattCallback) {
718718
return;
@@ -1652,13 +1652,15 @@ export class Bluetooth extends BluetoothCommon {
16521652
reject(err);
16531653
clearListeners();
16541654
};
1655-
const onDisconnect = () => {
1656-
onError(
1657-
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
1658-
method: methodName,
1659-
arguments: args,
1660-
})
1661-
);
1655+
const onDisconnect = (address) => {
1656+
if (address === pUUID) {
1657+
onError(
1658+
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
1659+
method: methodName,
1660+
arguments: args,
1661+
})
1662+
);
1663+
}
16621664
};
16631665
const subD = {
16641666
onConnectionStateChange: (gatt: android.bluetooth.BluetoothGatt, status: number, newState: number) => {
@@ -2314,13 +2316,15 @@ export class Bluetooth extends BluetoothCommon {
23142316
reject(err);
23152317
clearListeners();
23162318
};
2317-
const onDisconnect = () => {
2318-
onError(
2319-
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
2320-
method: methodName,
2321-
arguments: args,
2322-
})
2323-
);
2319+
const onDisconnect = (address) => {
2320+
if (address === args.peripheralUUID) {
2321+
onError(
2322+
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
2323+
method: methodName,
2324+
arguments: args,
2325+
})
2326+
);
2327+
}
23242328
};
23252329
this.addDisconnectListener(onDisconnect);
23262330
try {
@@ -2506,8 +2510,7 @@ export class Bluetooth extends BluetoothCommon {
25062510
}
25072511
if (this.disconnectListeners.length > 0) {
25082512
const error = new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {});
2509-
this.disconnectListeners.forEach((d) => d());
2510-
this.disconnectListeners = [];
2513+
this.disconnectListeners = this.disconnectListeners.filter((d) => d(address));
25112514
}
25122515

25132516
gatt.close();
@@ -2627,13 +2630,15 @@ export class Bluetooth extends BluetoothCommon {
26272630
ctx.reject(err);
26282631
clearListeners();
26292632
};
2630-
const onDisconnect = () => {
2631-
onError(
2632-
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
2633-
method: ctx.methodName,
2634-
arguments: ctx.args,
2635-
})
2636-
);
2633+
const onDisconnect = (address) => {
2634+
if (address === ctx.args.peripheralUUID) {
2635+
onError(
2636+
new BluetoothError(BluetoothCommon.msg_peripheral_disconnected, {
2637+
method: ctx.methodName,
2638+
arguments: ctx.args,
2639+
})
2640+
);
2641+
}
26372642
};
26382643
const subD = createSubDelegate(clearListeners, onError);
26392644
this.bluetoothGattCallback.addSubDelegate(subD);

0 commit comments

Comments
 (0)