安卓。连接蓝牙设备
我正在尝试编写一个涉及通过蓝牙连接两个 Android 设备的应用程序。
据我了解,一个设备充当服务器,侦听传入连接,而另一台设备充当客户端,并使用服务器的 MAC 地址初始化连接。
考虑以下情况:
我的 Android 手机放在口袋里,我开始在 Android 平板电脑上工作。我想将平板电脑连接到手机(通过蓝牙),而无需将手机从口袋中取出(我不想拿出手机并手动开始侦听传入连接)。
实现这一目标的好方法是什么?看来我必须让手机成为服务器,24/7 不断监听连接。然而,我的直觉告诉我,这会严重耗尽电池电量。
有什么建议吗?
I'm attempting to write an app that involves connecting two android devices via bluetooth.
It is my understanding that one device acts as a server, listening for incoming connections while the other acts as the client, and initializes the connection using the server's MAC address.
Consider the following:
I have my android phone in my pocket, I start working on my android tablet. I want to connect the tablet to the phone (via bluetooth) without taking the phone out of my pocket (I do not want to pull out the phone and manually start listening for incoming connections).
What would be a good way to achieve this? It seems like I would have to make the phone the server, constantly listening for connections 24/7. However, my gut says this will violently drain the battery.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,你可以让你的手机(或平板电脑)成为服务器。但它不应该使用那么多电池;一旦您设置了服务器套接字并开始
接受
连接,代码就会简单地阻塞并等待传入连接。根据我的经验,我并没有发现这会消耗过多的电池(但我没有确凿的数据来支持这一点)。如果服务器涉及 UI,您需要将服务器套接字拆分为单独的线程,以免阻塞 UI 事件。
You are right, you could make your phone (or your tablet) the server. But it ought not use that much battery; once you set up a server socket and start
accept
ing connections the code simple blocks and waits for an incoming connection. In my experience I haven't found this to be too much of a battery drain (but I have no hard figures to back that up).If there is a UI involved with the server you'll want to split out the server socket into a separate thread so as to not block UI events.
通常,手机会以这种方式运行 - 即等待来自配对设备的传入连接。等待/扫描传入连接比尝试在轮询模式下查找或连接设备更省电。许多蓝牙芯片都具有低功耗扫描功能,可实现节能扫描。
Typically phones behave this way - i.e wait for incoming connections from paired devices. Waiting / Scanning for incoming connections is less power hungry than trying to find or connect to devices in a poll mode. Many bluetooth chips have low power scan which implements power efficient scanning.