BluetoothLowEnergy Range Issue android

1.5k Views Asked by At

Actually currently i am storing bluetooth Low energy device address in database. so at any time user can tap on listview and get the value from device. right now there is no mechanism to remove device (if device not in range(not found in scanning)). because we provide feature that user can get previous history value if device are not in range .so user tap on screen retrieve address from database and use

mBluetoothAdapter.getRemoteDevice(bleAddress);

method to retrieve BluetoothDevice object for further operation like connectgatt e.t.c. but according to google documentaion of getRemoteDevice(bleAddress)

return device object even if device is not in range according to google doc.

A BluetoothDevice will always be returned for a valid hardware address, even if this adapter has never seen that device.

so how do i check whether device is not in range. i can try to put timer for it but it is not good option please give me some more idea.

[UPDATE] ok i found some work around if i am try to connect already available device address which are not in range .so after some time android disconnected with status 133.

@Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {
            if(status==133){
                Intent intent=new Intent(FetchBluetoothCharacteristicService.DEVICE_NOT_RANGE);
                sendBroadcast(intent);
            }
            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.d(TAG, "GATT DEVICE CONNECTED");
                Log.d(TAG,
                        "And Attempting to start service discovery:"
                                + gatt.discoverServices());
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                close();
                Intent intent=new Intent(FetchBluetoothCharacteristicService.DEVICE_DISCONNECTED);
                intent.putExtra(COUNTER, counter);
                sendBroadcast(intent);
            }
    } 
1

There are 1 best solutions below

4
On

Can you look at the RSSI to see if it is in range?

https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#readRemoteRssi%28%29

That would tell you the received signal strength of the device - or FALSE if it can't be found.