Hi
I am trying to connect with a Ble device using Webluetooth api
Example:
//my device object
var myDevice;
//HD Steth Device Characteristic details
// fill in a service you're looking for here - VSP Service
var myService = '569a1101-b87f-490c-92cb-11ba5ea5167c';
// fill in a characteristic from the service here - TX FIFO Characteristic
var writeCharacteristic = '569a2000-b87f-490c-92cb-11ba5ea5167c';
//- RX FIFO Characteristic
var readCharacteristic = '569a2001-b87f-490c-92cb-11ba5ea5167c';
//- Modem In
var modemInCharacterstic = '569a2003-b87f-490c-92cb-11ba5ea5167c';
//- Modem Out
var modemOutCharacterstic = '569a2002-b87f-490c-92cb-11ba5ea5167c';
//Basic Bluetooth connection steps
function connect() {
navigator.bluetooth.requestDevice({
filters: [{ services: [myService] }],
//filters: [myFilters] // you can't use filters and acceptAllDevices together
optionalServices: [myService]
//acceptAllDevices: true
})
.then(function (device) {
// save the device returned so you can disconnect later:
//alert("Inside connect, before request device");
if (appLaunchFlag == 1) {
appLaunchFlag = 1;
alert('Device ' +
device.name + ' Restoring Bluetooth Connectivity...');
}
// Set up event listener for when device gets disconnected.
device.addEventListener('gattserverdisconnected', onDisconnected);
myDevice = device;
console.log(device);
// alert("Inside device");
// connect to the device once you find it:
return device.gatt.connect();
})
.then(function (server) {
// get the primary service:
//Invoke the timer function show() & start()
show();
reset();
start();
$('#rec_btns').show();
console.log("Inside the server function");
return server.getPrimaryService(myService);
})
.then(function (service) {
// get the characteristic:
console.log("Inside the service function");
//$("#connect").addClass('disabled');
//update the values in html file
$("#connect").removeClass("btn off");
$('#connect').addClass("btn on");
$("#connect").attr("onclick", "disconnect()");
$("#data_name").html('');
$("#byteval").html('');
$('ul').show();
//myCharacteristic
return service.getCharacteristics(writeCharacteristic);
})
.then(function (characteristics) {
// subscribe to the characteristic:
for (c in characteristics) {
console.log(characteristics[c]);
characteristics[c].startNotifications()
.then(subscribeToChanges);
}
})
.catch(function (error) {
// catch any errors:
console.log('Connection failed!', error.messsage);
$("#connect").removeClass('disabled');
//console.error('Connection failed!', error);
});
}
// subscribe to changes from the HD Steth device
function subscribeToChanges(characteristic) {
console.log("Inside the subscribeToChanges");
console.log(characteristic);
characteristic.oncharacteristicvaluechanged = handleData;
console.log("End of the subscribeToChanges");
console.log("Invoke onRenderGraphLoad method to plot live signals - PCG, ECG");
//test here to get sample ECG, HS and MUR data for max time interval
//recordCountdown();
}
This piece of code works fine in chrome browser on Windows 10 Professional edtion with Ble.
Whereas the same code is not working in (Windows 10 - 2004 / Chrome 88).
Pairing works without a issue, but I cannot connect after shows connection failed.
The error message is
"Connection failed! DOMException: GATT Server is disconnected. Cannot retrieve services. (Re)connect first with `device.gatt.connect`."
It fails while calling getPrimaryService() not resolving appropriately
This issue occurs only in Windows 10 Home edition
I tried enabling these two flags also but not resolved
- chrome://flags/#enable-experimental-web-platform-features 🚩 flag.
- The Persistent Device Permissions feature is behind the chrome://flags/#enable-web-bluetooth-new-permissions-backend
Please help how to resolve this.
Regards