Improve logging when multiple devices are connected

This commit is contained in:
José Rebelo
2025-12-23 17:31:46 +00:00
parent 538f16d210
commit 53c9355e98
8 changed files with 27 additions and 17 deletions
@@ -43,7 +43,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public final class BtBRQueue {
private static final Logger LOG = LoggerFactory.getLogger(BtBRQueue.class);
private final Logger LOG;
private static final AtomicLong QUEUE_COUNTER = new AtomicLong(0L);
private static final AtomicLong THREAD_COUNTER = new AtomicLong(0L);
public static final int HANDLER_SUBJECT_CONNECT = 0;
public static final int HANDLER_SUBJECT_PERFORM_TRANSACTION = 1;
@@ -68,7 +69,7 @@ public final class BtBRQueue {
return new Thread("BtBRQueue_read_" + THREAD_COUNTER.getAndIncrement()) {
@Override
public void run() {
LOG.debug("started thread {}", getName());
LOG.debug("started thread {} for {}", getName(), mGbDevice.getAddress());
final byte[] buffer = new byte[mBufferSize];
int nRead;
@@ -115,6 +116,8 @@ public final class BtBRQueue {
}
public BtBRQueue(BluetoothAdapter btAdapter, GBDevice gbDevice, Context context, SocketCallback socketCallback, @NonNull UUID supportedService, int bufferSize) {
LOG = LoggerFactory.getLogger(BtBRQueue.class.getName() + "(" + QUEUE_COUNTER.getAndIncrement() + ")");
mBtAdapter = btAdapter;
mGbDevice = gbDevice;
mContext = context;
@@ -126,9 +129,10 @@ public final class BtBRQueue {
mWriteHandlerThread.start();
new Handler(mWriteHandlerThread.getLooper()).post(()
-> LOG.debug("started thread {}", Thread.currentThread().getName()));
-> LOG.debug("started thread {} for {}", Thread.currentThread().getName(), gbDevice.getAddress()));
LOG.debug("Write handler thread for {} is prepared, creating write handler", gbDevice.getAddress());
LOG.debug("Write handler thread is prepared, creating write handler");
mWriteHandler = new Handler(mWriteHandlerThread.getLooper()) {
@SuppressLint("MissingPermission")
@Override
@@ -87,7 +87,7 @@ public abstract class BtClassicIoThread extends GBDeviceIoThread {
@Override
public void run() {
LOG.debug("started thread {}", getName());
LOG.debug("Started thread {} for {}", getName(), gbDevice.getAddress());
mIsConnected = connect();
if (!mIsConnected) {
if (GBApplication.getPrefs().getAutoReconnect(getDevice()) && !mQuit) {
@@ -65,8 +65,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB;
*/
@SuppressLint("MissingPermission") // if we're using this, we have bluetooth permissions
public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(BtLEQueue.class);
private final Logger LOG;
private static final byte[] EMPTY = new byte[0];
private static final AtomicLong QUEUE_COUNTER = new AtomicLong(0L);
private static final AtomicLong THREAD_COUNTER = new AtomicLong(0L);
private final Object mGattMonitor;
@@ -101,7 +102,7 @@ public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
private class DispatchRunnable implements Runnable {
@Override
public void run() {
LOG.debug("started thread {}", Thread.currentThread().getName());
LOG.debug("started thread {} for {}", Thread.currentThread().getName(), mGbDevice.getAddress());
boolean crashed = false;
while (!mDisposed.get() && !crashed) {
@@ -216,6 +217,12 @@ public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
};
BtLEQueue(GBDevice gbDevice, Set<? extends BluetoothGattService> supportedServerServices, AbstractBTLEDeviceSupport deviceSupport) {
final long threadIdx = THREAD_COUNTER.getAndIncrement();
LOG = LoggerFactory.getLogger(BtLEQueue.class.getName() + "(" + QUEUE_COUNTER.getAndIncrement() + ")");
LOG.debug("Initializing queue for {} with threadIdx={}", gbDevice.getAddress(), threadIdx);
// 1) apply all settings
mBluetoothAdapter = deviceSupport.getBluetoothAdapter();
mContext = deviceSupport.getContext();
@@ -228,8 +235,6 @@ public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
// #5414 - some older android versions misbehave with the new constructor
connectionForceLegacyGatt = deviceSupport.getDevicePrefs().getConnectionForceLegacyGatt();
long threadIdx = THREAD_COUNTER.getAndIncrement();
// 2) create new objects
mDisposed = new AtomicBoolean(false);
mGattMonitor = new Object();
@@ -243,12 +248,12 @@ public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
mDispatchThread.start();
// 4) handler thread ensure serial processing and informative thread name in the log
if(GBApplication.isRunningOreoOrLater() && !connectionForceLegacyGatt){
if (GBApplication.isRunningOreoOrLater() && !connectionForceLegacyGatt) {
mReceiverThread = new HandlerThread("BtLEQueue_" + threadIdx + "_in");
mReceiverThread.setUncaughtExceptionHandler(this);
mReceiverThread.start();
mReceiverHandler = new Handler(mReceiverThread.getLooper());
mReceiverHandler.post(() -> LOG.debug("started thread {}", Thread.currentThread().getName()));
mReceiverHandler.post(() -> LOG.debug("started thread {} for {}", Thread.currentThread().getName(), gbDevice.getAddress()));
} else {
mReceiverThread = null;
mReceiverHandler = null;
@@ -46,7 +46,7 @@ public class CasioGB6900HandlerThread extends GBDeviceIoThread {
@Override
public void run() {
LOG.debug("started thread {}", getName());
LOG.debug("started thread {} for {}", getName(), gbDevice.getAddress());
mQuit = false;
@@ -85,7 +85,7 @@ class HPlusHandlerThread extends GBDeviceIoThread {
@Override
public void run() {
LOG.debug("started thread {}", getName());
LOG.debug("started thread {} for {}", getName(), gbDevice.getAddress());
mQuit = false;
@@ -245,7 +245,7 @@ class PebbleIoThread extends GBDeviceIoThread {
@Override
public void run() {
LOG.debug("started thread {}", getName());
LOG.debug("started thread {} for {}", getName(), gbDevice.getAddress());
mIsConnected = connect();
if (!mIsConnected) {
@@ -68,7 +68,7 @@ public class PebbleLESupport {
mWriteHandlerThread = new HandlerThread("PebbleLESupport_write_" + THREAD_COUNTER.getAndIncrement());
mWriteHandlerThread.start();
mWriteHandler = new Handler(mWriteHandlerThread.getLooper());
mWriteHandler.post(() -> LOG.debug("started thread {}", Thread.currentThread().getName()));
mWriteHandler.post(() -> LOG.debug("started thread {} for {}", Thread.currentThread().getName(), gbDevice.getAddress()));
mMTULimit = GBApplication.getDevicePrefs(gbDevice).getInt("pebble_mtu_limit", 512);
mMTULimit = Math.max(mMTULimit, 20);
@@ -109,8 +109,9 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
*/
public synchronized GBDeviceIoThread getDeviceIOThread() {
if (gbDeviceIOThread == null || !gbDeviceIOThread.isAlive()) {
LOG.debug("Creating new IO thread");
gbDeviceIOThread = createDeviceIOThread();
LOG.debug("Creating new IO thread for {}", gbDevice.getAddress());
final Thread thread = (gbDeviceIOThread = createDeviceIOThread());
LOG.debug("New IO thread: {}", thread.getName());
}
return gbDeviceIOThread;
}