Closed Eashubh128 closed 1 year ago
onConnectionStateChange
only appears once in your logs, so it agrees with what you are saying.
There is logic in the connectionState
code to get the initialValue
, perhaps this is causing your problem. You should try adding logs and seeing if this is causing your issue.
Stream<BluetoothConnectionState> get connectionState {
BluetoothConnectionState initialValue = BluetoothConnectionState.disconnected;
if (FlutterBluePlus._connectionStates[remoteId] != null) {
initialValue = _bmToBluetoothConnectionState(FlutterBluePlus._connectionStates[remoteId]!.connectionState);
print("remoteId $remoteId initialValue $initialValue");
} else {
print("remoteId $remoteId no initialValue, assuming disconnected");
}
return FlutterBluePlus._methodStream.stream
.where((m) => m.method == "OnConnectionStateChanged")
.map((m) => m.arguments)
.map((args) => BmConnectionStateResponse.fromMap(args))
.where((p) => p.remoteId == remoteId.str)
.map((p) => _bmToBluetoothConnectionState(p.connectionState))
.newStreamWithInitialValue(initialValue);
}
If you see lots of "assuming disconnected", then you should check that the caching logic is being called and caching the value correctly.
// keep track of connection states
if (call.method == "OnConnectionStateChanged") {
BmConnectionStateResponse response = BmConnectionStateResponse.fromMap(call.arguments);
_connectionStates[DeviceIdentifier(response.remoteId)] = response;
if (response.connectionState == BmConnectionStateEnum.disconnected) {
// clear known services, must call discoverServices again
_knownServices.remove(DeviceIdentifier(response.remoteId));
}
}
I am sorry , but what do i have to do exactly ? you want me to replace this code in my flutterBluePlus library ?
yes. add those logs!
see readme for how to alter FBP code
I am getting these logs I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): connected devices [BluetoothDevice{remoteId: 30:83:98:D6:CE:1A, localName: b43, type: BluetoothDeviceType.le, isDiscoveringServices: false, services: [BluetoothService{remoteId: 30:83:98:D6:CE:1A, serviceUuid: 00001800-0000-1000-8000-00805f9b34fb, isPrimary: true, characteristics: [BluetoothCharacteristic{remoteId: 30:83:98:D6:CE:1A, serviceUuid: 00001800-0000-1000-8000-00805f9b34fb, secondaryServiceUuid: null, characteristicUuid: 00002a00-0000-1000-8000-00805f9b34fb, descriptors: [], properties: CharacteristicProperties{broadcast: false, read: true, writeWithoutResponse: false, write: false, notify: false, indicate: false, authenticatedSignedWrites: false, extendedProperties: false, notifyEncryptionRequired: false, indicateEncryptionRequired: false}, value: []}, BluetoothCharacteristic{remoteId: 30:83:98:D6:CE:1A, serviceUuid: 00001800-0000-1000-8000-00805f9b34fb, secondaryServiceUuid: null, characteristicUuid: 00002a01-0000-1000-8000-00805f9b34fb, descriptors: [], properties: CharacteristicProperties{broadc I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected I/flutter (27974): remoteId 30:83:98:D6:CE:1A no initialValue, assuming disconnected
When the connected device was printed just before that i restarted the application , and clicked the button , which checks if there are any connected devices , then if there are connected devices i connect to them and then move to the other screen where i can send an recieve data as soon as i reach that screen the stream outputs this , as the screen is wrapped with
Can you put a breakpoint here and see if it is called?
// keep track of connection states
if (call.method == "OnConnectionStateChanged") {
BmConnectionStateResponse response = BmConnectionStateResponse.fromMap(call.arguments);
_connectionStates[DeviceIdentifier(response.remoteId)] = response;
if (response.connectionState == BmConnectionStateEnum.disconnected) {
// clear known services, must call discoverServices again
_knownServices.remove(DeviceIdentifier(response.remoteId));
}
}
the question: why is initialValue not set?
Does it have to do with the flow that i have ? If you want i can explain you the flow maybe that could help you point out something
Where exactly do i add this ?
// keep track of connection states
if (call.method == "OnConnectionStateChanged") {
BmConnectionStateResponse response = BmConnectionStateResponse.fromMap(call.arguments);
_connectionStates[DeviceIdentifier(response.remoteId)] = response;
if (response.connectionState == BmConnectionStateEnum.disconnected) {
// clear known services, must call discoverServices again
_knownServices.remove(DeviceIdentifier(response.remoteId));
}
}
Perhaps do this:
// keep track of connection states
if (call.method == "OnConnectionStateChanged") {
BmConnectionStateResponse response = BmConnectionStateResponse.fromMap(call.arguments);
_connectionStates[DeviceIdentifier(response.remoteId)] = response;
print("updating _connectionStates $response");
if (response.connectionState == BmConnectionStateEnum.disconnected) {
// clear known services, must call discoverServices again
_knownServices.remove(DeviceIdentifier(response.remoteId));
}
}
But how where to add this , that i am unable to under stand
But how where to add this , that i am unable to under stand
Add the print statement here: https://github.com/boskokg/flutter_blue_plus/blob/227be87ee0d5e55f20848553f0ea4d89daca86a3/lib/src/flutter_blue_plus.dart#L267
i restarted the application
I think this might be your problem.
When you restart the application, it probably clears FlutterBluePlus. _connectionStates
?
Does the problem only happen when you "hot restart"? Does it happen when you do a full quit + relaunch?
hot restart , ps: if the problem happens in the hotrestart i assume it would also occur in the full quit + relaunch ?
So when user clicks on the my device button , if the device is connected then i directly take them to the device page , whose body is wrapped with the connection stream , but if the device is not connected then i take them to the scanning or connection page after which they go to the device page
It used to work fine , recently it broke, i assumed it to be one of my issues but turns out it had something to do with the library .
And my flutter_blue_plus.dart is
library flutter_blue_plus;
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
part 'src/bluetooth_characteristic.dart';
part 'src/bluetooth_descriptor.dart';
part 'src/bluetooth_device.dart';
part 'src/bluetooth_msgs.dart';
part 'src/bluetooth_service.dart';
part 'src/bluetooth_utils.dart';
part 'src/flutter_blue_plus.dart';
part 'src/guid.dart';
part 'src/utils.dart';
So the problem is hot restart?
simple solution: do not use hot restart
It used to work fine
yes there were some changes to fix race conditions in 1.14.8
Let me have a look to that , whether quit + close also has the issue
Yes its just hot restart , Quit + relaunch does work as intended
hot restart should be fixed in 1.14.20
I now close all connections when the dart vm is restarted
open a new issue if you have more issues.
FlutterBluePlus Version
1.14.11
Flutter Version
3.10.4
What OS?
Android
OS Version
10
What happened?
I have a scaffold body wrapped with StreamBuilder , that pops up a dialog when the device disconnects , as soon as i reach that screen the dialog appears , even if i close the dialog another one appears as the stream sends duplicate events , so i have added a flag that allows only one dialog ,
But here is the catch the dialog appears , but the device is still connected , as i can send the data to the device .
StreamBuilder<BluetoothConnectionState>( stream: deviceController.connectedDevice?.connectionState ?? const Stream.empty(), builder: (context, snapshot) { log("Stream output is ${snapshot.data} "); if (snapshot.data == BluetoothConnectionState.disconnected) { if (!isDialogup) { WidgetsBinding.instance.addPostFrameCallback( (timeStamp) { setState(() { isDialogup = true; }); AwesomeDialog( context: context, title: "Bluetooth Disconnected", dismissOnTouchOutside: false, desc: "Oops , lost bluetooth connection please try and connect again", btnOkOnPress: () { deviceController.clearConnectedDevice(); Navigator.popUntil( context, ModalRoute.withName("/"), ); }, btnOkText: "Take me to home", btnOkColor: AppColor.greenDarkColor) .show(); }, ); } }
Logs