1

my friends. I am working on a graduation project and would like your help, please. One of the functionalities is to connect the exercise machine via Bluetooth to the app (this part is already done). When the machine is connected to the app, the user is redirected to another page, and this page should display what is shown on the machine's display, for example, "time," "distance," "speed," etc., and the values of these data should be updated in real-time. However, even with the machine connected via Bluetooth to the app, the exercise values do not appear on the phone.

I also used the nRF Connect app to connect to the machine and obtain the UUID of the characteristics since I don't have the documentation for the machine. However, even with the information obtained from this app, I can't get the data collection and display to work. Here's a section on the characteristics of the machine:

Fitness Machine
UUID:0x1826
PRIMARY SERVICE
Fitness Machine Feature
UUID: 0x2ACC
Properties: READ
Value: Fitness Machine Features:
Average Speed Supported
Cadence Supported
Total Distance Supported
Inclination Supported
Elevation Gain Supported
Pace Supported
Step Count Supported
Resistance Level Supported
Stride Count Supported
Expended Energy Supported
Heart Rate Measurement Supported
Metabolic Equivalent Supported
Elapsed Time Supported
Remaining Time Supported
Power Measurement Supported
Force on Belt and Power Output Supported
Reserved for Future Use: 0x00800000
Target Setting Features:
Speed Target Setting Supported
Inclination Target Setting Supported
Resistance Target Setting Supported
Power Target Setting Supported
Heart Target Setting Supported
Targeted Expended Energy Configuration Supported
Targeted Step Number Configuration Supported
Targeted Stride Number Configuration Supported
Targeted Distance Configuration Supported
Targeted Training Time Configuration Supported
Targeted Time in Two Heart Rate Zones Configuration Supported
Targeted Time in Three Heart Rate Zones Configuration Supported
Targeted Time in Five Heart Rate Zones Configuration Supported
Indoor Bike Simulation Parameters Supported
Wheel Circumference Configuartion Supported
Spin Down Control Supported 
Reserved for Future Use:0x00800000

This is the code to connect the machine with the app (it works):

`import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:eco/devicepage.dart';
class RemoPage extends StatefulWidget {
@override
_RemoPageState createState() => _RemoPageState();
}
class _RemoPageState extends State<RemoPage> {
final FlutterBlue flutterBlue = FlutterBlue.instance;
List<BluetoothDevice> devicesList = [];
bool isSearching = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Remo'),
centerTitle: true, 
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
child: Icon(Icons.bluetooth_searching, size: 100),
onTap: () async {
setState(() {
isSearching = true;
});
await searchBluetoothDevices();
setState(() {
isSearching = false;
});
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
'Dispositivos encontrados:',
style: TextStyle(
fontSize: 20,
color: Colors.black,
),
),
),
SizedBox(height: 20),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: devicesList.length,
itemBuilder: (BuildContext context, int index) {
BluetoothDevice device = devicesList[index];
return ListTile(
title: Text(device.name ?? 'Unknown Device'),
subtitle: Text(device.id.toString()),
);
},
),
),
Padding(
padding: EdgeInsets.only(bottom: 20),
),
],
);
},
);
},
onTapDown: (TapDownDetails details) async {
if (devicesList.isNotEmpty) {
int index = details.localPosition.dy ~/ 70;
if (index < devicesList.length) {
BluetoothDevice device = devicesList[index];
await Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
DevicePage(device: device),
));
}
}
},
),
SizedBox(height: 20),
isSearching ? CircularProgressIndicator() : Container(),
],
),
),
);
}
Future<void> searchBluetoothDevices() async {
devicesList.clear();
flutterBlue.startScan(timeout: Duration(seconds: 4));
flutterBlue.scanResults.listen((results) {
for (ScanResult result in results) {
BluetoothDevice device = result.device;
if (device.name.startsWith("DFIT")) {
devicesList.add(device);
}
}
});
await flutterBlue.stopScan();
}
}`

This is the code to display and update in real-time the values of each exercise data that the user is performing on the machine (it does not work):

`import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
class DevicePage extends StatefulWidget {
final BluetoothDevice device;
DevicePage({required this.device});
@override
_DevicePageState createState() => _DevicePageState();
}
class _DevicePageState extends State<DevicePage> {
late StreamSubscription<List<int>> _subscription;
String _distance = '';
String _time = '';
String _spm = '';
String _calories = '';
@override
void initState() {
super.initState();
_connectToDevice();
}
@override
void dispose() {
super.dispose();
_subscription?.cancel();
}
void _connectToDevice() async {
await widget.device.connect();
List<BluetoothService> services = await widget.device.discoverServices();
services.forEach((service) {
if (service.uuid.toString().toUpperCase() == '00001826-0000-1000-8000-00805f9b34fb') {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString().toUpperCase() == '00002a28-0000-1000-8000-00805f9b34fb') {
_subscribeToCharacteristic(characteristic);
}
});
}
});
}
void _subscribeToCharacteristic(BluetoothCharacteristic characteristic) {
_subscription = characteristic.value.listen((value) {
List<int> data = value.sublist(1);
double distance = _parseDistance(data);
int time = _parseTime(data);
int spm = _parseSPM(data);
int calories = _parseCalories(data);
setState(() {
_distance = '$distance m';
_time = '$time s';
_spm = '$spm spm';
_calories = '$calories kcal';
});
print("Distance: $_distance");
print("Time: $_time");
print("SPM: $_spm");
print("Calories: $_calories");
});
characteristic.setNotifyValue(true);
}
double _parseDistance(List<int> data) {
int value = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0];
return value / 10.0;
}
int _parseTime(List<int> data) {
return data[5] << 8 | data[4];
}
int _parseSPM(List<int> data) {
return data[6];
}
int _parseCalories(List<int> data) {
return data[11] << 8 | data[10];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Data'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Distance: $_distance'),
SizedBox(height: 20),
Text('Time: $_time'),
SizedBox(height: 20),
Text('SPM: $_spm'),
SizedBox(height: 20),
Text('Calories: $_calories'),
],
),
),
);
}
}`
Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23
  • What exactly does not work? Your code is quite hard to read without proper formatting. Could you [edit] your code to add some indentation? – Michael Kotzjan May 09 '23 at 05:57
  • @MichaelKotzjan Hello, I copied the code from Visual Studio Code and pasted it into the post, the copied code was already properly formatted, but an error message appeared when I tried to submit my question on the site, and then I had to place the code as is, because only then the error disappeared. The second code should collect the values of "String _distance = ''; String _time = ''; String _spm = ''; String _calories = ''; " that appear on the machine's display, and then these values should be displayed in the app and updated in real-time, but the string values do not appear. – Gustavo Fonsêca May 09 '23 at 10:05
  • But does connecting and subscribing to the characteristic work? – Michael Kotzjan May 09 '23 at 10:58
  • @MichaelKotzjan Yes, the machine connects via Bluetooth to the app, and the subscribing also works (I can see this on the debug console), but as I mentioned earlier, the values that appear on the exercise machine display are not shown on the phone screen. – Gustavo Fonsêca May 09 '23 at 11:12
  • Have you tried adding the values to the debug console? Do the notifications work? – Michael Kotzjan May 09 '23 at 12:05
  • @MichaelKotzjan I have already done that too and nothing happens. I even created a button with a functionality to send the values to be stored in Firestore, but the values didn't even show up there – Gustavo Fonsêca May 09 '23 at 13:04

0 Answers0