godice_universal_ble
GoDiceTransport and scanner for godice
built on universal_ble. Works on
Android, iOS, macOS, Windows, Linux and Web.
Setup
Add both packages:
dependencies:
godice: ^0.1.0
godice_universal_ble: ^0.1.0
Then do the usual universal_ble platform setup:
- Android:
BLUETOOTH_SCANandBLUETOOTH_CONNECTin the manifest (plusACCESS_FINE_LOCATIONfor API 30 and below). - iOS / macOS:
NSBluetoothAlwaysUsageDescriptioninInfo.plist; on macOS also thecom.apple.security.device.bluetoothentitlement.
The example/ app has all of these in place.
Usage
import 'package:godice/godice.dart';
import 'package:godice_universal_ble/godice_universal_ble.dart';
// Let the scanner start/stop the BLE scan for us.
final scanner = GoDiceScanner(manageScan: true);
final device = await scanner.find(null); // first GoDice seen
final die = GoDiceUniversalBle.fromDevice(device!, dieType: DieType.d6);
await die.connect();
die.rolls.listen((roll) => print('Rolled ${roll.value}'));
print('Battery ${await die.getBatteryLevel()}%');
await die.setLeds(die.color!.rgb);
Everything after connect() is package:godice; see its README for the
full API (positions, battery, colour, LEDs, die types).
Sharing universal_ble with your own code
This package is built to coexist with an app that already drives
universal_ble:
- It only uses the broadcast streams (
scanStream,connectionStream,characteristicValueStream), never the single-owneronScanResult/onValueChangecallbacks, so it cannot steal events from you. GoDiceScannerdoes not start or stop scanning unless you passmanageScan: true. By default it just filtersUniversalBle.scanStreamforGoDice_names while your app controls the scan.UniversalBleTransport(deviceId, ownsConnection: false)attaches to a connection your app manages: it checks the link is up, subscribes to the notify characteristic only if nobody has yet, and never disconnects or unsubscribes on dispose.
// App already connected the device itself:
final die = GoDiceUniversalBle.fromDeviceId(id, ownsConnection: false);
await die.connect(); // just attaches
If your app has its own BLE layer, you can also skip this package and
implement GoDiceTransport from package:godice directly in about 50 lines:
write to characteristic 6e400002-…, forward characteristicValueStream of
6e400003-… into notifications, and forward connectionStream into
connectionState.
Libraries
- godice_universal_ble
- GoDice over
package:universal_ble.