From 19caa149ceb896cb78c97a7a7b7c8bff094ed494 Mon Sep 17 00:00:00 2001 From: Jonas Greifenhain Date: Fri, 13 Dec 2024 23:40:30 +0100 Subject: [PATCH] List all service UUIDs for web bluetooth ...and hide constants for lib users --- lib/open_earable_flutter.dart | 2 -- lib/src/constants.dart | 34 +++++++----------- lib/src/managers/ble_manager.dart | 3 +- .../managers/open_earable_sensor_manager.dart | 1 + lib/src/models/devices/cosinuss_one.dart | 27 ++++++++------ lib/src/models/devices/open_earable_v1.dart | 35 +++++++++++-------- 6 files changed, 53 insertions(+), 49 deletions(-) diff --git a/lib/open_earable_flutter.dart b/lib/open_earable_flutter.dart index 84a6eeb..891e03e 100644 --- a/lib/open_earable_flutter.dart +++ b/lib/open_earable_flutter.dart @@ -26,8 +26,6 @@ export 'src/models/capabilities/jingle_player.dart'; export 'src/models/capabilities/audio_player_controls.dart'; export 'src/models/capabilities/storage_path_audio_player.dart'; -part 'src/constants.dart'; - class WearableManager { static final WearableManager _instance = WearableManager._internal(); diff --git a/lib/src/constants.dart b/lib/src/constants.dart index 99ba65e..f422d88 100644 --- a/lib/src/constants.dart +++ b/lib/src/constants.dart @@ -1,4 +1,5 @@ -part of open_earable_flutter; +import 'models/devices/cosinuss_one.dart'; +import 'models/devices/open_earable_v1.dart'; const String sensorServiceUuid = "34c2e3bb-34aa-11eb-adc1-0242ac120002"; const String sensorConfigurationCharacteristicUuid = @@ -34,24 +35,15 @@ const String ledServiceUuid = "81040a2e-4819-11ee-be56-0242ac120002"; const String ledSetStateCharacteristic = "81040e7a-4819-11ee-be56-0242ac120002"; // All UUIDs in a list for filters -List allUuids = [ - sensorServiceUuid, - sensorConfigurationCharacteristicUuid, - sensorDataCharacteristicUuid, - deviceInfoServiceUuid, - deviceIdentifierCharacteristicUuid, - deviceFirmwareVersionCharacteristicUuid, - deviceHardwareVersionCharacteristicUuid, - parseInfoServiceUuid, - schemeCharacteristicUuid, - sensorNamesCharacteristicUuid, - audioPlayerServiceUuid, - audioSourceCharacteristic, - audioStateCharacteristic, - batteryServiceUuid, - batteryLevelCharacteristicUuid, - buttonServiceUuid, - buttonStateCharacteristicUuid, - ledServiceUuid, - ledSetStateCharacteristic, +List allServiceUuids = [ + OpenEarableV1.ledServiceUuid, + OpenEarableV1.deviceInfoServiceUuid, + OpenEarableV1.audioPlayerServiceUuid, + OpenEarableV1.sensorServiceUuid, + OpenEarableV1.parseInfoServiceUuid, + OpenEarableV1.buttonServiceUuid, + OpenEarableV1.batteryServiceUuid, + CosinussOne.ppgAndAccServiceUuid, + CosinussOne.temperatureServiceUuid, + CosinussOne.heartRateServiceUuid, ]; diff --git a/lib/src/managers/ble_manager.dart b/lib/src/managers/ble_manager.dart index 890ca08..1c563d2 100644 --- a/lib/src/managers/ble_manager.dart +++ b/lib/src/managers/ble_manager.dart @@ -6,6 +6,7 @@ import 'package:permission_handler/permission_handler.dart'; import 'package:universal_ble/universal_ble.dart'; import '../../open_earable_flutter.dart'; +import '../constants.dart'; /// A class that establishes and manages Bluetooth Low Energy (BLE) /// communication with OpenEarable devices. @@ -131,7 +132,7 @@ class BleManager { await UniversalBle.startScan( scanFilter: ScanFilter( // Needs to be passed for web, can be empty for the rest - withServices: kIsWeb ? allUuids : [], + withServices: kIsWeb ? allServiceUuids : [], ), ); } diff --git a/lib/src/managers/open_earable_sensor_manager.dart b/lib/src/managers/open_earable_sensor_manager.dart index 72ca7dd..be7b894 100644 --- a/lib/src/managers/open_earable_sensor_manager.dart +++ b/lib/src/managers/open_earable_sensor_manager.dart @@ -4,6 +4,7 @@ import 'dart:math'; import 'package:flutter/foundation.dart'; import '../../open_earable_flutter.dart'; +import '../constants.dart'; import '../utils/mahony_ahrs.dart'; import 'ble_manager.dart'; diff --git a/lib/src/models/devices/cosinuss_one.dart b/lib/src/models/devices/cosinuss_one.dart index 2e15bd0..4294dc9 100644 --- a/lib/src/models/devices/cosinuss_one.dart +++ b/lib/src/models/devices/cosinuss_one.dart @@ -24,10 +24,14 @@ final List _sensorBluetoothCharacteristics = [ 0x30, 0x32, 0x34, - 0x35 + 0x35, ]; class CosinussOne extends Wearable implements SensorManager { + static const ppgAndAccServiceUuid = "0000a000-1212-efde-1523-785feabcd123"; + static const temperatureServiceUuid = "00001809-0000-1000-8000-00805f9b34fb"; + static const heartRateServiceUuid = "0000180d-0000-1000-8000-00805f9b34fb"; + final List _sensors; final BleManager _bleManager; final DiscoveredDevice _discoveredDevice; @@ -150,7 +154,7 @@ class _CosinussOneSensor extends Sensor { _bleManager.write( deviceId: _discoveredDevice.id, - serviceId: "0000a000-1212-efde-1523-785feabcd123", + serviceId: CosinussOne.ppgAndAccServiceUuid, characteristicId: "0000a001-1212-efde-1523-785feabcd123", byteData: _sensorBluetoothCharacteristics, ); @@ -159,7 +163,7 @@ class _CosinussOneSensor extends Sensor { _dataSubscription = _bleManager .subscribe( deviceId: _discoveredDevice.id, - serviceId: "0000a000-1212-efde-1523-785feabcd123", + serviceId: CosinussOne.ppgAndAccServiceUuid, characteristicId: "0000a001-1212-efde-1523-785feabcd123", ) .listen((data) { @@ -181,14 +185,14 @@ class _CosinussOneSensor extends Sensor { return streamController.stream; } - Stream _createPpqStream() { + Stream _createPpgStream() { StreamController streamController = StreamController(); int startTime = DateTime.now().millisecondsSinceEpoch; _bleManager.write( deviceId: _discoveredDevice.id, - serviceId: "0000a000-1212-efde-1523-785feabcd123", + serviceId: CosinussOne.ppgAndAccServiceUuid, characteristicId: "0000a001-1212-efde-1523-785feabcd123", byteData: _sensorBluetoothCharacteristics, ); @@ -197,7 +201,7 @@ class _CosinussOneSensor extends Sensor { _dataSubscription = _bleManager .subscribe( deviceId: _discoveredDevice.id, - serviceId: "0000a000-1212-efde-1523-785feabcd123", + serviceId: CosinussOne.ppgAndAccServiceUuid, characteristicId: "0000a001-1212-efde-1523-785feabcd123", ) .listen((data) { @@ -228,7 +232,7 @@ class _CosinussOneSensor extends Sensor { values: [ ppgRed.toDouble(), ppgGreen.toDouble(), - ppgGreenAmbient.toDouble() + ppgGreenAmbient.toDouble(), ], timestamp: DateTime.now().millisecondsSinceEpoch - startTime, ), @@ -247,7 +251,7 @@ class _CosinussOneSensor extends Sensor { _dataSubscription = _bleManager .subscribe( deviceId: _discoveredDevice.id, - serviceId: "00001809-0000-1000-8000-00805f9b34fb", + serviceId: CosinussOne.temperatureServiceUuid, characteristicId: "00002a1c-0000-1000-8000-00805f9b34fb", ) .listen((data) { @@ -255,7 +259,8 @@ class _CosinussOneSensor extends Sensor { // based on GATT standard double temperature = _twosComplimentOfNegativeMantissa( - ((data[3] << 16) | (data[2] << 8) | data[1]) & 16777215) / + ((data[3] << 16) | (data[2] << 8) | data[1]) & 16777215, + ) / 100.0; if ((flag & 1) != 0) { temperature = ((98.6 * temperature) - 32.0) * @@ -282,7 +287,7 @@ class _CosinussOneSensor extends Sensor { _dataSubscription = _bleManager .subscribe( deviceId: _discoveredDevice.id, - serviceId: "0000180d-0000-1000-8000-00805f9b34fb", + serviceId: CosinussOne.heartRateServiceUuid, characteristicId: "00002a37-0000-1000-8000-00805f9b34fb", ) .listen((data) { @@ -311,7 +316,7 @@ class _CosinussOneSensor extends Sensor { case "ACC": return _createAccStream(); case "PPG": - return _createPpqStream(); + return _createPpgStream(); case "TEMP": return _createTempStream(); case "HR": diff --git a/lib/src/models/devices/open_earable_v1.dart b/lib/src/models/devices/open_earable_v1.dart index 724ca01..50b95ff 100644 --- a/lib/src/models/devices/open_earable_v1.dart +++ b/lib/src/models/devices/open_earable_v1.dart @@ -20,11 +20,9 @@ import '../capabilities/storage_path_audio_player.dart'; import 'discovered_device.dart'; import 'wearable.dart'; -const String _ledServiceUuid = "81040a2e-4819-11ee-be56-0242ac120002"; const String _ledSetStateCharacteristic = "81040e7a-4819-11ee-be56-0242ac120002"; -const String _deviceInfoServiceUuid = "45622510-6468-465a-b141-0b9b0f96b468"; const String _deviceIdentifierCharacteristicUuid = "45622511-6468-465a-b141-0b9b0f96b468"; const String _deviceFirmwareVersionCharacteristicUuid = @@ -32,7 +30,6 @@ const String _deviceFirmwareVersionCharacteristicUuid = const String _deviceHardwareVersionCharacteristicUuid = "45622513-6468-465a-b141-0b9b0f96b468"; -const String _audioPlayerServiceUuid = "5669146e-476d-11ee-be56-0242ac120002"; const String _audioSourceCharacteristic = "566916a8-476d-11ee-be56-0242ac120002"; const String _audioStateCharacteristic = "566916a9-476d-11ee-be56-0242ac120002"; @@ -49,6 +46,16 @@ class OpenEarableV1 extends Wearable JinglePlayer, AudioPlayerControls, StoragePathAudioPlayer { + static const String ledServiceUuid = "81040a2e-4819-11ee-be56-0242ac120002"; + static const String deviceInfoServiceUuid = + "45622510-6468-465a-b141-0b9b0f96b468"; + static const String audioPlayerServiceUuid = + "5669146e-476d-11ee-be56-0242ac120002"; + static const String sensorServiceUuid = "34c2e3bb-34aa-11eb-adc1-0242ac120002"; + static const String parseInfoServiceUuid = "caa25cb7-7e1b-44f2-adc9-e8c06c9ced43"; + static const String buttonServiceUuid = "29c10bdc-4773-11ee-be56-0242ac120002"; + static const String batteryServiceUuid = "180F"; + final List _sensors; final List _sensorConfigurations; final BleManager _bleManager; @@ -180,7 +187,7 @@ class OpenEarableV1 extends Wearable data.setUint8(2, b); await _bleManager.write( deviceId: _discoveredDevice.id, - serviceId: _ledServiceUuid, + serviceId: ledServiceUuid, characteristicId: _ledSetStateCharacteristic, byteData: data.buffer.asUint8List(), ); @@ -193,7 +200,7 @@ class OpenEarableV1 extends Wearable Future readDeviceIdentifier() async { List deviceIdentifierBytes = await _bleManager.read( deviceId: _discoveredDevice.id, - serviceId: _deviceInfoServiceUuid, + serviceId: deviceInfoServiceUuid, characteristicId: _deviceIdentifierCharacteristicUuid, ); return String.fromCharCodes(deviceIdentifierBytes); @@ -206,7 +213,7 @@ class OpenEarableV1 extends Wearable Future readDeviceFirmwareVersion() async { List deviceGenerationBytes = await _bleManager.read( deviceId: _discoveredDevice.id, - serviceId: _deviceInfoServiceUuid, + serviceId: deviceInfoServiceUuid, characteristicId: _deviceFirmwareVersionCharacteristicUuid, ); return String.fromCharCodes(deviceGenerationBytes); @@ -219,7 +226,7 @@ class OpenEarableV1 extends Wearable Future readDeviceHardwareVersion() async { List hardwareGenerationBytes = await _bleManager.read( deviceId: _discoveredDevice.id, - serviceId: _deviceInfoServiceUuid, + serviceId: deviceInfoServiceUuid, characteristicId: _deviceHardwareVersionCharacteristicUuid, ); return String.fromCharCodes(hardwareGenerationBytes); @@ -269,7 +276,7 @@ class OpenEarableV1 extends Wearable data.setAll(6, loudnessBytes.buffer.asUint8List()); await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioSourceCharacteristic, byteData: data, ); @@ -294,7 +301,7 @@ class OpenEarableV1 extends Wearable data[0] = type; data[1] = jingleMap[jingle.key]!; await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioSourceCharacteristic, byteData: data, ); @@ -308,7 +315,7 @@ class OpenEarableV1 extends Wearable Uint8List data = Uint8List(1); data[0] = 1; await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioStateCharacteristic, byteData: data, ); @@ -319,18 +326,18 @@ class OpenEarableV1 extends Wearable Uint8List data = Uint8List(1); data[0] = 2; await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioStateCharacteristic, byteData: data, ); } @override - Future stopAudio()async { + Future stopAudio() async { Uint8List data = Uint8List(1); data[0] = 3; await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioStateCharacteristic, byteData: data, ); @@ -347,7 +354,7 @@ class OpenEarableV1 extends Wearable data.setRange(2, 2 + nameBytes.length, nameBytes); await _bleManager.write( - serviceId: _audioPlayerServiceUuid, + serviceId: audioPlayerServiceUuid, characteristicId: _audioSourceCharacteristic, byteData: data, );