Sony Headphones: Add 500ms connection delay

This commit is contained in:
José Rebelo
2026-01-01 23:01:34 +00:00
parent 43fc2e4727
commit e6ff0aef04
4 changed files with 45 additions and 4 deletions
@@ -83,7 +83,15 @@ public abstract class AbstractBTBRDeviceSupport extends AbstractDeviceSupport im
}
if (mQueue == null) {
mQueue = new BtBRQueue(getBluetoothAdapter(), getDevice(), getContext(), this, supportedService, getBufferSize());
mQueue = new BtBRQueue(
getBluetoothAdapter(),
getDevice(),
getContext(),
this,
supportedService,
getBufferSize(),
getConnectDelayMillis()
);
}
return mQueue.connect();
}
@@ -157,6 +165,14 @@ public abstract class AbstractBTBRDeviceSupport extends AbstractDeviceSupport im
return mBufferSize;
}
/**
* Some devices fail to connect to the btrfcomm socket if we connect too fast. Increase this delay
* to wait a few milliseconds.
*/
protected int getConnectDelayMillis() {
return 0;
}
/**
* Utility method that may be used to log incoming messages when we don't know how to deal with them yet.
*/
@@ -59,6 +59,7 @@ public final class BtBRQueue {
private final Context mContext;
private final int mBufferSize;
private final int mConnectDelayMillis;
private final Handler mWriteHandler;
private final HandlerThread mWriteHandlerThread = new HandlerThread("BtBRQueue_write_" + THREAD_COUNTER.getAndIncrement(), Process.THREAD_PRIORITY_BACKGROUND);
@@ -115,7 +116,13 @@ public final class BtBRQueue {
};
}
public BtBRQueue(BluetoothAdapter btAdapter, GBDevice gbDevice, Context context, SocketCallback socketCallback, @NonNull UUID supportedService, int bufferSize) {
public BtBRQueue(BluetoothAdapter btAdapter,
GBDevice gbDevice,
Context context,
SocketCallback socketCallback,
@NonNull UUID supportedService,
int bufferSize,
int connectDelayMillis) {
LOG = LoggerFactory.getLogger(BtBRQueue.class.getName() + "(" + QUEUE_COUNTER.getAndIncrement() + ")");
mBtAdapter = btAdapter;
@@ -124,6 +131,7 @@ public final class BtBRQueue {
mCallback = socketCallback;
mService = supportedService;
mBufferSize = bufferSize;
mConnectDelayMillis = connectDelayMillis;
mDisposed = new AtomicBoolean(false);
mWriteHandlerThread.start();
@@ -149,7 +157,18 @@ public final class BtBRQueue {
return;
}
if (mConnectDelayMillis > 0) {
LOG.debug("Waiting {} ms before connecting to RFCOMM socket", mConnectDelayMillis);
try {
Thread.sleep(mConnectDelayMillis);
} catch (final InterruptedException e) {
LOG.error("Interrupted while waiting for connect", e);
}
}
try {
LOG.debug("Connecting to RFCOMM socket for {}", mGbDevice.getName());
mBtSocket.connect();
LOG.info("Connected to RFCOMM socket for {}", mGbDevice.getName());
@@ -82,6 +82,12 @@ public class SonyHeadphonesSupport extends AbstractHeadphoneSerialDeviceSupportV
return new SonyHeadphonesProtocol(getDevice());
}
@Override
protected int getConnectDelayMillis() {
// Connecting too fast fails with an IOException
return 500;
}
@Override
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof SonyHeadphonesEnqueueRequestEvent enqueueRequestEvent) {
@@ -46,7 +46,7 @@ public class TestBtBRQueue {
BluetoothAdapter btAdapter = Mockito.mock(BluetoothAdapter.class);
when(btAdapter.getRemoteDevice((String) any())).thenReturn(btDevice);
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512);
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512, 0);
Assert.assertTrue(queue.connect());
}
@@ -60,7 +60,7 @@ public class TestBtBRQueue {
BluetoothAdapter btAdapter = Mockito.mock(BluetoothAdapter.class);
when(btAdapter.getRemoteDevice((String) any())).thenReturn(btDevice);
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512);
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512, 0);
Assert.assertTrue(queue.connect());
queue.disconnect();