简体   繁体   中英

How to persist the Bluetooth Low Energy Device connection state after passing from one activity to another in Android?

Here I have following components

Activity 1 -> has button to connect to blue tooth. Activity 2 -> Displays the data from blue tooth.

Service 1 -> It has all the blue tooth connection and data gathering operations.

So Activity 1 uses Service 1 for connection and So Activity 2 uses Service 1 for data gathering.

My Question is: Since Activity 1 is starting the service for connecting to the BLE Device.. will that connection be open still when the activity 2 is launched and open for collecting data from BLE Device?

As far as I know, Service is independent from any Activity, so you can establish Bluetooth connection or scan available BLE devices nearby in a Service. After that, you can pass data from Service to the Activity in any way you want - eg via Event Bus, BroadcastReceiver or another mechanism. Android Service should work even if Activity, which started it, is stopped. It should work even, when you leave the app and go to the home screen until application is killed, Service is stopped on demand or work is done in case of using IntentService.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM