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