检测所有的蓝牙设备(耳机,手机等)附近,而不强制设备发现模式 [英] Detect all Bluetooth devices (headsets, phones etc) nearby, without forcing the devices in discoverable mode

查看:782
本文介绍了检测所有的蓝牙设备(耳机,手机等)附近,而不强制设备发现模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:

要从我的Andr​​oid应用程序检测所有附近的蓝牙设备(手机,耳机等)。

To detect all nearby bluetooth devices(phones, headsets etc.) from my Android application.

下面是从 developer.android.com 其中发现蓝牙设备附近连同已经配对的设备列表。

Here's a nice example from developer.android.com which discovers the bluetooth devices nearby along with the list of already paired devices.

我的情况:

我有两个蓝牙耳机打开,并且他们没有被检测到后一个成功的蓝牙扫描!所以,我研究这个问题,并某处发现蓝牙耳机需要由Android被发现的情况下切换到配对模式。

I had two bluetooth headsets turned on and they were not being detected after a successful bluetooth scan! So I dig into the problem and somewhere found that the bluetooth headset needs to be switched to pairing mode in case of being detected by android.

要切换耳机配对模式,我不得不长时间preSS的电源按钮,同时打开它们。是的,最后,蓝牙耳机,现在由我的应用程序的扫描检测。

To switch the headsets in pairing mode, I had to long press the power button while turning them on. And yes, finally, the bluetooth headsets are now detected by the scan from my application.

我的问题:

我想我的耳机会自动无配对模式切换它们进行检测。无法找到一种方法来检测它们打开附近所有的蓝牙设备。

I want my headsets will be detected automatically without switching them in pairing mode. Couldn't find a way to detect all nearby bluetooth devices which are turned on.

推荐答案

所以,在这里就是我读关于Android的蓝牙发现博客,主题,文档,所以答案等后发现的。想这可能是张贴在这里我的发现是有用的。

So, here's all I found after reading blogs, threads, documentations, SO answers etc. regarding Android's bluetooth discovery. Thought this might be useful to post my findings here.

因此​​,有人开始读,我想澄清的是,之前,我找不到任何办法来检测附近的这是打开但无法发现的蓝牙设备。

So before someone starts reading, I want to clarify that, I could not find any way to detect a bluetooth device nearby which is turned on but not discoverable.

我的首要目标是检测所有附近的蓝牙设备。要做到这一点,我们有一个 BluetoothAdapter 类在Android中具有 startDiscovery 功能可以搜索附近的蓝牙设备。因此,这不提供我的目的,如在蓝牙设备需要可发现通过由Android蓝牙扫描来检测。我想是到附近的检测所有的蓝牙设备,而不强迫他们可发现。我开始寻找一个解决方案,实现了这个目标。

My primary target is to detect all nearby bluetooth devices. To do so, we have a BluetoothAdapter class in Android which has a startDiscovery function to scan for bluetooth devices nearby. Hence, this doesn't serves my purpose, as the bluetooth devices need to be discoverable to be detected by a bluetooth scan by Android. What I want is to detect all Bluetooth devices nearby without forcing them to be discoverable. I started searching for a solution to achieve this goal.

我开始学习蓝牙协议并发现用于蓝牙检测,连接的底层协议及配对。对于Android,我发现没有API来发现/检测哪些已打开,但没有发现模式BT的设备。

I started with studying Bluetooth protocols and found the underlying protocols used for bluetooth detection, connection and pairing. For Android, I found no API to discover/detect BT devices which are turned on but not in discoverable mode.

挖掘到这个检测问题而不强制蓝牙设备发现模式带领我们到一个地步,我们发现蓝牙装置,它标榜自己是 0级×00 自动忽略而附近的BT设备上运行扫描。这个问题在几个地方入账。在这里我列出了一些。

Digging into this detection problem without forcing the bluetooth devices to discoverable mode lead us to a point where we found Bluetooth device which advertises itself as class 0×00 is ignored automatically while running scan for nearby BT devices. This problem is stated in several places. Here I have listed some.


  1. 问题#1

  2. 谷歌组

  3. 讨论DEV

  4. 发现在HTC设备

  1. Stackoverflow question
  2. Google group
  3. Discuss DEV
  4. Found in HTC devices

我试图找到这个问题的解决方法,是的,我发现一个,尽管文档和评论与此变通说,这并不适用于所有类型的Andr​​oid设备一起工作。无论如何,隔行扫描是一样的默认的BT扫描,但它确实一些额外的作品。它读取一个成功的BT扫描后Android的日志,并检查是否有任何蓝牙设备通过跳过的操作。这是一个简单的黑客这里描述。有一个谷歌组讨论大约在同一工作围绕这里

