简体   繁体   English

从Android中的一项活动转移到另一项活动后,如何保持蓝牙低功耗设备的连接状态?

[英]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. 活动1->具有用于连接蓝牙的按钮。 Activity 2 -> Displays the data from blue tooth. 活动2->显示来自蓝牙的数据。

Service 1 -> It has all the blue tooth connection and data gathering operations. 服务1->它具有所有蓝牙连接和数据收集操作。

So Activity 1 uses Service 1 for connection and So Activity 2 uses Service 1 for data gathering. 因此,活动1使用服务1进行连接,因此活动2使用服务1进行数据收集。

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? 我的问题是:由于活动1正在启动用于连接到BLE设备的服务..当活动2启动时,该连接是否仍将打开并为从BLE设备收集数据而打开?

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. 据我所知,服务独立于任何活动,因此您可以建立蓝牙连接或扫描服务附近的可用BLE设备。 After that, you can pass data from Service to the Activity in any way you want - eg via Event Bus, BroadcastReceiver or another mechanism. 之后,您可以通过任何所需的方式(例如,通过事件总线,BroadcastReceiver或其他机制)将数据从服务传递到活动。 Android Service should work even if Activity, which started it, is stopped. 即使启动该服务的活动已停止,Android服务也应能正常工作。 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. 即使您离开应用程序并转到主屏幕,直到应用程序被终止,服务按需停止或在使用IntentService的情况下完成工作,它也应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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