android – 如何在每个打开同一个蓝牙低能量设备时重新连接我的应用程序?

android – 如何在每个打开同一个蓝牙低能量设备时重新连接我的应用程序?,第1张

概述我正在开发一个 Android应用程序.一开始,这个应用程序向用户显示一个蓝牙设备列表,当他选择一个打开另一个活动时. 我的目标是:一天之后,当用户重新打开应用程序时,应用程序将不得不记住有缺陷的设备,并且必须尝试重新连接它. 现在我以这种方式获得了我的目标: >我最好记住所选设备的mac地址 >当用户重新打开应用程序时,应用程序执行隐藏的扫描,并尝试重新连接到具有相同MAC地址的设备. >为了检 我正在开发一个 Android应用程序.一开始,这个应用程序向用户显示一个蓝牙设备列表,当他选择一个打开另一个活动时.

我的目标是:一天之后,当用户重新打开应用程序时,应用程序将不得不记住有缺陷的设备,并且必须尝试重新连接它.

现在我以这种方式获得了我的目标:

>我最好记住所选设备的mac地址
>当用户重新打开应用程序时,应用程序执行隐藏的扫描,并尝试重新连接到具有相同MAC地址的设备.
>为了检索mac地址我使用函数getAddress()(当我尝试连接到这个功能给我的androID外设
一些问题,因为设备每个更改他的MAC地址
开始:/)

有一个最好的方式来获得同样的事情吗?

关闭主题:ScanScult的功能onScanResult有一段时间返回给我的设备,是不是正常?
我已经使用了startLeScan(UUID [] serviceUuIDs,BluetoothAdapter.LeScanCallback回调),而在棒棒糖之后,我使用了新版本的startLeScan与Scanfilter.这可能是问题吗?

解决方法 为了保护隐私,mac地址不断变化 – 请参考下面的代码片段:

5.4.5 Privacy Feature

Bluetooth LE supports a feature that reduces the ability to track a LE device over a period of time by changing the
Bluetooth device address on a frequent basis. The privacy feature is
not used in the GAP discovery mode and procedures but it is used,when
supported,during connection mode and connection procedures. In order
for a device using the privacy feature to reconnect to kNown devices,
the device address,referred to as the private address,must be
resolvable by the other device.

The private address is generated using
the device’s resolving IDentity key (IRK) exchanged during the bonding
procedure.

所以理想的方法是使用IRK来实现设备的实际地址.但是我没有找到任何API来做.

目前,我在mny应用程序中使用的工作重新连接到设备,

>循环遍历所有可用的设备.
>获得序列号,如果序列号不匹配已存储的号码
然后断开连接.
>对所有设备重复步骤2.

如果设备通过某些服务暴露了序列号,这项工作将会起作用.

您也可以在调用connectGatt (Context context,boolean autoConnect,BluetoothGattCallback callback)时尝试将autoconnect标志设置为true

从文档:

public BluetoothGatt connectGatt (Context context,
BluetoothGattCallback callback) Added in API level 18

Connect to GATT Server hosted by this device. Caller acts as GATT
clIEnt. The callback is used to deliver results to Caller,such as
connection status as well as any further GATT clIEnt operations. The
method returns a BluetoothGatt instance. You can use BluetoothGatt to
conduct GATT clIEnt operations. Parameters

autoConnect Whether to
directly connect to the remote device (false) or to automatically
connect as soon as the remote device becomes available (true).

callback GATT callback handler that will receive asynchronous callbacks. Throws IllegalArgumentException if callback is null

总结

以上是内存溢出为你收集整理的android – 如何在每个打开同一个蓝牙低能量设备时重新连接我的应用程序?全部内容,希望文章能够帮你解决android – 如何在每个打开同一个蓝牙低能量设备时重新连接我的应用程序?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1131965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存