I tried to find a work-around of this problem and yes, I found one, though the documentation and reviews along with this work-around says, it doesn't work for all kind of Android devices. Anyway, the alternate scan is the same as the default BT scan, but it does some additional works. It reads the Android log after a successful BT scan and check if there's any BT device is skipped through out the operation. This is a simple hack described here . There's a google group discussing about the same work-around here.

的变通上面并没有任何解决我的问题。蓝牙耳机需要为通过BT扫描被检测可发现的。而Android正在扫描附近的BT的设备不会被跳过。他们根本就没有找到。

The work-around above doesn't solve my problem either. Bluetooth headsets need to be discoverable for being detected by a BT scan. They are not skipped while Android is scanning nearby BT devices. They are simply not found.

不过,我又开始寻找一个解决方案,并再次发现一些有趣的事情!这是从另一<一href=\"http://stackoverflow.com/questions/15820829/detect-hidden-not-visible-bluetooth-device-on-mobile/15822738#15822738\">Stackoverflow回答它说,这是不可能知道蓝牙设备是否身边,即使他处于不可发现模式下,首先知道他的全MAC地址即可。我从他的回答再次引用以供将来参考,

However, I started searching for a solution again and found something interesting again! This is from another Stackoverflow answer which says, It is possible to know whether a bluetooth device is around, even if he is in an undiscoverable mode, by knowing his full MAC address in the first place. I'm quoting again from his answer for future reference,

该技术是尝试一个页面请求,发送所有的6个字节正在撰写的seeked蓝牙主机的MAC标识符。页面请求允许知道他的BT ADDR当一个人与一个蓝牙从站连接。该设备是不可发现模式并没有查询扫描(设备发现意向)回应,但他们做的网页扫描它是用于希望连接到另一个previously称为设备的设备作出回应。

The technique is to try a PAGE request, sending all the 6 bytes that are composing the seeked Bluetooth host MAC identifier. A PAGE request allows one to connect with a Bluetooth slave when knowing his BT ADDR. Devices that are in undiscoverable mode does not respond to inquiry scan (device discovery intent), but they do respond to page scan which is used by a device wanting to connect to an another previously known device.

我找到了希望,并开始寻找,我们如何能够发起的Andr​​oid页扫描,但太失败了这个时候。找不到任何的API来启动页扫描。从 BluetoothAdapter 类的Andr​​oid文档,我才知道,当BT扫描开始,

I found hope and started searching for, how can we initiate a page scan in Android but failed this time too. Couldn't find any kind of APIs to initiate page scan. From Android documentation of BluetoothAdapter class, I came to know that, when a BT scan starts,

这通常需要约12秒的查询扫描,然后每个新设备的页面扫描,以获取它的蓝牙名称。

This usually involves an inquiry scan of about 12 seconds, followed by a page scan of each new device to retrieve its Bluetooth name.

有是页扫描Android文档中的指示,但是,奇怪的是,没有关于页扫描任何地方任何其他文档。我把<一个href=\"http://stackoverflow.com/questions/35536940/how-to-initiate-a-page-scan-for-bluetooth-in-android\">Stackoverflow问题的查询。

There is an indication of page scan in Android documentation, but, surprisingly, there is no other documentation regarding page scan anywhere. I put a Stackoverflow question for inquiry.

没有,我没有找到任何办法这样做。我读到关于Android的蓝牙配对是如何工作的。这里是一个<一个href=\"http://stackoverflow.com/questions/27360338/how-does-bluetooth-pairing-work/27596841#27596841\">nice有详细阅读说明书沿着流程图。我们试图使用Android的隐藏API和可以使用反射使用一些隐藏的API。然后,我们开始寻找,我们可以使用服务于我的目的隐藏的API。但不幸的是,我们没有找到任何隐藏的API来在Android的已经配对列表中添加BT设备编程。

No, I did not find any way to do so. I read about how Android's bluetooth pairing works. Here is a nice flow diagram along with detailed reading manual. We were trying to use Android's hidden APIs and could use some hidden APIs using Reflection. Then we started to look for hidden APIs which we might use to serve my purpose. But, unfortunately, we failed to find any hidden APIs to add a BT device in Android's already paired list programatically.

下面是一些有用的链接检查蓝牙配对,隐藏的API,以及如何通过以供将来参考反射来调用它们。

