Dart wrapper via dart:js
for https://webbluetoothcg.github.io/web-bluetooth/
- canUseBlue
- getAvailability
- subscribeAvailabilitychanged/unsubscribeAvailabilitychanged
- getDevices/requestDevice
- connect/disconnect
- getPrimaryService/getCharacteristic
- startNotifications/stopNotifications
- subscribeValueChanged/unsubscribeValueChanged
- readValue/writeValueWithResponse
bool canUse = canUseBlue();
print('canUse $canUse');
https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getAvailability
bool availability = await blue.getAvailability();
print('availability $availability');
https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/onavailabilitychanged
final EventListener _handleAvailabilitychanged = allowInterop((Event event) {}
...
blue.subscribeAvailabilitychanged(_handleAvailabilitychanged);
...
blue.unsubscribeAvailabilitychanged(_handleAvailabilitychanged);
List<BlueDevice> getDevices = await blue.getDevices();
_device = getDevices[0];
BlueDevice requestDevice = await blue.requestDevice(RequestOptions(
optionalServices: [BlueUUID.getService(WOODEMI_SERV__COMMAND)],
acceptAllDevices: true,
));
_device = requestDevice;
_device?.gatt.connect().then((value) {
print('device.gatt.connect success');
}).catchError((error) {
print('device.gatt.connect $error');
});
_device?.gatt.disconnect();
_service = await _device!.gatt.getPrimaryService(BlueUUID.getService(WOODEMI_SERV__COMMAND));
_characteristic = await _service!.getCharacteristic(BlueUUID.getCharacteristic(WOODEMI_CHAR__COMMAND_REQUEST));
await _characteristic!.startNotifications();
- https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTCharacteristic/stopNotifications
await _characteristic!.stopNotifications();
final EventListener _handleValueChanged = allowInterop((event) {}
...
_characteristic!.subscribeValueChanged(_handleValueChanged);
...
_characteristic!.unsubscribeValueChanged(_handleValueChanged);
var byteData = await _characteristic!.readValue();
_characteristic!.writeValueWithResponse(Uint8List.fromList([0x01, 0x0A, 0x00, 0x00, 0x00, 0x01]));
Status in Chromium: https://chromestatus.com/feature/5264933985976320