Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
await device.connect();
} else {
throw e;
}
}
}
await device.discoverAllServicesAndCharacteristics();
let res = retrieveInfos(device);
let characteristics;
if (!res) {
for (const uuid of getBluetoothServiceUuids()) {
try {
characteristics = await device.characteristicsForService(uuid);
res = getInfosForServiceUuid(uuid);
break;
} catch (e) {
// we attempt to connect to service
}
}
}
if (!res) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
const { deviceModel, serviceUuid, writeUuid, notifyUuid } = res;
if (!characteristics) {
}
log("ble-verbose", `open(${deviceOrId})`);
await awaitsBleOn(bleManager);
if (!device) {
// works for iOS but not Android
const devices = await bleManager.devices([deviceOrId]);
log("ble-verbose", `found ${devices.length} devices`);
[device] = devices;
}
if (!device) {
const connectedDevices = await bleManager.connectedDevices(
getBluetoothServiceUuids()
);
const connectedDevicesFiltered = connectedDevices.filter(
d => d.id === deviceOrId
);
log(
"ble-verbose",
`found ${connectedDevicesFiltered.length} connected devices`
);
[device] = connectedDevicesFiltered;
}
if (!device) {
log("ble-verbose", `connectToDevice(${deviceOrId})`);
try {
device = await bleManager.connectToDevice(deviceOrId, connectOptions);
} catch (e) {
if (!discoveredDevices[peripheral.uuid]) {
discoveredDevices[peripheral.uuid] = peripheral
observer.next({
type: 'add',
descriptor: peripheral,
device: {
id: peripheral.uuid,
name: peripheral.advertisement.localName,
},
})
}
}
}
noble.addListener('discover', detectingDevices)
noble.startScanning(getBluetoothServiceUuids(), allowDuplicates)
function unsubscribe() {
noble.removeListener('discover', detectingDevices)
noble.stopScanning()
}
return unsubscribe
}).subscribe(o)
}
const requestDeviceParam = () => ({
filters: getBluetoothServiceUuids().map(uuid => ({
services: [uuid]
}))
});
const { uuid: id } = peripheral;
const { localName } = peripheral.advertisement;
const name =
localName ||
(discoveredDevices[id] ? discoveredDevices[id].name : null);
discoveredDevices[id] = { peripheral, name };
log("ble-advertisement", id + " (" + String(name) + ")");
observer.next({
type: "add",
descriptor: peripheral,
device: { id, name }
});
};
noble.addListener("discover", onDiscover);
noble.startScanning(getBluetoothServiceUuids(), true);
return () => {
noble.removeListener("discover", onDiscover);
noble.stopScanning();
};
});
const stateSub = bleManager.onStateChange(async state => {
if (state === "PoweredOn") {
stateSub.remove();
const devices = await bleManager.connectedDevices(
getBluetoothServiceUuids()
);
if (unsubscribed) return;
await Promise.all(
devices.map(d => BluetoothTransport.disconnect(d.id).catch(() => {}))
);
if (unsubscribed) return;
bleManager.startDeviceScan(
getBluetoothServiceUuids(),
null,
(bleError, device) => {
if (bleError) {
observer.error(bleError);
unsubscribe();
return;
}
const res = retrieveInfos(device);
const deviceModel = res && res.deviceModel;
observer.next({ type: "add", descriptor: device, deviceModel });
}
);
}
}, true);
const unsubscribe = () => {
const stateSub = bleManager.onStateChange(async state => {
if (state === "PoweredOn") {
stateSub.remove();
const devices = await bleManager.connectedDevices(
getBluetoothServiceUuids()
);
if (unsubscribed) return;
await Promise.all(
devices.map(d => BluetoothTransport.disconnect(d.id).catch(() => {}))
);
if (unsubscribed) return;
bleManager.startDeviceScan(
getBluetoothServiceUuids(),
null,
(bleError, device) => {
if (bleError) {
observer.error(bleError);
unsubscribe();
return;