Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bleManager.startDeviceScan(null, null, (bleError, device) => {
if (bleError) {
observer.error(bleError);
unsubscribe();
return;
}
// FIXME this is not the final filtering. we should eventually use the serviceUUIDs
if (device.name === "Blue" || device.id === "3A:11:99:A9:08:C4") {
observer.next({ type: "add", descriptor: device });
}
});
if (sub) sub.remove();
} else if (state === "Unsupported") {
unsubscribe();
observer.error(
new TransportError(
"Bluetooth BLE is not supported",
"BLENotSupported"
)
);
}
};
const sub = bleManager.onStateChange(onBleStateChange, true);
subscription = characteristic.monitor((error, c) => {
if (error) return reject(error);
try {
const value = Buffer.from(c.value, "base64");
if (debug) {
console.log("<=", value.toString("hex"));
}
const tag = value.readUInt8(0);
const index = value.readUInt16BE(1);
let data = value.slice(3);
if (tag !== TagId) {
throw new TransportError(
"Invalid tag " + tag.toString(16),
"InvalidTag"
);
}
if (notifiedIndex !== index) {
throw new TransportError(
"BLE: Invalid sequence number. discontinued chunk. Received " +
index +
" but expected " +
notifiedIndex,
"InvalidSequence"
);
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
const value = Buffer.from(c.value, "base64");
if (debug) {
console.log("<=", value.toString("hex"));
}
const tag = value.readUInt8(0);
const index = value.readUInt16BE(1);
let data = value.slice(3);
if (tag !== TagId) {
throw new TransportError(
"Invalid tag " + tag.toString(16),
"InvalidTag"
);
}
if (notifiedIndex !== index) {
throw new TransportError(
"BLE: Invalid sequence number. discontinued chunk. Received " +
index +
" but expected " +
notifiedIndex,
"InvalidSequence"
);
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
}
notifiedIndex++;
notifiedData = Buffer.concat([notifiedData, data]);
if (notifiedData.length > notifiedDataLength) {
throw new TransportError(
"BLE: received too much data. discontinued chunk. Received " +
const characteristics = await device.characteristicsForService(ServiceUuid);
if (!characteristics) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
let writeC;
let notifyC;
for (const c of characteristics) {
if (c.uuid === WriteCharacteristicUuid) {
writeC = c;
} else if (c.uuid === NotifyCharacteristicUuid) {
notifyC = c;
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid",
);
}
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid",
);
}
if (!notifyC.isNotifiable) {
throw new TransportError(
"notify characteristic not notifiable",
"BLEChracteristicInvalid",
);
}
logSubject.next({ type: "verbose", message: `device.mtu=${device.mtu}` });
const notifyObservable = monitorCharacteristic(notifyC).pipe(
tap(value => {
logSubject.next({
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid"
);
}
if (!notifyC.isNotifiable) {
throw new TransportError(
"notify characteristic not notifiable",
"BLEChracteristicInvalid"
);
}
return new BluetoothTransport(device, writeC, notifyC);
}
const subscription = characteristic.monitor((error, c) => {
if (error) {
logSubject.next({
type: "verbose",
message: "error monitor " + characteristic.uuid + ": " + error,
});
o.error(error);
} else if (!c) {
o.error(
new TransportError(
"characteristic monitor null value",
"CharacteristicMonitorNull",
),
);
} else {
try {
const value = Buffer.from(c.value, "base64");
o.next(value);
} catch (error) {
o.error(error);
}
}
});
next: value => {
const tag = value.readUInt8(0);
const index = value.readUInt16BE(1);
let data = value.slice(3);
if (tag !== TagId) {
o.error(
new TransportError("Invalid tag " + tag.toString(16), "InvalidTag"),
);
return;
}
if (notifiedIndex !== index) {
o.error(
new TransportError(
"BLE: Invalid sequence number. discontinued chunk. Received " +
index +
" but expected " +
notifiedIndex,
"InvalidSequence",
),
);
return;
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
}
notifiedIndex++;
notifiedData = Buffer.concat([notifiedData, data]);
let notifyC;
for (const c of characteristics) {
if (c.uuid === WriteCharacteristicUuid) {
writeC = c;
} else if (c.uuid === NotifyCharacteristicUuid) {
notifyC = c;
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound",
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid",
);
}
if (!notifyC.isNotifiable) {
throw new TransportError(
"notify characteristic not notifiable",
"BLEChracteristicInvalid",
);
}
}
*/
const characteristics = await device.characteristicsForService(ServiceUuid);
if (!characteristics) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
let writeC, notifyC;
for (const c of characteristics) {
if (c.uuid === WriteCharacteristicUuid) {
writeC = c;
} else if (c.uuid === NotifyCharacteristicUuid) {
notifyC = c;
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid"
);
}