Skip to content

Commit

Permalink
Merge pull request #26 from Gloveland/feature/calibration
Browse files Browse the repository at this point in the history
Feature/calibration
  • Loading branch information
jazminsofiaf authored Sep 29, 2021
2 parents 7fabab4 + 4c79a12 commit fa8800e
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 278 deletions.
19 changes: 19 additions & 0 deletions lib/connection/ble/bluetooth_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,30 @@ class BluetoothBackend {
connectedDevices, BluetoothSpecification.START_INTERPRETATIONS);
}

static void sendCalibrationCommand(BluetoothDevice device) async {
sendCommandToConnectedDevice(device, BluetoothSpecification.CALIBRATE);
}

static void sendStopCommand(List<BluetoothDevice> connectedDevices) async {
sendCommandToConnectedDevices(
connectedDevices, BluetoothSpecification.STOP_ONGOING_TASK);
}

/// Sends the command specified as a parameter to the connected device
/// through the control characteristic.
static void sendCommandToConnectedDevice(BluetoothDevice connectedDevice, String command) async {
BluetoothService? service = await getLsaGlovesService(connectedDevice);
if (service != null) {
BluetoothCharacteristic characteristic = getControllerCharacteristic(service);
try {
await characteristic.write(utf8.encode(command), withoutResponse: true);
} catch (err) {
developer.log("Characteristic write failed: " + err.toString(),
name: TAG);
}
}
}

/// Sends the commands specified as a parameter to the connected devices
/// through the control characteristic.
///
Expand Down
163 changes: 58 additions & 105 deletions lib/connection/ble/bluetooth_device.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:lsa_gloves/connection/ble/bluetooth_backend.dart';
import 'package:lsa_gloves/connection/ble/bluetooth_specification.dart';
import 'package:lsa_gloves/connection/ble/bluetooth_services.dart';
import 'dart:developer' as developer;

class DeviceScreen extends StatefulWidget {
Expand All @@ -18,120 +18,73 @@ class _DeviceScreenState extends State<DeviceScreen> {

BluetoothDevice device;

List<Widget> _buildServiceTiles(
String deviceId, List<BluetoothService> services) {
return services
.where((bleService) =>
bleService.uuid.toString().toLowerCase() ==
BluetoothSpecification.LSA_GLOVE_SERVICE_UUID.toLowerCase())
.map(
(bleService) => ServiceTile(
service: bleService,
deviceId: deviceId,
characteristics: bleService.characteristics,
),
)
.toList();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(device.name),
actions: <Widget>[
appBar: AppBar(title: Text(device.name)),
body: ListView(
children: <Widget>[
StreamBuilder<BluetoothDeviceState>(
stream: device.state,
initialData: BluetoothDeviceState.connecting,
builder: (c, snapshot) {
String text;
switch (snapshot.data) {
case BluetoothDeviceState.connected:
text = 'CONNECTED';
break;
case BluetoothDeviceState.disconnected:
text = 'DISCONNECTED';
break;
default:
text = snapshot.data.toString().substring(21).toUpperCase();
break;
}
return Center(
child: Text(
text,
textAlign: TextAlign.left,
style: Theme.of(context)
.primaryTextTheme
.button
?.copyWith(color: Colors.white),
),
);
},
)
],
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
StreamBuilder<BluetoothDeviceState>(
stream: device.state,
initialData: BluetoothDeviceState.connecting,
builder: (c, snapshot) => ListTile(
leading: (snapshot.data == BluetoothDeviceState.connected)
? Icon(Icons.bluetooth_connected)
: Icon(Icons.bluetooth_disabled),
title: Text(
'Device is ${snapshot.data.toString().split('.')[1]}.'),
subtitle: Text('${device.id}'),
trailing: StreamBuilder<List<BluetoothService>>(
stream: Stream.periodic(Duration(seconds: 2))
.asyncMap((_) => device.discoverServices()),
initialData: [],
builder: (context, servicesSnapshot) => IndexedStack(
index: servicesSnapshot.data!.isEmpty ? 1 : 0,
children: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: () => device.discoverServices(),
),
IconButton(
icon: SizedBox(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.red),
),
width: 18.0,
height: 18.0,
),
onPressed: null,
)
],
),
),
initialData: BluetoothDeviceState.disconnected,
builder: (c, snapshot) => SwitchListTile(
secondary: Container(
height: double.infinity,
child: Icon(snapshot.data == BluetoothDeviceState.connected
? Icons.bluetooth_connected
: Icons.bluetooth),
),
title: Text('${device.name.toString()}'),
subtitle: Text('${device.id}'),
onChanged: (btConnectionStatus) {
if (btConnectionStatus) {
setState(() {
device.connect().then((_) =>
device.requestMtu(BluetoothSpecification.MTU_BYTES_SIZE));
});
} else {
device.disconnect();
}
},
value: snapshot.data == BluetoothDeviceState.connected
? true
: false,
),
StreamBuilder<int>(
stream: device.mtu,
initialData: 0,
builder: (c, snapshot) => ListTile(
title: Text('MTU Size'),
subtitle: Text('${snapshot.data} bytes'),
),
ListTile(title: Text("ID: ${device.id}")),
StreamBuilder<int>(
stream: device.mtu,
initialData: 0,
builder: (c, snapshot) {
print("Mtu updated");
return ListTile(
title: Text('MTU Size: ${snapshot.data} bytes'),
trailing: IconButton(
icon: Icon(Icons.edit),
onPressed: () => device.requestMtu(512),
onPressed: () => device.requestMtu(BluetoothSpecification.MTU_BYTES_SIZE),
),
),
),
StreamBuilder<List<BluetoothService>>(
stream: device.services,
initialData: [],
builder: (c, snapshot) {
return Column(
children: _buildServiceTiles('${device.id}', snapshot.data!),
);
},
),
],
),
);
},
),
ListTile(
title: Text("Calibración"),
trailing: IconButton(
icon: Icon(Icons.settings_backup_restore),
onPressed: () {
BluetoothBackend.sendCalibrationCommand(device);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Row(
children: [
Icon(Icons.lightbulb, color: Colors.blue),
SizedBox(width: 20),
Expanded(
child: Text(
"Calibrando... espere a que se apague el led azul del dispositivo."))
],
)));
},
)),
],
),
);
}
Expand Down
59 changes: 0 additions & 59 deletions lib/connection/ble/bluetooth_services.dart

This file was deleted.

9 changes: 8 additions & 1 deletion lib/connection/ble/bluetooth_specification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class BluetoothSpecification {
/// interpretations task on the device if one of them is being run
static const String STOP_ONGOING_TASK = "stop";

/// Calibrate command.
///
/// This command will stop any running task and start the calibration process
/// in the glove. The built in led of the glove will be turn on while the
/// calibration process is still running.
static const String CALIBRATE = "calibrate";

/// MTU to request
static int mtu = 512;
static const int MTU_BYTES_SIZE = 512;
}
Loading

0 comments on commit fa8800e

Please sign in to comment.