Here are some helpful links to check for bluetooth pairing, hidden APIs and how to invoke them via reflection for future reference.


  1. 如何启动配对

  2. 如何使用反射与Android的隐藏API的工作

  3. 这里有一个谷歌的小组讨论我跟着,有的人都面临着同样问题,我们这里已经

  1. How to initiate pairing
  2. How to use reflection to work with Android's hidden APIs
  3. Here's a google group discussion I followed, some people are facing the same problem we've here

所以,我可以配对蓝牙设备时,它是发现模式,并没有在Android上的成对列表。就我而言,我用了一个蓝牙耳机。它被打开,并把发现模式。耳机的BT地址(MAC地址)是众所周知的。于是我打电话使用反射功能 createBond 从我的code,它完美地工作。该器件采用了Android的已配对列表添加成功。尽管漫游各地的解决方案,满足我的目的,我发现一些有趣的事情...

So I could pair a BT device when it was in discoverable mode and was not in the paired list in Android. In my case, I used a bluetooth headset. It was turned on and put in discoverable mode. The BT Address (MAC address) of the headset was known. So I called the function createBond from my code using reflection and it worked perfectly. The device was added in the "already-paired" list of Android successfully. While roaming around for solutions to serve my purpose I found something interesting...

我发现,Android的preserves一个$ P $,即使我从已经配对列表中删除其内存pviously配对设备。让我们从调查结果一起分享一个案例这里更好的理解。在我的情况下,耳机是pviously配对$ P $。

I found that, android preserves a previously paired device in its memory even if I remove it from the already-paired list. Let us share a case study along with the findings here for a better understanding. In my case the headset was paired previously.


  1. 不成/忘记从Android的耳机

  2. 然后转身耳机关闭。

  3. 然后附近的蓝牙设备开始扫描

  4. 当然,这款耳机并没有发现

  5. 但是,我们通过Android日志去,结果发现,Android的记忆键合状态(配对状态)的耳机的变化。

  6. 它扫描为previously保税耳机和远程报告设备关闭

下面是,与我们以前然后不成对配对的耳机的MAC地址印刷沿着日志。

Here is the log that printed along with the MAC address of the headset we paired before and then unpaired.

PREV_BOND_STATE = 11 // BOND_BONDING    (BluetoothDevice.java)
BOND_STATE = 10      // BOND_NONE
REASON = 4           // UNBOND_REASON_REMOTE_DEVICE_DOWN

话又说回来,我跑了相同的测试,但这次我们把蓝牙耳机(未​​发现)。这款耳机并没有在这种情况下发现过,但有趣的是,这一次是印不同的日志。

Then again, I ran the same test, but this time we turned the bluetooth headset on (not discoverable). The headset was not found in this case too, but the interesting thing is, this time a different log was printed.

PREV_BOND_STATE = 11 // BOND_BONDING    (BluetoothDevice.java)
BOND_STATE = 10      // BOND_NONE
REASON = 2           // UNBOND_REASON_AUTH_CANCELLED

原因被改变,所以我能理解的Andr​​oid试图与蓝牙耳机连接。耳机是不是在发现模式,但Android的能找到它,并试图与它连接,因为它是成对列表中。

The REASON is changed and hence I can understand Android tried to connect with the bluetooth headset. The headset was not in discoverable mode, yet Android could find it and tried to connect with it as it was in paired list before.

而蓝牙扫描与加入 ACTION_BOND_STATE_CHANGED 行动 intent.addAction 运行印此日志。

This log was printed while a bluetooth scan was running with adding ACTION_BOND_STATE_CHANGED action with intent.addAction.

是的,但在这种情况下,Android的需要做广告本身作为一个BLE了。这是新的Andr​​oid和不是所有的设备都支持这一功能至今。这里是设备的支持蓝牙LE广告的名单能力。但是,正如蓝牙LE广告具有高性能沿着功耗非常低,我认为这将是Android中的下一个大事件。

Yes, but in that case Android needs to advertise itself as a BLE too. This is what new in Android and not all devices support this functionality till now. Here is a list of devices which support Bluetooth LE advertisement capability. But, as Bluetooth LE advertisement has a very low power consumption along with a high performance, I think this will be the next big thing in Android.

下面是对未来的研究一些有用的链接。

Here are some helpful links for future research.

  • Can Android act as an iBeacon?
  • Android 4.4 as an iBeacon
  • Android Beacon Library
  • AltBeacon library in Github
  • Learning more about AltBeacon
  • Rooting your phone to advertise
  • Helpful library for easy Bluetooth operations, named BTWiz.
  • Bluedroid - native bluetooth functions
  • More about native APIs for bluetooth communication

这篇关于检测所有的蓝牙设备(耳机,手机等)附近,而不强制设备发现模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