Closed n1705771 closed 4 years ago
You can either use the high level api: setCharacteristicState
: https://github.com/manekinekko/angular-web-bluetooth/blob/master/projects/angular-web-bluetooth/src/lib/bluetooth.service.ts#L171
Or the low level stream api writeValue$
: https://github.com/manekinekko/angular-web-bluetooth/blob/master/projects/angular-web-bluetooth/src/lib/bluetooth.service.ts#L241
Let me know if that is what you needed, or if you need any additional feature ;)
Many thanks!
I need to read and write a string from mobile phone to peripheral BLE device. Can I use readValue$ and wirteValue$? or there are other observable for read/write string? I feel the readValue$ and writeValue$ are only for number...
In case using low level stream api, just simply replace readValue$ with writeValue$ in your sample code will works?
try {
return (
this.ble
// 1) call the discover method will trigger the discovery process (by the browser)
.discover$({
acceptAllDevices: true,
optionalServices: [BatteryLevelService.GATT_PRIMARY_SERVICE]
})
.pipe(
// 2) get that service
mergeMap((gatt: BluetoothRemoteGATTServer) => {
return this.ble.getPrimaryService$(gatt, BatteryLevelService.GATT_PRIMARY_SERVICE);
}),
// 3) get a specific characteristic on that service
mergeMap((primaryService: BluetoothRemoteGATTService) => {
return this.ble.getCharacteristic$(primaryService, BatteryLevelService.GATT_CHARACTERISTIC_BATTERY_LEVEL);
}),
// 4) ask for the value of that characteristic (will return a DataView)
mergeMap((characteristic: BluetoothRemoteGATTCharacteristic) => {
return this.ble.readValue$(characteristic);
}),
// 5) on that DataView, get the right value
map((value: DataView) => value.getUint8(0))
)
);
} catch (e) {
console.error('Oops! can not read value from %s');
}
}
Is there a sample reading/writing data to peripheral BLE device we can use as reference?
Hi @n1705771 sorry about the delay.
To answer your question, reading and writing to a device characteristic are done using the DataView interface. So whenever you need to read or write, you have to encode or decode your value (string or number...) using the DataView interface.
When it comes to how the data should be encoded or decoded, this is something that is specific to your device. You will have to read the manufacturer's spec for this device or reverse engineer it yourself.
Can you provide the code your are using to write a value?
No activity on this issue. Closing it.
Hi Manekinekko,
I want to know which API I can use to send message to peripheral ble device. In the battery level example, seem only use reading message from ble device.
Thanks