1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

be even more robust #35

This commit is contained in:
cpfeiffer 2015-04-25 23:47:28 +02:00
parent 1772076b62
commit 0377a751b0

View File

@ -30,6 +30,7 @@ public final class BtLEQueue {
private BluetoothGatt mBluetoothGatt; private BluetoothGatt mBluetoothGatt;
private volatile BlockingQueue<Transaction> mTransactions = new LinkedBlockingQueue<Transaction>(); private volatile BlockingQueue<Transaction> mTransactions = new LinkedBlockingQueue<Transaction>();
private volatile boolean mDisposed; private volatile boolean mDisposed;
private volatile boolean mCrashed;
private volatile boolean mAbortTransaction; private volatile boolean mAbortTransaction;
private Context mContext; private Context mContext;
@ -42,7 +43,9 @@ public final class BtLEQueue {
@Override @Override
public void run() { public void run() {
while (!mDisposed) { Log.d(TAG, "Queue Dispatch Thread started.");
while (!mDisposed && !mCrashed) {
try { try {
Transaction transaction = mTransactions.take(); Transaction transaction = mTransactions.take();
if (!isConnected()) { if (!isConnected()) {
@ -77,6 +80,12 @@ public final class BtLEQueue {
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
mWaitForActionResultLatch = null; mWaitForActionResultLatch = null;
mConnectionLatch = null; mConnectionLatch = null;
Log.d(TAG, "Thread interrupted");
} catch (Throwable ex) {
Log.e(TAG, "Queue Dispatch Thread died: " + ex.getMessage());
mCrashed = true;
mWaitForActionResultLatch = null;
mConnectionLatch = null;
} finally { } finally {
mWaitCharacteristic = null; mWaitCharacteristic = null;
} }
@ -103,12 +112,14 @@ public final class BtLEQueue {
* specific initialization. This should be done in the specific {@link DeviceSupport} * specific initialization. This should be done in the specific {@link DeviceSupport}
* class. * class.
* *
* @return true whether the connection attempt was successfully triggered * @return <code>true</code> whether the connection attempt was successfully triggered and <code>false</code> if that failed or if there is already a connection
*/ */
public boolean connect() { public boolean connect() {
if (mBluetoothGatt != null) { if (isConnected()) {
disconnect(); Log.w(TAG, "Ingoring connect() because already connected.");
return false;
} }
Log.i(TAG, "Attempting to connect to " + mGbDevice.getName());
BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mGbDevice.getAddress()); BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mGbDevice.getAddress());
mBluetoothGatt = remoteDevice.connectGatt(mContext, false, internalGattCallback); mBluetoothGatt = remoteDevice.connectGatt(mContext, false, internalGattCallback);
boolean result = mBluetoothGatt.connect(); boolean result = mBluetoothGatt.connect();
@ -162,9 +173,11 @@ public final class BtLEQueue {
* @param transaction * @param transaction
*/ */
public void add(Transaction transaction) { public void add(Transaction transaction) {
Log.d(TAG, "about to add: " + transaction);
if (!transaction.isEmpty()) { if (!transaction.isEmpty()) {
mTransactions.add(transaction); mTransactions.add(transaction);
} }
Log.d(TAG, "adding done: " + transaction);
} }
public void clear() { public void clear() {
@ -179,6 +192,7 @@ public final class BtLEQueue {
*/ */
public List<BluetoothGattService> getSupportedGattServices() { public List<BluetoothGattService> getSupportedGattServices() {
if (mBluetoothGatt == null) { if (mBluetoothGatt == null) {
Log.w(TAG, "BluetoothGatt is null => no services available.");
return Collections.emptyList(); return Collections.emptyList();
} }
return mBluetoothGatt.getServices(); return mBluetoothGatt.getServices();