Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
cadivus committed Dec 6, 2024
1 parent 6192adb commit 65125b1
Show file tree
Hide file tree
Showing 10 changed files with 597 additions and 101 deletions.
243 changes: 142 additions & 101 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,49 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:open_earable_flutter/open_earable_flutter.dart';

import 'widgets/frequency_player_widget.dart';
import 'widgets/jingle_player_widget.dart';
import 'widgets/rgb_led_control_widget.dart';
import 'widgets/sensor_configuration_view.dart';
import 'widgets/audio_player_control_widget.dart';
import 'widgets/sensor_view.dart';
import 'widgets/storage_path_audio_player_widget.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
final OpenEarable _openEarable = OpenEarable();
final WearableManager _wearableManager = WearableManager();
StreamSubscription? _scanSubscription;
List discoveredDevices = [];
bool _connectedToEarable = false;
bool _waitingToConnect = false;
String? _deviceIdentifier;
String? _deviceFirmwareVersion;

void _readDeviceInfo() async {
String? deviceIdentifier =
await _openEarable.bleManager.readDeviceIdentifier();
String? deviceFirmwareVersion =
await _openEarable.bleManager.readDeviceFirmwareVersion();
setState(() {
_deviceIdentifier = deviceIdentifier;
_deviceFirmwareVersion = deviceFirmwareVersion;
});
}

@override
void initState() {
super.initState();
_setupListeners();
}

void _setupListeners() async {
_openEarable.bleManager.connectionStateStream.listen((connectionState) {
if (connectionState) {
_readDeviceInfo();
_writeSensorConfig();
setState(() {
_waitingToConnect = false;
});
}
setState(() {
_connectedToEarable = connectionState;
});
});
}
// DiscoveredDevice? _connectingDevice;
Wearable? _connectedDevice;

@override
Widget build(BuildContext context) {
List<SensorView>? sensorViews;
List<SensorConfigurationView>? sensorConfigurationViews;
if (_connectedDevice != null) {
sensorViews = SensorView.createSensorViews(_connectedDevice!);
sensorConfigurationViews =
SensorConfigurationView.createSensorConfigurationViews(
_connectedDevice!,
);
}

return MaterialApp(
home: Scaffold(
appBar: AppBar(
Expand All @@ -76,76 +66,111 @@ class MyAppState extends State<MyApp> {
),
),
Visibility(
visible: discoveredDevices.isNotEmpty,
child: Container(
margin: const EdgeInsets.fromLTRB(16, 0, 16, 16),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1.0,
),
borderRadius: BorderRadius.circular(8.0),
),
child: ListView.builder(
padding: EdgeInsets.zero,
physics:
const NeverScrollableScrollPhysics(), // Disable scrolling,
shrinkWrap: true,
itemCount: discoveredDevices.length,
itemBuilder: (BuildContext context, int index) {
final device = discoveredDevices[index];
return Column(children: [
Material(
type: MaterialType.transparency,
child: ListTile(
textColor: Colors.black,
selectedTileColor: Colors.grey,
title: Text(device.name),
titleTextStyle: const TextStyle(fontSize: 16),
visualDensity: const VisualDensity(
horizontal: -4, vertical: -4),
trailing: _buildTrailingWidget(device.id),
onTap: () {
setState(() => _waitingToConnect = true);
_connectToDevice(device);
},
)),
if (index != discoveredDevices.length - 1)
const Divider(
height: 1.0,
thickness: 1.0,
color: Colors.grey,
indent: 16.0,
endIndent: 0.0,
),
]);
},
))),
Visibility(
visible: _deviceIdentifier != null && _connectedToEarable,
child: Padding(
padding: const EdgeInsets.fromLTRB(33, 8, 0, 8),
child: Text(
"Connected to $_deviceIdentifier $_deviceFirmwareVersion",
style: const TextStyle(fontSize: 16),
))),
visible: discoveredDevices.isNotEmpty,
child: Container(
margin: const EdgeInsets.fromLTRB(16, 0, 16, 16),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1.0,
),
borderRadius: BorderRadius.circular(8.0),
),
child: ListView.builder(
padding: EdgeInsets.zero,
physics: const NeverScrollableScrollPhysics(),
// Disable scrolling,
shrinkWrap: true,
itemCount: discoveredDevices.length,
itemBuilder: (BuildContext context, int index) {
final device = discoveredDevices[index];
return Column(
children: [
Material(
type: MaterialType.transparency,
child: ListTile(
textColor: Colors.black,
selectedTileColor: Colors.grey,
title: Text(device.name),
titleTextStyle: const TextStyle(fontSize: 16),
visualDensity: const VisualDensity(
horizontal: -4, vertical: -4),
trailing: _buildTrailingWidget(device.id),
onTap: () {
setState(() => _waitingToConnect = true);
_connectToDevice(device);
},
),
),
if (index != discoveredDevices.length - 1)
const Divider(
height: 1.0,
thickness: 1.0,
color: Colors.grey,
indent: 16.0,
endIndent: 0.0,
),
],
);
},
),
),
),
Center(
child: ElevatedButton(
onPressed: _startScanning,
child: const Text('Restart Scan'),
),
)
),
Visibility(
visible: true,
//_deviceIdentifier != null && _connectedToEarable,
child: Padding(
padding: const EdgeInsets.fromLTRB(33, 8, 0, 8),
child: Text(
"Connected to $_deviceIdentifier $_deviceFirmwareVersion",
style: const TextStyle(fontSize: 16),
),
),
),
if (_connectedDevice is RgbLed)
RgbLedControlWidget(rgbLed: _connectedDevice as RgbLed),
if (_connectedDevice is FrequencyPlayer)
FrequencyPlayerWidget(
frequencyPlayer: _connectedDevice as FrequencyPlayer,
),
if (_connectedDevice is JinglePlayer)
JinglePlayerWidget(
jinglePlayer: _connectedDevice as JinglePlayer,
),
if (_connectedDevice is StoragePathAudioPlayer)
StoragePathAudioPlayerWidget(
audioPlayer: _connectedDevice as StoragePathAudioPlayer,
),
if (_connectedDevice is AudioPlayerControls)
AudioPlayerControlWidget(
audioPlayerControls: _connectedDevice as AudioPlayerControls,
),
if (sensorViews != null)
Column(
children: sensorViews,
),
if (sensorConfigurationViews != null)
Column(
children: sensorConfigurationViews,
),
],
)),
),
);
}

