简体   繁体   English

Android BluetoothAdapter getRemoteDevice已缓存

[英]Android BluetoothAdapter getRemoteDevice is cached

I am developing an Android application, where I use Bluetooth Low Energy. 我正在开发一个使用蓝牙低功耗的Android应用程序。 My code logic includes using the BluetoothAdapter and method getRemoteDevice(MAC-adddress). 我的代码逻辑包括使用BluetoothAdapter和方法getRemoteDevice(MAC-adddress)。

The problem is that when I use this method, Android seems to have cached it. 问题是,当我使用此方法时,Android似乎已对其进行了缓存。 Because when I am not close or the device is not on, it still creates the BluetoothDevice-object with the name and all that. 因为当我没有关闭或设备未打开时,它仍会使用名称和所有名称创建BluetoothDevice对象。 But I cannot connect ofcourse. 但是我当然不能联系。

How can I prevent Android from caching this old BluetoothDevice? 如何防止Android缓存此旧的BluetoothDevice?

I have tried reflection with 我尝试过反射

Method m = device.getClass().getMethod("removeBond", (Class[]) null); m.invoke(device, (Object[]) null);

But it wont yield any better result. 但是它不会产生任何更好的结果。

Thank you! 谢谢!

According to the documentation for getRemoteDevice : 根据getRemoteDevice的文档

A BluetoothDevice will always be returned for a valid hardware address, even if this adapter has never seen that device. 即使此适配器从未看到过该设备,也将始终为该设备返回有效的硬件地址。

So, it is not caching the result, it is just creating a dumb BluetoothDevice object, which has no idea if that MAC address even exists. 因此,它不是在缓存结果,而是在创建一个愚蠢的BluetoothDevice对象,该对象甚至不知道该MAC地址是否存在。

You could attempt to connect to the GATT service of the device, or start discovery, or use the LE scanner object, and use the appropriate callback to check if the connection has succeeded or not. 您可以尝试连接到设备的GATT服务,或者开始发现,或者使用LE扫描程序对象,并使用适当的回调检查连接是否成功。

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

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