mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 16:02:04 +02:00
Refactor BluetoothCommunication handler usage
refactors the `BluetoothCommunication` class to use a single `mainHandler` for managing various delayed tasks like pre-scan timeouts, disconnect timeouts, and connection delays.
This commit is contained in:
@@ -71,7 +71,11 @@ public abstract class BluetoothCommunication {
|
|||||||
protected Context context;
|
protected Context context;
|
||||||
|
|
||||||
private Handler callbackBtHandler;
|
private Handler callbackBtHandler;
|
||||||
private Handler disconnectHandler;
|
|
||||||
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
private Runnable preScanTimeout;
|
||||||
|
private Runnable disconnectTimeout;
|
||||||
|
private Runnable connectDelay;
|
||||||
|
|
||||||
private BluetoothCentralManager central;
|
private BluetoothCentralManager central;
|
||||||
private BluetoothPeripheral btPeripheral;
|
private BluetoothPeripheral btPeripheral;
|
||||||
@@ -86,10 +90,9 @@ public abstract class BluetoothCommunication {
|
|||||||
public BluetoothCommunication(Context context)
|
public BluetoothCommunication(Context context)
|
||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.disconnectHandler = new Handler(Looper.getMainLooper());
|
|
||||||
this.stepNr = 0;
|
this.stepNr = 0;
|
||||||
this.stopped = false;
|
this.stopped = false;
|
||||||
this.central = new BluetoothCentralManager(context, bluetoothCentralCallback, new Handler(Looper.getMainLooper()));
|
this.central = new BluetoothCentralManager(context, bluetoothCentralCallback, mainHandler);
|
||||||
this.selectedScaleUser = new ScaleUser();
|
this.selectedScaleUser = new ScaleUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,17 +444,18 @@ public abstract class BluetoothCommunication {
|
|||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
LogManager.d("BluetoothCommunication","Bluetooth disconnect");
|
LogManager.d("BluetoothCommunication","Bluetooth disconnect");
|
||||||
setBluetoothStatus(BT_STATUS.CONNECTION_DISCONNECT);
|
setBluetoothStatus(BT_STATUS.CONNECTION_DISCONNECT);
|
||||||
try {
|
try { central.stopScan(); } catch (Exception ex) {
|
||||||
central.stopScan();
|
LogManager.e(TAG, "Error on Bluetooth disconnecting " + ex.getMessage(), ex);
|
||||||
} catch (Exception ex) {
|
|
||||||
LogManager.e("BluetoothCommunication", "Error on Bluetooth disconnecting " + ex.getMessage(), ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (preScanTimeout != null) { mainHandler.removeCallbacks(preScanTimeout); preScanTimeout = null; }
|
||||||
|
if (disconnectTimeout != null) { mainHandler.removeCallbacks(disconnectTimeout); disconnectTimeout = null; }
|
||||||
|
if (connectDelay != null) { mainHandler.removeCallbacks(connectDelay); connectDelay = null; }
|
||||||
|
|
||||||
if (btPeripheral != null) {
|
if (btPeripheral != null) {
|
||||||
central.cancelConnection(btPeripheral);
|
central.cancelConnection(btPeripheral);
|
||||||
}
|
}
|
||||||
callbackBtHandler = null;
|
callbackBtHandler = null;
|
||||||
disconnectHandler.removeCallbacksAndMessages(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getScaleMacAddress() {
|
public byte[] getScaleMacAddress() {
|
||||||
@@ -594,6 +598,8 @@ public abstract class BluetoothCommunication {
|
|||||||
@Override
|
@Override
|
||||||
public void onDiscoveredPeripheral(BluetoothPeripheral peripheral, ScanResult scanResult) {
|
public void onDiscoveredPeripheral(BluetoothPeripheral peripheral, ScanResult scanResult) {
|
||||||
LogManager.d("BluetoothCommunication",String.format("Found peripheral '%s'", peripheral.getName()));
|
LogManager.d("BluetoothCommunication",String.format("Found peripheral '%s'", peripheral.getName()));
|
||||||
|
if (preScanTimeout != null) { mainHandler.removeCallbacks(preScanTimeout); preScanTimeout = null; }
|
||||||
|
btPeripheral = peripheral;
|
||||||
central.stopScan();
|
central.stopScan();
|
||||||
connectToDevice(peripheral);
|
connectToDevice(peripheral);
|
||||||
}
|
}
|
||||||
@@ -634,14 +640,16 @@ public abstract class BluetoothCommunication {
|
|||||||
LogManager.d("BluetoothCommunication",
|
LogManager.d("BluetoothCommunication",
|
||||||
"API≥31: Do LE scan before connecting (no location needed)");
|
"API≥31: Do LE scan before connecting (no location needed)");
|
||||||
central.scanForPeripheralsWithAddresses(new String[]{macAddress});
|
central.scanForPeripheralsWithAddresses(new String[]{macAddress});
|
||||||
new Handler(Looper.getMainLooper()).postDelayed(() -> { // wait for onDiscoveredPeripheral → connect
|
stopMachineState();
|
||||||
|
|
||||||
|
preScanTimeout = () -> {
|
||||||
if (btPeripheral == null || btPeripheral.getState() == ConnectionState.DISCONNECTED) {
|
if (btPeripheral == null || btPeripheral.getState() == ConnectionState.DISCONNECTED) {
|
||||||
try { central.stopScan(); } catch (Exception ignore) {}
|
try { central.stopScan(); } catch (Exception ignore) {}
|
||||||
setBluetoothStatus(BT_STATUS.NO_DEVICE_FOUND);
|
setBluetoothStatus(BT_STATUS.NO_DEVICE_FOUND);
|
||||||
sendMessage(R.string.info_bluetooth_connection_error_scale_offline, 0);
|
sendMessage(R.string.info_bluetooth_connection_error_scale_offline, 0);
|
||||||
resumeMachineState();
|
|
||||||
}
|
}
|
||||||
}, 10_000);
|
};
|
||||||
|
mainHandler.postDelayed(preScanTimeout, 10_000);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
LogManager.w("BluetoothCommunication",
|
LogManager.w("BluetoothCommunication",
|
||||||
@@ -664,11 +672,13 @@ public abstract class BluetoothCommunication {
|
|||||||
|
|
||||||
|
|
||||||
private void connectToDevice(BluetoothPeripheral peripheral) {
|
private void connectToDevice(BluetoothPeripheral peripheral) {
|
||||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
if (connectDelay != null) mainHandler.removeCallbacks(connectDelay);
|
||||||
|
connectDelay = () -> {
|
||||||
LogManager.d("BluetoothCommunication","Try to connect to BLE device " + peripheral.getAddress());
|
LogManager.d("BluetoothCommunication","Try to connect to BLE device " + peripheral.getAddress());
|
||||||
stepNr = 0;
|
stepNr = 0;
|
||||||
central.connectPeripheral(peripheral, peripheralCallback);
|
central.connectPeripheral(peripheral, peripheralCallback);
|
||||||
}, 1000);
|
};
|
||||||
|
mainHandler.postDelayed(connectDelay, 1_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean reConnectPreviousPeripheral(Handler uiHandler) {
|
protected boolean reConnectPreviousPeripheral(Handler uiHandler) {
|
||||||
@@ -686,18 +696,28 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void resetDisconnectTimer() {
|
private void resetDisconnectTimer() {
|
||||||
disconnectHandler.removeCallbacksAndMessages(null);
|
if (disconnectTimeout != null) {
|
||||||
disconnectWithDelay();
|
mainHandler.removeCallbacks(disconnectTimeout);
|
||||||
|
}
|
||||||
|
scheduleDisconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleDisconnect() {
|
||||||
|
disconnectTimeout = () -> {
|
||||||
|
LogManager.d("BluetoothCommunication","Timeout Bluetooth disconnect");
|
||||||
|
disconnect();
|
||||||
|
};
|
||||||
|
mainHandler.postDelayed(disconnectTimeout, 60_000); // 60 s
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disconnectWithDelay() {
|
private void disconnectWithDelay() {
|
||||||
disconnectHandler.postDelayed(new Runnable() {
|
mainHandler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
LogManager.d("BluetoothCommunication","Timeout Bluetooth disconnect");
|
LogManager.d("BluetoothCommunication","Timeout Bluetooth disconnect");
|
||||||
disconnect();
|
disconnect();
|
||||||
}
|
}
|
||||||
}, 60000); // 60s timeout
|
}, 60_000); // 60s timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void nextMachineStep() {
|
private synchronized void nextMachineStep() {
|
||||||
|
Reference in New Issue
Block a user