Widget _buildTrailingWidget(String id) {
if (_openEarable.bleManager.connectedDevice?.id != id) {
/*if (_openEarable.bleManager.connectedDevice?.id != id) {
return const SizedBox.shrink();
} else if (_connectedToEarable) {
} else */
if (_connectedToEarable) {
return const Icon(size: 24, Icons.check, color: Colors.green);
} else if (_waitingToConnect) {
return const SizedBox(
Expand All @@ -157,12 +182,11 @@ class MyAppState extends State<MyApp> {
}

void _startScanning() async {
discoveredDevices.removeWhere(
(device) => device.id != _openEarable.bleManager.connectedDevice?.id);
_openEarable.bleManager.startScan();
// discoveredDevices.removeWhere(
// (device) => device.id != _openEarable.bleManager.connectedDevice?.id);
_wearableManager.startScan();
_scanSubscription?.cancel();
_scanSubscription =
_openEarable.bleManager.scanStream.listen((incomingDevice) {
_scanSubscription = _wearableManager.scanStream.listen((incomingDevice) {
if (incomingDevice.name.isNotEmpty &&
!discoveredDevices.any((device) => device.id == incomingDevice.id)) {
setState(() {
Expand All @@ -174,13 +198,30 @@ class MyAppState extends State<MyApp> {

Future<void> _connectToDevice(device) async {
_scanSubscription?.cancel();
await _openEarable.bleManager.connectToDevice(device);
}
Wearable wearable = await _wearableManager.connectToDevice(device);

Future<void> _writeSensorConfig() async {
OpenEarableSensorConfig config =
OpenEarableSensorConfig(sensorId: 3, samplingRate: 0, latency: 0);
_openEarable.sensorManager.writeSensorConfig(config);
//_openEarable.sensorManager.subscribeToSensorData(3);
setState(() {
_connectedDevice = wearable;
_connectedToEarable = true;
});

if (wearable is DeviceIdentifier) {
(wearable as DeviceIdentifier)
.readDeviceIdentifier()
.then((deviceIdentifier) {
setState(() {
_deviceIdentifier = deviceIdentifier;
});
});
}
if (wearable is DeviceFirmwareVersion) {
(wearable as DeviceFirmwareVersion)
.readDeviceFirmwareVersion()
.then((deviceFirmwareVersion) {
setState(() {
_deviceFirmwareVersion = deviceFirmwareVersion;
});
});
}
}
}
31 changes: 31 additions & 0 deletions example/lib/widgets/audio_player_control_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:open_earable_flutter/open_earable_flutter.dart';

class AudioPlayerControlWidget extends StatelessWidget {
final AudioPlayerControls audioPlayerControls;

const AudioPlayerControlWidget({Key? key, required this.audioPlayerControls})
: super(key:key);

@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: audioPlayerControls.startAudio,
child: const Text('Start'),
),
ElevatedButton(
onPressed: audioPlayerControls.pauseAudio,
child: const Text('Pause'),
),
ElevatedButton(
onPressed: audioPlayerControls.stopAudio,
child: const Text('Stop'),
),
],
);
}
}
Loading

0 comments on commit 65125b1

Please sign in to comment.