Skip to content

Commit b79f475

Browse files
committed
fix(android): register notifyCallback earlier and optimistically
1 parent 30e0fc6 commit b79f475

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/bluetooth.android.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,23 @@ export class Bluetooth extends BluetoothCommon {
22662266
}
22672267

22682268
const pUUID = args.peripheralUUID;
2269+
const cUUID = uuidToString(characteristic.getUuid());
2270+
const sUUID = uuidToString(characteristic.getService().getUuid());
2271+
2272+
const stateObject = this.connections[pUUID];
2273+
stateObject.onNotifyCallbacks = stateObject.onNotifyCallbacks || {};
2274+
const key = sUUID + '/' + cUUID;
2275+
const onNotify = args.onNotify;
2276+
stateObject.onNotifyCallbacks[key] = function (result) {
2277+
// CLog(
2278+
// CLogTypes.warning,
2279+
// `onNotifyCallback ---- UUID: ${UUID}, pUUID: ${pUUID}, cUUID: ${cUUID}, args.characteristicUUID: ${
2280+
// args.characteristicUUID
2281+
// }, sUUID: ${sUUID}, args.serviceUUID: ${args.serviceUUID}, result: ${result}`
2282+
// );
2283+
onNotify(result);
2284+
};
2285+
22692286
this.attachSubDelegate(
22702287
{methodName, args, resolve, reject},
22712288
(clearListeners, onError) => ({
@@ -2278,26 +2295,12 @@ export class Bluetooth extends BluetoothCommon {
22782295
UUID = device.getAddress();
22792296
}
22802297

2281-
const cUUID = uuidToString(characteristic.getUuid());
2282-
const sUUID = uuidToString(characteristic.getService().getUuid());
22832298
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
22842299
if (status === GATT_SUCCESS) {
2285-
const stateObject = this.connections[pUUID];
2286-
stateObject.onNotifyCallbacks = stateObject.onNotifyCallbacks || {};
2287-
const key = sUUID + '/' + cUUID;
2288-
const onNotify = args.onNotify;
2289-
stateObject.onNotifyCallbacks[key] = function (result) {
2290-
// CLog(
2291-
// CLogTypes.warning,
2292-
// `onNotifyCallback ---- UUID: ${UUID}, pUUID: ${pUUID}, cUUID: ${cUUID}, args.characteristicUUID: ${
2293-
// args.characteristicUUID
2294-
// }, sUUID: ${sUUID}, args.serviceUUID: ${args.serviceUUID}, result: ${result}`
2295-
// );
2296-
onNotify(result);
2297-
};
22982300
resolve();
22992301
clearListeners();
23002302
} else {
2303+
delete stateObject.onNotifyCallbacks[key]
23012304
onError(
23022305
new BluetoothError(BluetoothCommon.msg_error_function_call, {
23032306
arguments: args,
@@ -2306,6 +2309,8 @@ export class Bluetooth extends BluetoothCommon {
23062309
})
23072310
);
23082311
}
2312+
} else {
2313+
delete stateObject.onNotifyCallbacks[key]
23092314
}
23102315
},
23112316
}),

0 commit comments

Comments
 (0)