简体   繁体   English

无法将Android连接到特定的BLE设备

[英]Unable to connect Android to a specific BLE device

I'm maintaining a legacy Android app that retrieves data from several BLE peripherals. 我正在维护一个可从多个BLE外设检索数据的旧版Android应用。 Trying to add support for a new peripheral, I find the app won't connect to it. 尝试添加对新外围设备的支持时,我发现该应用程序无法连接到它。 The basic problem seems to be that when it calls .connectGatt() I immediately get an "already connected" error. 基本问题似乎是当它调用.connectGatt()我立即收到“已连接”错误。

I wrote some test code using the RxAndroidBle library. 我使用RxAndroidBle库编写了一些测试代码。 With the autoConnect flag off, the device reports DISCONNECTED , then CONNECTING , then (about 1.5 sec later) a connection error: Disconnected from <device address> . 关闭autoConnect标志后,设备将报告DISCONNECTED ,然后是CONNECTING ,然后(大约1.5秒后)出现连接错误: Disconnected from <device address> When autoConnect is on, it proceeds smoothly through DISCONNECTED , CONNECTING , and CONNECTED states and operates normally. autoConnect启用时,它将在DISCONNECTEDCONNECTINGCONNECTED状态下顺利进行并正常运行。

An earlier version of my test allowed scanning to continue during the connecting phase. 我的测试的较早版本允许扫描在连接阶段继续进行。 It still worked if autoConnect was on, but it first (falsely) reported "already connected", then a few seconds later fixed itself and connected OK. 如果autoConnect处于打开状态,它仍然可以工作,但是首先(错误地)报告“已经连接”,然后几秒钟后修复了自身并连接好。

The legacy app won't connect regardless of the setting of autoConnect . 无论autoConnect的设置如何,旧版应用程序都不会连接。 My best guess so far is that the app isn't stopping scanning soon enough after finding the device (which shouldn't matter, but as we have learned, there are plenty of flaky BLE devices out there--and as noted, the problem only occurs with this device). 到目前为止,我最好的猜测是应用程序在找到设备后没有足够快地停止扫描(这没关系,但是据我们了解,那里有很多易碎的BLE设备-而且如上所述,问题是仅在此设备上发生)。 Is it possible that simply discovering the device causes it to make some kind of half-broken connection that prevents a successful one? 是否可能只是发现设备而导致它建立某种半中断的连接,从而阻止成功的连接?

I'm on API 21 so can't try the other forms of .connectGatt() . 我使用的是API 21,因此无法尝试其他形式的.connectGatt() Nordic's mobile nRF scanner app connects successfully, but I assume that app is full of workarounds for misbehaving devices. Nordic的移动nRF扫描器应用程序成功连接,但我认为该应用程序充满了行为异常的设备的变通办法。

On Android 6.0 and later, the connectGatt method of BluetoothDevice supports a 'transport' parameter which allows forcing a BLE connection. 在Android 6.0和更高版本上,BluetoothDevice的connectGatt方法支持允许强制BLE连接的'transport'参数。 On older versions of Android, this version of the method is accessible via reflection. 在较旧的Android版本上,可以通过反射来访问此方法的该版本。 Using the TRANSPORT_LE option solves connection problems for many dual-mode devices (for me at least - it's not documented what the default TRANSPORT_AUTO option actually does - some Bluetooth stacks may prefer BLE when both modes are available) 使用TRANSPORT_LE选项可以解决许多双模设备的连接问题(至少对我来说-没有记录默认的TRANSPORT_AUTO选项的实际作用-当两种模式都可用时,某些蓝牙堆栈可能更喜欢BLE)

nRF Connect should have an option to prefer BLE connections over BR/EDR (or even default to it). nRF Connect应该有一个选项,使BLE连接优于BR / EDR(甚至默认为BLE连接)。

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

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