Skip to content

save BluetoothConnection variable in foreground #30

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

Closed
alishe79 opened this issue Nov 28, 2021 · 8 comments
Closed

save BluetoothConnection variable in foreground #30

alishe79 opened this issue Nov 28, 2021 · 8 comments

Comments

@alishe79
Copy link

alishe79 commented Nov 28, 2021

Hi.
Thank you for develop this plugin.
This dependency solves all my problems except for two, which I will explain below:

  1. how save BluetoothConnection class variable .
    now BluetoothConnection variable is dead when app close from task manager.
    I'm only test this on Android Devices.

  2. I'm not able to network call in background i test two dependeny. retrofit & http

I am also in an urgent position that encourages me to do this today.
Thanks in advance for taking the time to respond to this issue

@alishe79
Copy link
Author

I found the main problem.
The main problem is that receivePort crashes after closing the application from task Manager.
What is the reason for this?
How do I fix this problem?

@Dev-hwang
Copy link
Owner

It's hard to pinpoint exactly what's going on. Could you please provide some sample code or project?

For network issues, try setting allowWifiLock to true as on this page.

@Dev-hwang
Copy link
Owner

This project is part of a project I've been working on in the past. This is an example of transmitting the information of the nearest device among the discovered Bluetooth devices to the server. There is no permission handler, so manually grant location permission. It works fine on Android 10 (sdk 30).

@Dev-hwang
Copy link
Owner

Dev-hwang commented Nov 29, 2021

Even using this plugin does not prevent the destruction of the Dart code when the app is killed by the task manager. Dart code usually runs in the main isolate, and the main isolate is destroyed when the app exits. So global variables and dart classes in the main isolate are newly created every time the app starts.

This plugin takes advantage of foreground service. In the Android platform, the foreground service remains in the form of a notification even when the app is closed, helping to continue the work. So we call the FlutterForegroundTask.startService function to create a new isolate, and make that new isolate run in the foreground service. This way we can continue working on the new isolate even if the main isolate is destroyed.

The ReceivePort object returned when the FlutterForegroundTask.startService function is called is used to communicate messages between the main isolate and the new isolate. If the app is restarted, it creates a new main isolate. In order to communicate with new isolate again, FlutterForegroundTask.restartService should be called to get a new ReceivePort object.

@alishe79
Copy link
Author

Is there a specific solution to this problem here that can help me?
In general, is what I want to do possible?

@Dev-hwang
Copy link
Owner

Dev-hwang commented Nov 29, 2021

It seems that the only way to do this is to add code to disconnect the bluetooth connection to the onDestroy function of the ReadParametersTask implementation class and restart the service when the app starts.

@Dev-hwang
Copy link
Owner

How about implementing in ReadParametersTask the part that connects the bluetooth device and handles the data?

It would be better to implement widgets to simply handle UI.

@alishe79
Copy link
Author

Thanks for taking the time to answer this question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants