应用程序关闭后,Android保存BLE设备以重新连接 [英] Android save BLE device to reconnect after app close

查看:53
本文介绍了应用程序关闭后,Android保存BLE设备以重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android BLE应用程序,其中我试图在该应用程序和BLE设备之间建立稳定的连接.

I'm developing an android BLE app in which i try to establish a stable Connection between the app and a BLE device.

要实现这一点,我希望我的应用程序将BLE设备数据保留在共享的首选项中或某个位置.

To achieve this i want my app to keep the BLE device data in shared preferences or somewhere.

应用程序关闭后,我希望我的应用程序检索此数据并尝试建立连接而不扫描设备.

After app close, i'd like my app to retrive this data and attemp a connection without scan for devices.

我宁愿避免每次都进行扫描,因为扫描给我带来了很多问题并且需要时间.

I prefer to avoid scanning every time becouse scan gives me a lot of problems and it takes time.

该怎么做?有没有保存BLE数据的方法?

推荐答案

您需要在共享首选项或sqlite中存储设备的蓝牙设备地址(AB:CD:EF:01:02:03) D b.然后使用 https://developer.android. com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String),当您稍后重新启动应用程序以检索BluetoothDevice对象时.拥有BluetoothDevice对象后,您可以照常使用connectGatt方法.

You need to store the Bluetooth Device Address (AB:CD:EF:01:02:03) of the device in for example shared preferences or in a sqlite db. Then use https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String) when you later restart the app to retrieve a BluetoothDevice object. Once you have the BluetoothDevice object you can use the connectGatt method as usual.

但是,您需要记住一些未记录的内容.由于Android的BLE API中存在一些可怕的设计缺陷,因此无法告诉您指定的地址是公共地址还是随机地址. (您可以在 https://devzone.nordicsemi.com/question/43670/how-to-distinguish-between-random-and-public-gap-addresses/). getRemoteDevice方法应采用附加参数随机地址/公共地址",但不可以.没有正确的地址类型,蓝牙控制器将无法连接到设备.

There are some undocumented things you need to keep in mind however. Due to some horrible design flaws in Android's BLE API there is no way to tell it if you mean the given address is a Public Address or a Random Address. (You can read more about different address types at https://devzone.nordicsemi.com/question/43670/how-to-distinguish-between-random-and-public-gap-addresses/). The getRemoteDevice method should take an additional parameter "random address/public address" but it doesn't. Without the correct address type, the Bluetooth controller cannot connect to the device.

如果地址是公共的或随机的,则Android的BLE堆栈具有一些内部启发法来猜测",但是不幸的是,这在Android版本之间也有所不同,并且如果您使用autoConnect = true或false,也是如此.但是,如果您已绑定设备( https://developer.android .com/reference/android/bluetooth/BluetoothDevice.html#createBond()),则无论给定地址是公共地址还是随机地址,它都会存储在其内部数据库中.如果要连接的外围设备使用随机可解析的地址,并且在使用绑定时,它也可以处理该地址.因此,我强烈建议使用绑定.

Android's BLE stack has some internal heuristics to "guess" if the address is public or random, but unfortunately that differs between Android versions and also if you use autoConnect=true or false. However if you have bonded the device (https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond()) then it will store in its internal DB whether the given address is public or random. If the peripheral you want to connect to uses a random resolvable address it can handle that as well if you use bonding. Therefore I strongly suggest to use bonding.

如果您不使用绑定,则在连接设备之前需要进行扫描,因为当您开始扫描并检测到设备时,Android的BLE堆栈将暂时(直到下次重启蓝牙)记住地址类型一个地址.

If you don't use bonding you will need to scan before you connect to the device, since when you start a scan and a device is detected, Android's BLE stack will temporarily (until next restart of Bluetooth) remember the address type for an address.

这篇关于应用程序关闭后,Android保存BLE设备以重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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