在范围内时自动连接到配对的蓝牙设备 [英] Automatically connect to paired bluetooth device when in range

查看:199
本文介绍了在范围内时自动连接到配对的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

我正在尝试让我的android服务自动连接到已经 已配对的蓝牙设备.

I'm trying to get my android service to auto-connect to an already paired bluetooth device when its in range.

示例:

我将我的iPhone与我的汽车配对.当我坐下汽车音乐时 开始自动播放.我不需要再次连接.一世 不需要开始唱歌.它只是启动iTunes并开始播放 音乐.我想要在我的Android应用程序中实现这种连接.

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.

一个明显的解决方案是让一个线程在后台服务中运行,该线程每隔X秒尝试连接一次.但是,由于电池寿命长短,这并不是最佳选择.

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.

我尝试设置广播接收器以在ACTION_ACL_CONNECTED上触发,但这似乎只有在连接到设备后才被调用.

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.

当手机进入设备范围时,我的应用程序是否无法获得通知?

Is there no way my app can get a notification when the phone walks into range of the device?

在过去的两天中,我花了大部分时间试图使它正常运行.有十亿篇关于如何连接bt设备的教程,但没有一部在范围内时自动连接的教程.开始怀疑这是否可能..如果不行,我会被吓倒了:(

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 :(

相关问题:

自动查找已配对的蓝牙设备,当它们在范围内时

推荐答案

据我所知,一旦处于范围之内,就无法自动连接到通用蓝牙设备.最好的选择是有一个查询循环,定期查询可用的设备并连接到与之绑定的设备(尽管正如您所说,这会消耗大量电池).

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).

但是,如果您的设备(电话和外围设备)都支持Bluetooth 4.0(LE)和GATT,则可以使用

However, if your device (both the phone and the peripheral) support Bluetooth 4.0 (LE) as well as GATT, you can use connectGatt method 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).

我不确定设备非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).

如果您找到解决方案或调查此方法在非LE/非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.

这篇关于在范围内时自动连接到配对的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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