Reputation: 21
I am new to web bluetooth and I was wondering how can I read some value from one characteristic that only can notify the app of value that has changed. I am trying to get voice data from my hardware BLE device and I have been stuck for days with this. Service that I am using and its' characteristics:
Service: Base UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
1st characteristic: 6e400002-b5a3-f393-e0a9-e50e24dcca9e : Write / Write without response.
2nd characteristic: 6e400003-b5a3-f393-e0a9-e50e24dcca9e : Notify
// This is the data that we get from the badge. We use states to edit it.
const [badgedata, setBadgedata] = useState("");
const connectToDeviceAndSubscribeToUpdates = async () =>{
//Here we request a device which has the same name as the badges.
const device = await navigator.bluetooth.requestDevice({
filters: [{
name: 'HDBDG'
}],
optionalServices: ['6e400001-b5a3-f393-e0a9-e50e24dcca9e',
'00001800-0000-1000-8000-00805f9b34fb',
'00001801-0000-1000-8000-00805f9b34fb']
})
//Then we continue to connect to it via GATT.
const server = await device.gatt.connect();
console.log('Device: ' + JSON.stringify(device));
// Here we ask the server for badges' service.
const service = await server.getPrimaryService('6e400001-b5a3-f393-e0a9-e50e24dcca9e')
// And here we ask the service for its characterics --> notify -characterics in this case.
const notifyC = await service.getCharacteristic('6e400003-b5a3-f393-e0a9-e50e24dcca9e');
const writeC = await service.getCharacteristic('6e400002-b5a3-f393-e0a9-e50e24dcca9e');
console.log("Getting the characteristic");
console.log(notifyC)
//const badgeVoiceData = await notifyC.readValue();
//console.log(badgeVoiceData)
await notifyC.startNotifications();
await notifyC.addEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
console.log('Notifications have been started.');
const data = Uint8Array.of(1);
console.log(data)
await writeC.writeValue(data)
setBadgedata("something here")
const descriptor = await notifyC.getDescriptor('00002902-0000-1000-8000-00805f9b34fb');
const descValue = await descriptor.readValue();
console.log(descValue + 'something here should be');
const decoder = new TextDecoder('utf-8');
console.log(`User Description: ${decoder.decode(descValue)}`);
}
// This is a function which should convert the data types to values we could use in the front end.
//This should be called when we add an event listener for future notifications.
function handleCharacteristicValueChanged(event) {
const badgeValue = event.target.value.getUint8(0);
console.log('Received ' + badgeValue);
console.log('Hello')
// This sets badge data to the data we get from badges.
setBadgedata(badgeValue)
}
return (
<div className="App">
<header className="App-header">
<h2>
Hub
</h2>
<div className="buttonGroup">
<button className="button" onClick={connectToDeviceAndSubscribeToUpdates}>Connect</button>
<p>Badge data: {badgedata}</p>
</div>
</header>
</div>
);
}```
So I cannot get any live data from the device at the moment.
Some info about nRF [here][1]
[1]: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/include/bluetooth/services/nus.html
Upvotes: 1
Views: 526
Reputation: 5659
It looks like you're trying to communicate with this device without knowing too much about how it works. Always fun ;)
I'd suggest you give a try to the internal page about:bluetooth-internals
to get deeper in this device behavior as you'll be able to see all services/characteristics and interact with them. Check out Reverse-Engineering a Bluetooth Lightbulb from Uri Shaked as well for more tips on reverse-engineering.
Upvotes: 2