I am writing a app in Vue.JS that will read the UV Index from a Bluetooth LE UV Detector. How it works: the user must hit a button and connect with a device called “UV Detector”, and then the device will show the device info and UV index.
For now, I am trying to get the app to show the device info, but that is where I am stuck.
I get this error: Uncaught (in promise) DOMException: GATT Error Unknown.
The code that I am writing was modified from my earlier work, where it read skin data from a skincare device. I did not get that error reading the data from the previous device. But for this particular device, it’s giving me this error, and I’m pulling my hair over how to fix this.
My code:
methods: {
getDevice: function () {
if (navigator.bluetooth) {
this.bluetoothDevice = navigator.bluetooth.requestDevice({
filters: [{
name: "UV Detector",
}],
optionalServices: [this.deviceServices.deviceInfoSvc, this.deviceServices.batteryInfoSvc] // Required to access service later.
})
.then(device => {
this.bluetoothDevice = device;
this.instruction = "Device is connected. Press the device against your skin before you hit 'Press Data'.";
device.addEventListener('gattserverdisconnected', this.onDisconnected);
return device.gatt.connect()
})
.then((server) => {
// Set the isConnected variable to true
this.bluetoothDevice.isConnected = true;
// Get the accelerometer service (if it's not enabled, the error will be caught below)
return server.getPrimaryServices();
})
.then(services => {
console.log("Getting characteristics...");
let queue = Promise.resolve();
services.forEach(service => {
console.log("Service: " + service.uuid);
switch (service.uuid) {
case ('0000180f-0000-1000-8000-00805f9b34fb'): {
queue = queue.then(_ => service.getCharacteristics().then(characteristics => {
////console.log("Service: " + service.uuid);
characteristics.forEach(characteristic => {
switch (characteristic.uuid) {
case ('00002a19-0000-1000-8000-00805f9b34fb'): {
queue = queue.then(_ => this.getBatteryPercent(characteristic)); break;
}
}
})
}));
}
case ('0000180a-0000-1000-8000-00805f9b34fb'): {
queue = queue.then(_ => service.getCharacteristics().then(characteristics => {
////console.log("Service: " + service.uuid);
characteristics.forEach(characteristic => {
console.log("Characteristic: " + characteristic.uuid);
switch (characteristic.uuid) {
case ('00002a23-0000-1000-8000-00805f9b34fb'): { queue = queue.then(_ => this.getMACAddress(characteristic)); break; }
case ('00002a24-0000-1000-8000-00805f9b34fb'): { queue = queue.then(_ => this.getModelNum(characteristic)); break; }
case ('00002a26-0000-1000-8000-00805f9b34fb'): { queue = queue.then(_ => this.getFW(characteristic)); break; }
case ('00002a27-0000-1000-8000-00805f9b34fb'): { queue = queue.then(_ => this.getHW(characteristic)); break; }
case ('00002a29-0000-1000-8000-00805f9b34fb'): { queue = queue.then(_ => this.getMfgName(characteristic)); break; }
}
})
}))
}
}
});
})
.catch(error => { console.error("ERROR"); });
}
},
//BATTERY
getBatteryPercent: function (characteristic) {
return characteristic.readValue().then(value => {
this.deviceInfo.battery= value.getUint8(0);
});
},
//DEVICE INFO
getMACAddress: function (characteristic) {
let decoder = new TextDecoder('utf-8');
return characteristic.readValue().then(value => {
this.deviceInfo.macAddress = decoder.decode(value);
});
},
getModelNum: function (characteristic) {
let decoder = new TextDecoder('utf-8');
return characteristic.readValue().then(value => {
this.deviceInfo.modelNum = decoder.decode(value);
});
},
getFW: function (characteristic) {
let decoder = new TextDecoder('utf-8');
return characteristic.readValue().then(value => {
this.deviceInfo.fw = decoder.decode(value);
});
},
getHW: function (characteristic) {
let decoder = new TextDecoder('utf-8');
return characteristic.readValue().then(value => {
this.deviceInfo.hw = decoder.decode(value);
});
},
getMfgName: function (characteristic) {
let decoder = new TextDecoder('utf-8');
return characteristic.readValue().then(value => {
this.deviceInfo.mfgName = decoder.decode(value);
});
},
Upon hitting the Connect button, I get this error: localhost/:1 Uncaught (in promise) DOMException: GATT Error Unknown.
How can I fix this?