Robert Kofoed
Robert Kofoed

Reputation: 59

Android BluetoothAdapter getRemoteDevice is cached

I am developing an Android application, where I use Bluetooth Low Energy. My code logic includes using the BluetoothAdapter and method getRemoteDevice(MAC-adddress).

The problem is that when I use this method, Android seems to have cached it. Because when I am not close or the device is not on, it still creates the BluetoothDevice-object with the name and all that. But I cannot connect ofcourse.

How can I prevent Android from caching this old 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!

Upvotes: 3

Views: 1993

Answers (1)

James Allen
James Allen

Reputation: 7159

According to the documentation for 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.

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.

Upvotes: 1

Related Questions