22. FIND MY Device Reconnection Compatibility Handling

FIND MY devices must support RPA addresses (resolvable random addresses), which change every 15 minutes. The device’s address needs to be identified through the IRK value to reconnect the device.

22.1. Flow Chart

FIND MY Device Reconnection Flow Chart

22.2. Synchronize IRK Information

//Get watch implementation class
WatchManager watchManager = WatchManager.getInstance();
//Get operation device
final BluetoothDevice device = watchManager.getConnectedDevice();
if (null == device) return; //Device not connected
targetAddress = device.getAddress(); //Cache target device address
//Register watch event listener
watchManager.registerOnRcspEventListener(new OnRcspEventListener() {
    @Override
    public void onIrkMessage(BluetoothDevice device, IrkMessage irkMessage) {
        //Callback IRK information (device pushes actively or APP requests actively)
        //Remove watch event listener
        watchManager.unregisterOnRcspEventListener(this);
        //Cache IRK information
        final IHistoryRecordOp dbHelper = BluetoothManager.getInstance().getHistoryRecordHelper();
        final HistoryRecord historyRecord = dbHelper.getHistoryRecord(device.getAddress());
        if (null == historyRecord) return;
        final String cacheIrkMsg = historyRecord.getIrkValue();
        String irkValue = CHexConver.byte2HexStr(irkMessage.getIrkValue());
        if (!TextUtils.equals(cacheIrkMsg, irkValue)) {
            historyRecord.setIrkValue(irkValue);
            //Update database
            dbHelper.updateHistoryRecord(historyRecord);
        }
    }
});
//Execute synchronize IRK information
watchManager.syncIrkMessage(new OnWatchOpCallback<IrkMessage>() {
    @Override
    public void onSuccess(IrkMessage result) {
        //Callback operation success
        //Also triggers OnRcspEventListener#onIrkMessage callback
        //result --- IRK information
    }

    @Override
    public void onFailed(BaseError error) {
        //Callback operation failure
        //error --- Error information
    }
});

22.3. Reconnect FIND MY Device

if (!CHexConver.checkBluetoothAddress(targetAddress)) return; //No target device address
final BluetoothManager btManager = BluetoothManager.getInstance();
final HistoryRecord historyRecord = btManager.getHistoryRecord(targetAddress);
final String irkValue = null == historyRecord ? "" : historyRecord.getIrkValue();
if (TextUtils.isEmpty(irkValue)) return; //No IRK information
final byte[] irkInfo = CHexConver.hexStr2Bytes(irkValue);
BluetoothEventCallback callback = new BluetoothEventCallback() {
    @Override
    public void onDiscoveryStatus(boolean bBle, boolean bStart) {
        super.onDiscoveryStatus(bBle, bStart);
        if (!bStart) {
            btManager.unregisterBluetoothCallback(this);
        }
    }

    @Override
    public void onDiscovery(BluetoothDevice device, BleScanMessage bleScanMessage) {
        if (null == device) return;
        // Check if device is reconnection device
        if (ParseDataUtil.isRightRPA(device.getAddress(), irkInfo)) {
            //Stop Bluetooth search
            btManager.stopBLEScan();
            //Reconnect device
            btManager.connectBtDevice(device, BluetoothConstant.PROTOCOL_TYPE_BLE);
        }
    }
};
//Register Bluetooth event listener
btManager.registerBluetoothCallback(callback);
//Start searching for BLE devices
if (!btManager.startBLEScan(30 * 1000)) {
    //Start searching for BLE devices failed
    btManager.unregisterBluetoothCallback(callback);
}