Reputation: 109
I am facing some problems with web Bluetooth API. the problem is that I have a temperature and humidity recorder Bluetooth device, which recorded temperature and humidity data through the sensor. I want to fetch that data in my web app by using web Bluetooth in javascript. so how can I perform this task?
I have tried web Bluetooth but I only get device name and battery level but I am not able to fetch temperature & humidity data.
Upvotes: 0
Views: 853
Reputation: 5649
If the Bluetooth device uses the GATT Environmental Sensing service (GATT Assigned Number 0x181A
), you should be able to read and get notified of the Temperature (0x2A6E
) and Humidity (0x2A6F
) GATT characteristics.
Here's some code that should* work.
const serviceUuid = 0x181A;
// Requesting Bluetooth Device assuming it advertises
// the GATT Environmental Sensing Service...
const device = await navigator.bluetooth.requestDevice({
filters: [{services: [serviceUuid]}]});
// Connecting to GATT Server...
const server = await device.gatt.connect();
// Getting Environmental Sensing Service...
const service = await server.getPrimaryService(serviceUuid);
// Getting Humidity Characteristic...
const characteristic = await service.getCharacteristic(0x2A6F);
// Reading Humidity...
const value = await characteristic.readValue();
console.log(value);
The sample at https://googlechrome.github.io/samples/web-bluetooth/notifications.html?characteristic=humidity&service=environmental_sensing may also help you get started.
Upvotes: 2
Reputation: 6093
You need to know what Bluetooth GATT service provides the temperature and humidity data. Applications like "nRF Connect" and the about://bluetooth-internals page built into your browser can help you explore the services provided by the device.
Web Bluetooth requires you to pass the UUIDs of the services you want to access to the navigator.bluetooth.requestDevice()
function and so you need to figure out which GATT services you want to access before they will be visible on the BluetoothRemoteGATTServer
object provided by the API.
If you post the manufacturer and model information for the device as part of the question someone may be able to direct you to documentation from the manufacturer for the GATT services the device provides or someone who has already reverse engineered how to access the data you are looking for.
Upvotes: 1