Skip to content

Commit

Permalink
List all service UUIDs for web bluetooth
Browse files Browse the repository at this point in the history
...and hide constants for lib users
  • Loading branch information
cadivus committed Dec 13, 2024
1 parent 690c9e1 commit 19caa14
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
2 changes: 0 additions & 2 deletions lib/open_earable_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
34 changes: 13 additions & 21 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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<String> allUuids = [
sensorServiceUuid,
sensorConfigurationCharacteristicUuid,
sensorDataCharacteristicUuid,
deviceInfoServiceUuid,
deviceIdentifierCharacteristicUuid,
deviceFirmwareVersionCharacteristicUuid,
deviceHardwareVersionCharacteristicUuid,
parseInfoServiceUuid,
schemeCharacteristicUuid,
sensorNamesCharacteristicUuid,
audioPlayerServiceUuid,
audioSourceCharacteristic,
audioStateCharacteristic,
batteryServiceUuid,
batteryLevelCharacteristicUuid,
buttonServiceUuid,
buttonStateCharacteristicUuid,
ledServiceUuid,
ledSetStateCharacteristic,
List<String> allServiceUuids = [
OpenEarableV1.ledServiceUuid,
OpenEarableV1.deviceInfoServiceUuid,
OpenEarableV1.audioPlayerServiceUuid,
OpenEarableV1.sensorServiceUuid,
OpenEarableV1.parseInfoServiceUuid,
OpenEarableV1.buttonServiceUuid,
OpenEarableV1.batteryServiceUuid,
CosinussOne.ppgAndAccServiceUuid,
CosinussOne.temperatureServiceUuid,
CosinussOne.heartRateServiceUuid,
];
3 changes: 2 additions & 1 deletion lib/src/managers/ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 : [],
),
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/managers/open_earable_sensor_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
27 changes: 16 additions & 11 deletions lib/src/models/devices/cosinuss_one.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ final List<int> _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<Sensor> _sensors;
final BleManager _bleManager;
final DiscoveredDevice _discoveredDevice;
Expand Down Expand Up @@ -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,
);
Expand All @@ -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) {
Expand All @@ -181,14 +185,14 @@ class _CosinussOneSensor extends Sensor {
return streamController.stream;
}

Stream<SensorValue> _createPpqStream() {
Stream<SensorValue> _createPpgStream() {
StreamController<SensorValue> 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,
);
Expand All @@ -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) {
Expand Down Expand Up @@ -228,7 +232,7 @@ class _CosinussOneSensor extends Sensor {
values: [
ppgRed.toDouble(),
ppgGreen.toDouble(),
ppgGreenAmbient.toDouble()
ppgGreenAmbient.toDouble(),
],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand All @@ -247,15 +251,16 @@ 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) {
var flag = data[0];

// 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) *
Expand All @@ -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) {
Expand Down Expand Up @@ -311,7 +316,7 @@ class _CosinussOneSensor extends Sensor {
case "ACC":
return _createAccStream();
case "PPG":
return _createPpqStream();
return _createPpgStream();
case "TEMP":
return _createTempStream();
case "HR":
Expand Down
35 changes: 21 additions & 14 deletions lib/src/models/devices/open_earable_v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ 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 =
"45622512-6468-465a-b141-0b9b0f96b468";
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";
Expand All @@ -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<Sensor> _sensors;
final List<SensorConfiguration> _sensorConfigurations;
final BleManager _bleManager;
Expand Down Expand Up @@ -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(),
);
Expand All @@ -193,7 +200,7 @@ class OpenEarableV1 extends Wearable
Future<String?> readDeviceIdentifier() async {
List<int> deviceIdentifierBytes = await _bleManager.read(
deviceId: _discoveredDevice.id,
serviceId: _deviceInfoServiceUuid,
serviceId: deviceInfoServiceUuid,
characteristicId: _deviceIdentifierCharacteristicUuid,
);
return String.fromCharCodes(deviceIdentifierBytes);
Expand All @@ -206,7 +213,7 @@ class OpenEarableV1 extends Wearable
Future<String?> readDeviceFirmwareVersion() async {
List<int> deviceGenerationBytes = await _bleManager.read(
deviceId: _discoveredDevice.id,
serviceId: _deviceInfoServiceUuid,
serviceId: deviceInfoServiceUuid,
characteristicId: _deviceFirmwareVersionCharacteristicUuid,
);
return String.fromCharCodes(deviceGenerationBytes);
Expand All @@ -219,7 +226,7 @@ class OpenEarableV1 extends Wearable
Future<String?> readDeviceHardwareVersion() async {
List<int> hardwareGenerationBytes = await _bleManager.read(
deviceId: _discoveredDevice.id,
serviceId: _deviceInfoServiceUuid,
serviceId: deviceInfoServiceUuid,
characteristicId: _deviceHardwareVersionCharacteristicUuid,
);
return String.fromCharCodes(hardwareGenerationBytes);
Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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<void> stopAudio()async {
Future<void> stopAudio() async {
Uint8List data = Uint8List(1);
data[0] = 3;
await _bleManager.write(
serviceId: _audioPlayerServiceUuid,
serviceId: audioPlayerServiceUuid,
characteristicId: _audioStateCharacteristic,
byteData: data,
);
Expand All @@ -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,
);
Expand Down

0 comments on commit 19caa14

Please sign in to comment.