Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
characteristics = await device.characteristicsForService(serviceUuid);
}
const retrieveService = async device => {
if (!device.gatt) throw new Error("bluetooth gatt not found");
const [service] = await device.gatt.getPrimaryServices();
if (!service) throw new Error("bluetooth service not found");
const infos = getInfosForServiceUuid(service.uuid);
if (!infos) throw new Error("bluetooth service infos not found");
return [service, infos];
};
const retrieveInfos = device => {
if (!device || !device.serviceUUIDs) return;
const [serviceUUID] = device.serviceUUIDs;
if (!serviceUUID) return;
const infos = getInfosForServiceUuid(serviceUUID);
if (!infos) return;
return infos;
};
const retrieveService = async device => {
if (!device.gatt) throw new Error('bluetooth gatt not found')
const [service] = await device.gatt.getPrimaryServices()
if (!service) throw new Error('bluetooth service not found')
const infos = getInfosForServiceUuid(service.uuid)
if (!infos) throw new Error('bluetooth service infos not found')
return [service, infos]
}
export const retrieveServiceAndCharacteristics = async (device: *) => {
const [service] = await discoverDeviceServices(device);
const infos = getInfosForServiceUuid(service.uuid);
if (!infos) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
const characteristics = await discoverServiceCharacteristics(service);
let writeC;
let notifyC;
for (const c of characteristics) {
if (c.uuid === infos.writeUuid.replace(/-/g, "")) {
writeC = c;
} else if (c.uuid === infos.notifyUuid.replace(/-/g, "")) {
notifyC = c;
}
}
if (!writeC || !notifyC) {
throw new TransportError(
"missing characteristics",