Reputation: 93
I'm trying to communicate via chrome with a BLE device and I'm following along with this video:
https://youtu.be/XDc5HUVMI5U?t=1023
Everything works until he says to use the method:
characteristic.writeValue(new Uint8Array[ 0x00,r, g,b ]);
I looked it up and it's deprecated: https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTCharacteristic/writeValue
Instead you should use:
characteristic.writeValueWithoutResponse(new Uint8Array[ 0x00,r, g,b ]);
The problem is that this doesn't work either. It says:
script.js:29 Uncaught (in promise) TypeError: characteristic.writeValueWithoutResponse is not a function at HTMLButtonElement.setupBLE (script.js:29)
Does anyone know what I might be doing wrong? Here's all my code so far:
button.onclick = setupBLE;
let options = {
filters: [
{ services: ['19b10000-e8f2-537e-4f6c-d104768a1214'] },
{ name: 'Stepper motor control' }
]
};
async function setupBLE() {
console.log("test");
let device = await navigator.bluetooth.requestDevice(options);
let server = await device.gatt.connect();
let service = await server.getPrimaryService("19b10000-e8f2-537e-4f6c-d104768a1214");
//Same name but with 19b10001 instead of 19b10000
let characteristic = service.getCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
console.log(characteristic);
characteristic.writeValueWithoutResponse(new Uint8Array([0x00, 0x00, 0x00, 0x00]));
}```
Upvotes: 1
Views: 1388
Reputation: 5629
As service.getCharacteristic()
returns a promise, you have to use await
there. Note that writeValueWithoutResponse
also returns a promise.
const characteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
const characteristic = await service.getCharacteristic(characteristicUuid);
const value = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
await characteristic.writeValueWithoutResponse(value);
console.log("wrote value to characteristic");
You can find more documentation at https://web.dev/bluetooth/#write
Upvotes: 1