简体   繁体   English

在Android中手动配对Bluetooth Decive?

[英]Manually pairing Bluetooth Decives in Android?

I was reading this http://developer.android.com/guide/topics/wireless/bluetooth.html#QueryingPairedDevices 我正在阅读此http://developer.android.com/guide/topics/wireless/bluetooth.html#QueryingPairedDevices

which is allot of help on how to pair,connect to a bluetooth device. 这是有关如何配对,连接到蓝牙设备的帮助。

I have a situation where I have several BT devices that are in Non-Discover mode always. 我遇到的情况是,我有多个BT设备始终处于非发现模式。 I know the MAC and the PIN of these devices. 我知道这些设备的MAC和PIN。 IS there a way in Android Dev to manually add devices to the PAIRED list so i can just use the connect as a client. Android Dev中是否有一种方法可以手动将设备添加到PAIRED列表中,因此我可以将connect用作客户端。 I understand this maual is written allot for V3. 我了解这本手册是为V3分配的。 i think i will need to do this on 2.0 ; 我认为我将需要在2.0上执行此操作; 2.1- has anybody done this before? 2.1-有人做过吗?

Basically these devices I want to connect to are power saving modules I used pre built BT modules to monitor daylight, another one humidity, etc.. every 3hrs or when interrupted and runs of a single battery for months. 基本上,我要连接的这些设备是省电模块,我使用预制的BT模块监视日光,另一湿度等,每3小时一次或中断并用一个电池运行几个月。 So turning off divcory on server saves immense power and prevents other people trying to connect and waste battery. 因此,关闭服务器上的divcory可以节省大量电量,并防止其他人尝试连接并浪费电池。

Not sure what you mean by "manually": Do you mean "manually" as in GUI/user interaction, or "manually" as "I do it in my own application code"? 不确定“手动”是什么意思:在GUI /用户交互中是“手动”还是在“我自己的应用程序代码中完成”是“手动”?

Some suggestions though: 一些建议:

If you can make your BT devices discoverable at all, you could do it this way: 如果您完全可以发现BT设备,则可以通过以下方式实现:

  1. Make your BT device discoverable 使您的BT设备可被发现
  2. Let Android search for and find the device and then initiate a connection 让Android搜索并找到设备,然后启动连接
  3. Android will ask for the PIN for pairing with the device; Android会要求提供与设备配对的PIN码; enter the PIN. 输入PIN码。
  4. Once pairing was successful, Android stores the pairing information for future use, so that you can 配对成功后,Android会存储配对信息以供将来使用,以便您可以
  5. Make your BT device invisible again. 使您的BT设备再次不可见。

From then on your app should be able to connect to the BT device at any time without further pairing operations. 从那时起,您的应用程序应该可以随时连接到BT设备,而无需进行进一步的配对操作。

If the said is not an option for you, maybe you want to go another way: 如果所说的不是您的选择,也许您想换种方式:

In current Android versions there are different API routines implemented which are neither documented nor exposed in the normal SDK. 在当前的Android版本中,实现了不同的API例程,这些例程在常规SDK中都没有记录或公开。 A hack kind of solution may be to use some of these "hidden" ("@hide"...) APIs, either via reflection or via modification of your SDK installation. 骇人听闻的解决方案可能是通过反射或修改SDK安装来使用其中一些“隐藏”(“ @hide” ...)API。

But be aware that this is always a hack and it may work on a specific device with a specific version of Android and is likely to break your app on another device and/or any other Android version. 但是请注意,这始终是黑客行为 ,它可能会在具有特定版本Android的特定设备上运行,并且有可能在其他设备和/或任何其他Android版本上破坏您的应用程序。

Having said that, here comes some reference: 话虽如此,这里有一些参考:

Example of how to access "hidden" bluetooth API . 如何访问“隐藏”蓝牙API的示例

Then, have a look at the source code for android.bluetooth.BluetoothDevice, eg here . 然后,查看android.bluetooth.BluetoothDevice的源代码,例如here

In there, public boolean createBond(){...} may do what you want. 在这里, public boolean createBond(){...}可能会做您想要的。

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

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