Please do not be afraid by seeing the code only. I pasted the whole for your reference. The issue, I am trying to save data in save() async function in the ThermometerRegistration.dart. I use DeviceId from bleModels.dart.
I want to use the value for this device saved from shared preference in ThermometerRegistration.dart to blecontroller.dart.
However, the error response is failed call back from the terminal. Also, my thermometer does not connect.
However, if I am using id: XX:XX:XX:XX:XX-Real mac, my thermometer gets connected-meaning that I probably am not being able to transfer data between pages.
Can anyone assist? Just focus on the part of the code I mentioned where the problem is happening. I just pasted the whole code for your understanding and reference.
bleModels.dart
class bleModels {
String DeviceId;
bleModels({
this.DeviceId,
}
);
}
RegisterThermometer.dart
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:attendanceapp/models/bleModels.dart';
import 'package:attendanceapp/commons.dart';
import 'appData.dart';
SharedPreferences prefs;
final bleModels BleModels=bleModels();
class ThermoRegistration extends StatelessWidget {
static Future init() async {
prefs = await SharedPreferences.getInstance();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: InitiateRegistration(),
);
}
}
class InitiateRegistration extends StatelessWidget {
final TextEditingController _MacIDControllerText=TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(10),
child: TextField(
obscureText: false,
controller: _MacIDControllerText,
decoration: InputDecoration(
labelText:'Please key in the physical address of your device',
border: OutlineInputBorder(),
),
),
),
Text(
"Digital Thermometer",
style: TextStyle(
fontSize: 15,
color: Colors.blue,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 50,
),
RaisedButton(child: Text("Register your device"), onPressed: save),
RaisedButton(child: Text("Fetch Data"), onPressed: fetch),
RaisedButton(child: Text("Delete Data"), onPressed: remove)
],
),
),
),
);
}
}
save() async {
await ThermoRegistration.init();
prefs.setString('string',BleModels.DeviceId);
}
fetch() async {
String Stringval = prefs.getString('string') ?? '';
BleModels.DeviceId=Stringval;
log(" $Stringval");
}
remove() async {
prefs.remove('int');
}
bleController.dart
import 'dart:async';
import 'dart:developer';
import 'package:attendanceapp/RegistertThermometer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:attendanceapp/commons.dart';
class BleController {
final frb = FlutterReactiveBle();
String deviceID=BleModels.DeviceId;
StreamSubscription<ConnectionStateUpdate> connection;
QualifiedCharacteristic rx;
RxString status ='DEVICE NOT CONNECTED'.obs;
RxString temperature = ' '.obs;
void connect() async {
connection = frb.connectToDevice(id: '$deviceID', connectionTimeout: const Duration(seconds: 5)).listen((state) {
if (state.connectionState == DeviceConnectionState.connected) {
status.value = 'CONNECTED!';
// get rx
rx = QualifiedCharacteristic(
serviceId: Uuid.parse("0000fff0-0000-1000-8000-00805f9b34fb"),
characteristicId:
Uuid.parse("0000fff1-0000-1000-8000-00805f9b34fb"),
deviceId: '$deviceID');
// subscribe to rx
// ignore: unnecessary_statements
print(deviceID);
frb.subscribeToCharacteristic(rx).listen((data) {
print("Hello");
print(data);
print(data.elementAt(5));
print(data.elementAt(6));
print((data.elementAt(4) * 256 + data.elementAt(5)) / 10);
double final_temp =
(data.elementAt(4) * 256 + data.elementAt(5)) / 10;
String final_temp_value = final_temp.toString();
temperature.value = final_temp_value;
});
}
else{
status.value='DEVICE NOT CONNECTED';
}
});
}
void disconnect() async {
connection.cancel();
}
}
OUTPUT:
E/DartMessenger(18243): Uncaught exception in binary message listener
E/DartMessenger(18243): java.lang.IllegalStateException: Reply already submitted
E/DartMessenger(18243): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:431)
E/DartMessenger(18243): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:282)
E/DartMessenger(18243): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:296)
E/DartMessenger(18243): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:320)
E/DartMessenger(18243): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/DartMessenger(18243): at android.os.Handler.handleCallback(Handler.java:907)
E/DartMessenger(18243): at android.os.Handler.dispatchMessage(Handler.java:105)
E/DartMessenger(18243): at android.os.Looper.loop(Looper.java:216)
E/DartMessenger(18243): at android.app.ActivityThread.main(ActivityThread.java:7625)
E/DartMessenger(18243): at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger(18243): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
E/DartMessenger(18243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
E/MethodChannel#flutter_reactive_ble_method(18243): Failed to handle method call
E/MethodChannel#flutter_reactive_ble_method(18243): java.lang.IllegalArgumentException: null is not a valid Bluetooth address
E/MethodChannel#flutter_reactive_ble_method(18243): at android.bluetooth.BluetoothDevice.<init>(BluetoothDevice.java:774)
E/MethodChannel#flutter_reactive_ble_method(18243): at android.bluetooth.BluetoothAdapter.getRemoteDevice(BluetoothAdapter.java:671)
E/MethodChannel#flutter_reactive_ble_method(18243): at com.polidea.rxandroidble2.internal.util.RxBleAdapterWrapper.getRemoteDevice(RxBleAdapterWrapper.java:36)
E/MethodChannel#flutter_reactive_ble_method(18243): at com.polidea.rxandroidble2.internal.DeviceModule.provideBluetoothDevice(DeviceModule.java:39)
E/MethodChannel#flutter_reactive_ble_method(18243): at com.polidea.rxandroidble2.internal.DeviceModule_ProvideBluetoothDeviceFactory.get(DeviceModule_ProvideBluetoothDeviceFactory.java:24)
E/MethodChannel#flutter_reactive_ble_method(18243): at com.polidea.rxandroidble2.internal.DeviceModule_ProvideBluetoothDeviceFactory.get(DeviceModule_ProvideBluetoothDeviceFactory.java:10)
E/MethodChannel#flutter_reactive_ble_method(18243): at com.polidea.rxandroidble2.internal.RxBleDeviceImpl_Factory.get(RxBleDeviceImpl_Factory.java:31)