Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect after Disconnect Error #50

Closed
rickdroio opened this issue Jul 31, 2019 · 7 comments
Closed

Connect after Disconnect Error #50

rickdroio opened this issue Jul 31, 2019 · 7 comments
Assignees
Labels
Type: Bug Something isn't working Type: Question Further information is requested.

Comments

@rickdroio
Copy link
Contributor

Trying to make a simple test with this lib connecting to a BT printer.

BluetoothConnection connection = await BluetoothConnection.toAddress(printerAddress);
connection.output.add(utf8.encode(text)); //actually prints the text on printer
...
connection.cancel();

on log I got the message:

D/FlutterBluePlugin(15353): Disconnected (id: 14)

But when I try to execute the same code it gives me the follow error:

I/flutter (15353): PlatformException(connect_error, read failed, socket might closed or timeout, read ret: -1, java.io.IOException: read failed, socket might closed or timeout, read ret: -1
I/flutter (15353): 	at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:774)
I/flutter (15353): 	at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:788)
I/flutter (15353): 	at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:411)
I/flutter (15353): 	at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:57)
I/flutter (15353): 	at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:64)
I/flutter (15353): 	at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin.lambda$onMethodCall$0(FlutterBluetoothSerialPlugin.java:880)
I/flutter (15353): 	at io.github.edufolly.flutterbluetoothserial.-$$Lambda$FlutterBluetoothSerialPlugin$ysyByl051NlOhDx-4TeZKtqb1pA.run(Unknown Source:10)
I/flutter (15353): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
I/flutter (15353): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)

Looks like it doesnt close the connection.
Any clue how to fix it?

Thanks

@AgainPsychoX
Copy link
Collaborator

AgainPsychoX commented Jul 31, 2019

The error is about connection, rather than canceling or disconnecting. See #18 and especially my last comment there, where I listed possible problems causing the issue.

If you are sure that all the problems listed in the comment are not your case, please feel free to ask for further help here.

BTW,

when I try to execute the same code

Does it mean, that you are immediately creating another connection just after closing the previous one? You might want to await connection.cancel(); in that case, or maybe even add some more delay.

@AgainPsychoX AgainPsychoX added Type: Bug Something isn't working Type: Question Further information is requested. labels Jul 31, 2019
@AgainPsychoX AgainPsychoX self-assigned this Jul 31, 2019
@rickdroio
Copy link
Contributor Author

rickdroio commented Aug 2, 2019

Dear @PsychoXIVI
This plugin works as expected with my BT printer during the entire object lifecycle except after close connection and try to create a new connection in another page.
FYI when I turn off the BT the plugin returns "Disconnected from remote" even after object disposal.
is there any guideline for this situation?

@rickdroio
Copy link
Contributor Author

when I try to execute the same code
Does it mean, that you are immediately creating another connection just after closing the previous one? You might want to await connection.cancel(); in that case, or maybe even add some more delay.

actually it never get out of this await connection.cancel();

@AgainPsychoX
Copy link
Collaborator

AgainPsychoX commented Aug 3, 2019

actually it never get out of this await connection.cancel();

I added button to ChatPage.dart for a test:

      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('TEST: canceling the connection');
          connection.cancel().then((_) {
            print('TEST: connection cancelled');
          });
        },
        child: Icon(Icons.cancel),
      ),

connection.cancel() seems to working fine: Clicking the button does cancel the connection and invokes second print('TEST which means, that it executes asynchronously well.

I/flutter (31148): TEST: canceling the connection
I/flutter (31148): Disconnected by remote request
I/flutter (31148): TEST: connection cancelled
D/BluetoothSocket(31148): close() this: android.bluetooth.BluetoothSocket@75f54be, mSocketState: CONNECTED
D/BluetoothSocket(31148): close() this: android.bluetooth.BluetoothSocket@75f54be, mSocketState: CLOSED
D/BluetoothSocket(31148): close() this: android.bluetooth.BluetoothSocket@75f54be, mSocketState: CLOSED
D/FlutterBluePlugin(31148): Connection onDisconnected by local
D/FlutterBluePlugin(31148): Disconnected (id: 1)

If it is not working in your case, I think you should modify cancel of BluetoothConnection to provide more debug log:

  /// Closes connection (rather immediately), in result should also disconnect.
  Future<void> cancel() async {
    print('TEST: cancel - entered');
    await output.close();
    print('TEST: cancel - output closed');
    await _readStreamController.close();
    print('TEST: cancel - read stream controller closed');
    await _readStreamSubscription.cancel();
    print('TEST: cancel - read stream subscription closed]');
  }

You can use this modification by changing your dependency to:

dependencies:
  flutter_bluetooth_serial:
    git:
      url: git://github.com/PsychoXIVI/flutter_bluetooth_serial.git
      ref: issue50_debuging

Note: that is temporary branch, after solving issue you should change it back to normal

After applying the more logging, please share the log after using the cancel method.


when I turn off the BT the plugin returns "Disconnected from remote" even after object disposal.
is there any guideline for this situation?

Well, it looks like small bug, just lack of additional check for the situation, when user disables the Bluetooth while already connection ongoing. I think it may not be related to the main problem, but for sure I will do something about it in the future.

For now you can also listen to Bluetooth adapter status changes to determine when user is turning off the adapter.


I am still looking for other possibilities, that could cause the main issue happen. Could you confirm, that you are using both input and output of the connection? Are they are working fine?

@rickdroio
Copy link
Contributor Author

rickdroio commented Aug 5, 2019

Dear @PsychoXIVI

Using the official repository I got the follow response:

I/flutter (15895): Disconnected by remote request
I/flutter (15895): TEST: connection cancelled
D/FlutterBluePlugin(15895): Disconnected (id: 3)

But actually it doesnt disconnect it. When I try to reconnect after few minutes I got the message:

I/flutter (15895): Cannot connect, exception occured
I/flutter (15895): PlatformException(connect_error, read failed, socket might closed or timeout, read ret: -1, java.io.IOException: read failed, socket might closed or timeout, read ret: -1
I/flutter (15895): 	at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:774)
I/flutter (15895): 	at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:788)
I/flutter (15895): 	at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:411)
I/flutter (15895): 	at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:57)
I/flutter (15895): 	at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:64)
I/flutter (15895): 	at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin.lambda$onMethodCall$0(FlutterBluetoothSerialPlugin.java:880)

But... Using your git branch fix the issue!!! Great work!!!
Thanks for your support!! :)

Do you have any plan to release new plugin version?

Thank you again!!

@AgainPsychoX
Copy link
Collaborator

AgainPsychoX commented Aug 5, 2019

That sound great. I think I understand what went wrong: There was #48 PR fixing some disconnection problem - and it isn't already at official package release.

I will prepare v0.2.1 ASAP, to include that important fix. I'm sorry for procrastination - I should release the patch versions more often.

@AgainPsychoX
Copy link
Collaborator

Published patch version 0.2.1. You should update your pubspec.yaml.

I am closing the issue by now. Let me know in case is there something wrong with the patch version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working Type: Question Further information is requested.
Projects
None yet
Development

No branches or pull requests

2 participants