Android 蓝牙 &唤醒锁关系

发布于 2024-11-29 01:29:13 字数 254 浏览 2 评论 0原文

我正在开发一个在后台运行并支持蓝牙配件的 Android 应用程序。我想不断地在后台监听蓝牙设备,以尝试打开手机的套接字。我的问题是,是否可以在不持续保持部分唤醒锁的情况下实现这一目标,因为这显然会产生严重的电池后果。所以我想知道蓝牙对手机入睡有何影响。当蓝牙插座打开时,手机是否保持唤醒状态?如果设备尝试连接,蓝牙芯片是否会自动唤醒 Android?我尝试做一些测试来回答这些问题,但是很难隔离唤醒锁所发生的情况;此外,我不知道我是否可以依赖我观察到的行为,或者它在其他设备上是否会发生变化。

I'm working on an Android application that runs in the background and enables support for a Bluetooth accessory. I would like to be constantly listening in the background for the Bluetooth device to try and open a socket to the phone. My question is whether it is possible to achieve this without constantly keeping a partial wakelock, since that would obviously have severe battery consequences. So what I'm wondering is what effect Bluetooth has on the phone falling asleep. Does the phone stay awake when there is an open Bluetooth socket? Does the Bluetooth chip wake up Android automatically if a device tries to connect? I've attempted to do some testing to answer these questions, but it's difficult to isolate what is happening with wake locks; in addition, I don't know if I can rely on the behavior I observe or if it subject to change on other devices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画离情绘悲伤 2024-12-06 01:29:13

是的,如果您的应用程序正在运行并且线程位于 serverSocket.accept() 方法中,传入连接会唤醒手机,因此无需使用唤醒锁。但是,请确保您的服务位于前台并且没有被系统终止。

Yes, if your application is running and a thread is in serverSocket.accept() method, incoming connection wake up phone, so there is no need to use wakelocks. However, make sure that your service is in foreground and is not killed by system.

请别遗忘我 2024-12-06 01:29:13

如果您正在为基于棉花糖或以上的设备开发它,则可以使用 DOZE 模式来处理此类情况。那么你就不用担心这些事情了。它可以通过适当的机制处理WAKE_LOCK。

If you are developing it for devices target to marshmallow based or above, there is DOZE mode to treat such conditions. You then need not to worry about these thing. It can handle the WAKE_LOCK with appropriate mechanism.

墨洒年华 2024-12-06 01:29:13

如果存在打开的蓝牙套接字,手机不会保持唤醒状态;如果设备尝试连接,蓝牙芯片也不会唤醒 Android。通常有一个后台线程正在运行以接受开放端口上的连接,一旦设备尝试连接,该线程就会读取连接,从传入设备获取某种身份验证(我假设有一个安全协议来接受任何新的传入连接),并且一旦传入连接经过身份验证,就会创建/通知一个独立的线程来处理与该线程的后续信息交换。

因此,后台进程会消耗一些电量和电池电量,并且它还负责保持 Android 部分唤醒(部分作为后台进程,您始终可以控制它检查传入连接的频率)。通常这个后台进程并不总是运行,它仅在 Android 中打开蓝牙时运行。因此,您还可以创建一个线程,仅当 Android 中的蓝牙打开时才运行,否则它应该休眠。

The phone does not stay awake if there is an open Bluetooth socket, and neither does the Bluetooth chip wake up Android if a device tries to connect. Usually there is a background thread running to accept connections on the open port and as soon as a device tries to connect, it is this thread which reads the connection, gets some sort of authentication from the incoming device(I am assuming that there is a security protocol in place to accept any new incoming connections) and once the incoming connection is authenticated, an independent thread is created/notified to handle subsequent information exchange with this thread.

So the background process would consume some power and battery drain and it is also responsible for keeping Android partially awake(partially as its a background process and you can always control how frequently it checks for incoming connections). Usually this background process is not run always, its run only when Bluetooth is turned on in Android. So you can also create a thread which should run only when Bluetooth is switched on in Android, else it should sleep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文