[英]How should I manage Bluetooth connections in Android?
Q. What are your best practices in managing bluetooth connectivity? 问: 管理蓝牙连接的最佳做法是什么?
I've read the android bluetooth guide & many bluetooth connectivity tutorials. 我已经阅读了android蓝牙指南和许多蓝牙连接教程。 Not helpful with encapsulation-design nor best practices.
对封装设计和最佳实践没有帮助。
I've never coded connectivity with external devices before. 我以前从未编译过与外部设备的连接 。 It took two weeks for me to wrap my head around the code that scans for near-by bluetooth devices and throw them into a ListView.
我用了两个星期的时间来绕过扫描附近蓝牙设备的代码并将它们扔进ListView。 Listeners, Broadcasts, and Adapters!
听众,广播和适配器!
My project will be printing 1-40 receipts every 15 minutes on a bluetooth receipt printer. 我的项目将在蓝牙收据打印机上每15分钟打印1-40张收据。 At the moment, security is not an issue.
目前,安全性不是问题。 On the same connection, it will also be receiving data (sending & receiving simultaneously does not appear to be necessary but would be useful).
在同一个连接上,它也将接收数据(同时发送和接收似乎不是必要的,但会很有用)。 I'm not yet sure how the devices are configured on this single dongle device but I would guess the devices are connected via USB controller to the dongle.
我还不确定如何在这个单个加密狗设备上配置设备,但我猜这些设备是通过USB控制器连接到加密狗的。
So far, I have 1 object to manage a single I/O connection. 到目前为止,我有一个对象来管理单个I / O连接。 Staticly I open an activity to select a connection (to later save the label , mac , and pin in the database).
静态地我打开一个活动来选择一个连接(以后在数据库中保存标签 , mac和pin )。 Based on tutorials, I have "open", "listen", "send", and "close" methods.
根据教程,我有“打开”,“收听”,“发送”和“关闭”方法。 What confuses me is "how" to use these functions.
令我困惑的是“如何”使用这些功能。 Can I leave a connection open all day (10hrs) and use it every 3mins?
我可以整天打开连接(10小时)并每隔3分钟使用一次吗? Should I open/close the connection when sending or requesting data?
我应该在发送或请求数据时打开/关闭连接吗? Where would I detect the need to reconnect?
我在哪里可以检测到需要重新连接?
sorry for the short answer, but from my practice with the Bluetooth API, I have found that this video describe the things very good (totally personal opinion...) 对不起简短的回答,但是从我使用蓝牙API的练习中,我发现这个视频描述的东西非常好(完全是个人意见......)
In addition this is useful when you do NOT have any previous experience 此外,如果您没有任何以前的经验,这将非常有用
And as last check out this question in stackoverflow it has a bunch of good references and examples!! 最后在stackoverflow中查看这个问题它有一堆很好的参考和例子!
Again sorry for the shortage, but I believe that if you check these out at least most of your questions and concerns will become answered! 再次为这次短缺感到抱歉,但我相信,如果你检查这些,至少大多数问题和疑虑都会得到解答!
:) :)
EDIT 编辑
So, let me be a bit more descriptive and share some of my experience. 所以,让我更具描述性并分享我的一些经验。
I have written an App that communicates with BLE device that has 3 functions 我编写了一个与具有3个功能的BLE设备通信的应用程序
double sided event driven button (push the button on phone -> event is fired to the device; push the button on the BLE device -> event is fired to the phone) 双面事件驱动按钮(按下电话上的按钮 - >事件被触发到设备;按下BLE设备上的按钮 - >事件被触发到手机)
send request from phone -> BLE device answers with current battery percentage 从手机发送请求 - > BLE设备以当前电池百分比回答
continuously reading strength signal (as aprox. distance) between the phone and the BLE device 持续读取手机和BLE设备之间的强度信号(为aprox。距离)
So far so good, now the things is that the basic approach is: 到目前为止一切顺利,现在事情是基本的方法是:
Search for BLE devices (bluetooth search or "discovery" of nearby bluetooth devices) 搜索BLE设备(蓝牙搜索或附近蓝牙设备的“发现”)
Choose the device you want to connect to 选择要连接的设备
After both devices connect to each other you can then start the Gatt communication. 在两个设备相互连接后,您可以启动Gatt通信。 The approach with state machine is a little too much overkill for me.
使用状态机的方法对我来说有点太过分了。 But anyway the communication is done through bytes (in my case...)
但无论如何,通信是通过字节完成的(在我的情况下......)
In one of the videos/resources there was something specific and VERY HELPFUL at least for me! 在其中一个视频/资源中,至少对我来说有一些特定的,非常有用的! To be honest I don't remember it exactly, but the idea was that before any communication it's RECOMMENDED to read/get all the options from the BLE device or something similar...
说实话,我并不完全记得它,但想法是在任何通信之前,建议阅读/获取BLE设备的所有选项或类似的东西......
Great thing will be to know your device "communication codes" or at least I call them that way. 伟大的事情将是知道你的设备“通信代码”或至少我这样称呼他们。
I really hope that this is somehow helpful! 我真的希望这在某种程度上有所帮助!
PLEASE NOTE This is strictly coming from my experience and there could be some mismatches or wrong terms, but that's how I personally see the things and because my project was long ago, I don't remember most of the things exactly. 请注意 这完全来自我的经验,可能存在一些不匹配或错误的术语,但这就是我个人看到的东西,因为我的项目很久以前,我不记得大部分事情。
This is only a summery of STUCI's provided links above. 这只是上面提供的STUCI提供的链接的总结。 He has since updated his answer and I have not updated/edited this summery.
他已经更新了他的答案, 我没有更新/编辑这个夏季。 Topics in my summery are not explanatory but provided for reference and help in generating specific questions.
我的夏季主题不是解释性的,而是提供参考和帮助产生具体问题。
Thank you Stuci! 谢谢Stuci! Some of that was helpful:- some not.
其中一些是有帮助的: - 有些不是。 I thought it best to collect my thoughts and see what has been explained and if anything hasn't.
我认为最好收集我的想法,看看有什么解释,如果没有。
(I can't post this much in a comment tho, sorry) (我不能在评论中发布这么多,抱歉)
PLEASE CALL ME ON ANYTHING THAT IS INCORRECT. 请打电话给我任何不正确的事情。
(Covers a bunch of random things) (涵盖一堆随机的东西)
While I "dont-like" videos of code:- I watched it because it was recommended ... and I am glad I did. 虽然我“不喜欢”代码视频: - 我看了它,因为它被推荐......我很高兴我做到了。 While not very helpful it did introduce some concepts I was unaware of.
虽然不是很有帮助但它确实引入了一些我不知道的概念。 Since I am targeting old android devices (v8+) the LE features are inconsequential.
由于我的目标是旧的Android设备(v8 +),因此LE功能无关紧要。
Bluetooth connections can indeed remain open; 蓝牙连接确实可以保持开放; starting at the point which the "startActivityForResult(...) method is successfully called. Two basic things affect whether or not one would want to maintain an open connection:
从成功调用“startActivityForResult(...)方法的那一点开始。两个基本的因素会影响是否要维护一个打开的连接:
A single connection can indeed "have" simultaneous Input & Output streams. 单个连接确实可以“拥有”同时的输入和输出流。 I
一世
Since it was suggested, I re-read Android's Bluetooth Guide and under "managing a connection" (talking about a single socket) I noticed this... 既然有人建议,我重新阅读Android的蓝牙指南并在“管理连接”(谈论单个插座)下我注意到了......
...but continues with noting that read & write block each other. ...但继续注意到读写会相互阻塞。 Something I still need to look further into.
我仍需要进一步研究的东西。 It seems like you cant I/O simultaneously on the same socket???
好像你不能同时在同一个插槽上进行I / O ???
I also looked into the max connection issue Stuci added and found no documentation on the Android-side. 我还查看了Stuci添加的最大连接问题,并且在Android端没有找到任何文档。 It might exist, I cant find it.
它可能存在,我找不到它。 However, most people seem to agree that there is a limitation (that could be as low as 4) imposed by whatever hardware you are coding for.
但是,大多数人似乎都同意,无论您编写什么硬件,都存在限制(可能低至4)。 Some notable links: - How many devices we can pair via Bluetooth of BLE to Android?
一些值得注意的链接: - 我们可以通过BLE蓝牙与Android配对多少台设备? - How many maximum device can we pair via Bluetooth to android device at a time?
- 我们一次可以通过蓝牙与Android设备配对多少台设备? - https://groups.google.com/forum/#!topic/android-developers/adeBD275u30
- https://groups.google.com/forum/#!topic/android-developers/adeBD275u30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.