Open Fly-Felix opened 4 months ago
This is how I switch connections. Don't know what's the problem? Timeouts can easily occur after switching connections multiple times. Then the above problems will occur.
private ConnectRequest connectRequest;
private void addDevice(BluetoothDevice device) {
new Thread(new Runnable() {
@Override
public void run() {
if (clientManager.isConnected()) {
try {
clientManager.disconnect().await();
clientManager.close();
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
if (connectRequest != null) {
connectRequest.cancel();
}
connectRequest = clientManager.connect(device)
.useAutoConnect(true)
.retry(3, 0)
.timeout(20 * 1000);
connectRequest.enqueue();
}
}).start();
}
Sorry, let me correct the description above. This error does not necessarily occur when the connection times out, because I just tested it and this error also occurs when the connection is in progress, and you can always see it when there is a problem.
2024-04-18 11:32:39.998 BluetoothAdapter D onBluetoothServiceDown: android.bluetooth.IBluetooth$Stub$Proxy@9c53240
2024-04-18 11:32:40.021 BluetoothAdapter D onBluetoothServiceDown: null
So far I have tested 100% of the steps that trigger the problem. Triggering a rescan after connecting to the device or during the connection process can cause the above issues.
This is my scanning code. Currently, this kind of thing only happens on some Android 9.0 devices. I tested another Android 7.0 device. It was connecting or connected. It was normal when I rescanned it. With my scan settings, it might be possible. does it matter?
private void startScan() {
if (!PermissionUtils.isGpsEnabled(requireContext())) {
showGpsRequestReasonDialog();
return;
}
Log.e(TAG, "startScan: ---");
binding.mSrLayout.setRefreshing(true);
mDeviceAdapter.setNewData(getConnectedDevices());
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(5000)
.setUseHardwareBatchingIfSupported(false)
.setUseHardwareFilteringIfSupported(false)
.build();
final List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(HEART_RATE_SERVICE_UUID)).build());
scanner.startScan(filters, settings, scanCallback);
scanning = true;
handler.removeCallbacks(stopScanRunnable);
handler.postDelayed(stopScanRunnable, SCAN_DURATION);
}
This happens when switching connections between multiple heartbeat devices. Please help me take a look, thank you!
I'm using
implementation("no.nordicsemi.android:ble:2.7.4")
This is my error log: