防止蓝牙 LE 服务断开

Prevent disconnection of Bluetooth LE Service

我正在使用蓝牙 LE 服务制作应用程序。我注意到的是,在应用程序进入后台一段时间后,设备会自动断开连接。谁能解释一下这是为什么?

我遇到以下错误。

02-12 16:48:27.497 11695-11695/main.nesttech.com.nesttechble E/ActivityThread﹕ Activity main.nesttech.com.nesttechble.MainActivity has leaked ServiceConnection main.nesttech.com.nesttechble.MainActivity@428fc528 that was originally bound here android.app.ServiceConnectionLeaked: Activity main.nesttech.com.nesttechble.MainActivity has leaked ServiceConnection main.nesttech.com.nesttechble.MainActivity@428fc528 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:979) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:873) at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1818) at android.app.ContextImpl.bindService(ContextImpl.java:1806) at android.content.ContextWrapper.bindService(ContextWrapper.java:503) at main.nesttech.com.nesttechble.MainActivity.connectDevice(MainActivity.java:429) at main.nesttech.com.nesttechble.MainActivity.access0(MainActivity.java:93) at main.nesttech.com.nesttechble.MainActivity.run(MainActivity.java:399) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5414) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) at dalvik.system.NativeStart.main(Native Method)

您发布的日志只是丢失了 ServiceConnection,它用于通过 bindService 将您的 Activity 连接到您的 Service 对象。 当心它是 Android Service 对象而不是在外围设备中实现的 BLE 服务。

这与BLE连接无关,可能你的后台Activity只是被系统杀掉以释放内存,而你在[=之前没有在适当的生命周期回调中做unbindService 24=] 死亡。