Android 在范围内时自动连接到配对的蓝牙设备
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26569156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically connect to paired bluetooth device when in range
提问by Mahir
Goal:
目标:
I'm trying to get my android service to auto-connect to an already paired bluetooth device when its in range.
我试图让我的 android 服务在它在范围内时自动连接到已经配对的蓝牙设备。
Example:
例子:
I have my iphone paired to my car. When I sit down in my car music starts automatically playing. I don't need to connect it again. I don't need to start a song. It just launches itunes and starts playing music. I want this kind of connectivity in my Android application.
我已将我的 iPhone 与我的汽车配对。当我在车上坐下时,音乐开始自动播放。我不需要再次连接它。我不需要开始一首歌。它只是启动 iTunes 并开始播放音乐。我希望在我的 Android 应用程序中实现这种连接。
One obvious solution is to have a thread running in a background services that tries to connect every X seconds. This however is not optimal due to the toll on battery life.
一个明显的解决方案是在后台服务中运行一个线程,尝试每 X 秒连接一次。然而,由于对电池寿命的影响,这不是最佳的。
I tried setting up broadcast receivers to get triggered on ACTION_ACL_CONNECTED
but this seems to only get called after I connect to the device.
我尝试设置广播接收器以触发,ACTION_ACL_CONNECTED
但这似乎只有在我连接到设备后才会被调用。
Is there no way my app can get a notification when the phone walks into range of the device?
当手机进入设备范围时,我的应用程序是否无法收到通知?
I've spent the better part of the last two days trying to get this to work with no luck. There are a billion tutorials on how to connect to a bt device but none to auto connect when in range. Starting to question if this even possible.. I'll be blown away if its not :(
在过去两天的大部分时间里,我都在试图让它在没有运气的情况下正常工作。有十亿个教程关于如何连接到 bt 设备,但没有一个在范围内自动连接。开始质疑这是否可能......如果不是,我会被吹走:(
related questions:
相关问题:
Find already paired bluetooth devices automatically, when they are in range
采纳答案by Bart Platak
As far as I know there is no way to automatically connect to a generic Bluetooth device as soon as it's in range. Your best bet is to have an inquiry loop, periodically querying available devices and connecting to ones you are bonded with (although, as you said, this is fairly battery intensive).
据我所知,一旦它在范围内,就无法自动连接到通用蓝牙设备。你最好的办法是有一个查询循环,定期查询可用设备并连接到你绑定的设备(尽管,正如你所说,这是相当耗电的)。
However, if your device (both the phone and the peripheral) support Bluetooth 4.0 (LE) as well as GATT, you can use connectGattmethod that will automatically connect as soon as the device becomes available (again, not too sure about battery implications although limited testing I have done thus far shows that it is not significant).
但是,如果您的设备(手机和外围设备)支持蓝牙 4.0 (LE) 和 GATT,您可以使用connectGatt方法,该方法将在设备可用时自动连接(同样,虽然不太确定电池的影响,但到目前为止,我所做的有限测试表明它并不重要)。
I'm not entirely certain what the behaviour of connectGatt
is when device is non-GATT but it may be worth investigating - my guess would be that you'd receive a callback with status GATT_FAILURE
when device becomes in range but doesn't support GATT (this would at least give you an indication of when to connect).
我不完全确定connectGatt
当设备是非 GATT 时的行为是什么,但可能值得调查 - 我的猜测是GATT_FAILURE
当设备进入范围但不支持 GATT 时,您会收到带有状态的回调(这至少会告诉您何时连接)。
If you find a solution or investigate the behaviour of this method on non-LE / non-GATT devices please do update your question as I'd be quite curious to find out more about it.
如果您找到解决方案或调查此方法在非 LE / 非 GATT 设备上的行为,请更新您的问题,因为我很想了解更多信息。
回答by user149408
As mentioned before, there's no easy way to do that with Bluetooth Classic other than by constantly scanning.
如前所述,除了不断扫描之外,没有其他简单的方法可以使用 Bluetooth Classic 做到这一点。
The car radio scenario you mentioned, however, works with some car radios—mine automatically establishes a Bluetooth connection with my phone as soon as I turn the radio on. I have observed similar behavior with Bluetooth keyboards.
然而,你提到的汽车收音机场景适用于一些汽车收音机——我的收音机一打开就会自动与我的手机建立蓝牙连接。我观察到蓝牙键盘的类似行为。
I can only speculate that these peripherals periodically try to connect to paired devices until it is sucessful. This makes sense for a peripheral in certain conditions:
我只能推测这些外围设备会定期尝试连接到配对的设备,直到成功为止。这对于某些条件下的外围设备是有意义的:
- Draining the battery isn't a concern. (The radio runs off a car battery, and most of the time it is on, the engine is running and the battery gets charged.)
- The peripheral talks to only one phone at a time, and can thus suspend its scans while it is connected to one. (This particular radio can only handle one phone at a time.)
- The peripheral is mostly useless when not connected to another device, and can thus be expected to be connected to something for most of the time it is powered on. (This goes for headsets, keyboards, mice and the like.)
- 耗尽电池不是问题。(收音机由汽车电池供电,大部分时间它都打开,发动机在运转,电池充电。)
- 外围设备一次仅与一部手机通话,因此可以在连接到一部手机时暂停其扫描。(这个特殊的收音机一次只能处理一部电话。)
- 外围设备在未连接到另一个设备时通常是无用的,因此可以预期在它通电的大部分时间都连接到某些东西。(这适用于耳机、键盘、鼠标等。)
Obviously, that depends on your ability to influence the behavior of the paired device. If it's an off-the-shelf device, it either works or it doesn't—but if you're designing a custom peripheral, it's worth considering.
显然,这取决于您影响配对设备行为的能力。如果它是现成的设备,它要么有效,要么无效——但如果您正在设计定制外设,则值得考虑。