2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.btle;
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-04-20 23:45:34 +02:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCallback;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
2015-05-24 14:39:36 +02:00
|
|
|
import android.bluetooth.BluetoothGattDescriptor;
|
2015-04-20 23:45:34 +02:00
|
|
|
import android.bluetooth.BluetoothGattService;
|
|
|
|
import android.bluetooth.BluetoothProfile;
|
|
|
|
import android.content.Context;
|
2015-08-05 23:24:58 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-26 00:53:48 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
|
2015-08-05 23:24:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
|
2015-04-26 00:53:48 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
/**
|
|
|
|
* One queue/thread per connectable device.
|
|
|
|
*/
|
|
|
|
public final class BtLEQueue {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BtLEQueue.class);
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-05-22 23:15:45 +02:00
|
|
|
private Object mGattMonitor = new Object();
|
2015-04-19 02:37:29 +02:00
|
|
|
private GBDevice mGbDevice;
|
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
|
private BluetoothGatt mBluetoothGatt;
|
2015-07-13 21:54:46 +02:00
|
|
|
/**
|
|
|
|
* When an automatic reconnect was attempted after a connection breakdown (error)
|
|
|
|
*/
|
|
|
|
private long lastReconnectTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
private static final long MIN_MILLIS_BEFORE_RECONNECT = 1000 * 60 * 5; // 5 minutes
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
private volatile BlockingQueue<Transaction> mTransactions = new LinkedBlockingQueue<Transaction>();
|
|
|
|
private volatile boolean mDisposed;
|
2015-04-25 23:47:28 +02:00
|
|
|
private volatile boolean mCrashed;
|
2015-04-19 02:37:29 +02:00
|
|
|
private volatile boolean mAbortTransaction;
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
private Context mContext;
|
|
|
|
private CountDownLatch mWaitForActionResultLatch;
|
|
|
|
private CountDownLatch mConnectionLatch;
|
|
|
|
private BluetoothGattCharacteristic mWaitCharacteristic;
|
2015-08-05 23:24:58 +02:00
|
|
|
private final InternalGattCallback internalGattCallback;
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-05-17 21:55:02 +02:00
|
|
|
private Thread dispatchThread = new Thread("GadgetBridge GATT Dispatcher") {
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-04-20 23:45:34 +02:00
|
|
|
@Override
|
2015-04-19 02:37:29 +02:00
|
|
|
public void run() {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.debug("Queue Dispatch Thread started.");
|
2015-04-25 23:47:28 +02:00
|
|
|
|
|
|
|
while (!mDisposed && !mCrashed) {
|
2015-04-19 11:28:03 +02:00
|
|
|
try {
|
|
|
|
Transaction transaction = mTransactions.take();
|
2015-08-05 23:24:58 +02:00
|
|
|
|
2015-04-19 11:28:03 +02:00
|
|
|
if (!isConnected()) {
|
|
|
|
// TODO: request connection and initialization from the outside and wait until finished
|
2015-09-10 00:00:52 +02:00
|
|
|
internalGattCallback.reset();
|
2015-04-19 11:28:03 +02:00
|
|
|
|
|
|
|
// wait until the connection succeeds before running the actions
|
|
|
|
// Note that no automatic connection is performed. This has to be triggered
|
|
|
|
// on the outside typically by the DeviceSupport. The reason is that
|
|
|
|
// devices have different kinds of initializations and this class has no
|
|
|
|
// idea about them.
|
|
|
|
mConnectionLatch = new CountDownLatch(1);
|
|
|
|
mConnectionLatch.await();
|
|
|
|
mConnectionLatch = null;
|
|
|
|
}
|
|
|
|
|
2015-08-05 23:24:58 +02:00
|
|
|
internalGattCallback.setTransactionGattCallback(transaction.getGattCallback());
|
2015-04-19 11:28:03 +02:00
|
|
|
mAbortTransaction = false;
|
|
|
|
// Run all actions of the transaction until one doesn't succeed
|
|
|
|
for (BtLEAction action : transaction.getActions()) {
|
2015-08-14 00:23:01 +02:00
|
|
|
if (mAbortTransaction) { // got disconnected
|
|
|
|
LOG.info("Aborting running transaction");
|
|
|
|
break;
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
mWaitCharacteristic = action.getCharacteristic();
|
2015-05-24 14:39:36 +02:00
|
|
|
mWaitForActionResultLatch = new CountDownLatch(1);
|
2015-07-03 21:58:13 +02:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
LOG.debug("About to run action: " + action);
|
|
|
|
}
|
2015-05-17 21:55:02 +02:00
|
|
|
if (action.run(mBluetoothGatt)) {
|
2015-05-24 14:39:36 +02:00
|
|
|
// check again, maybe due to some condition, action did not need to write, so we can't wait
|
|
|
|
boolean waitForResult = action.expectsResult();
|
2015-05-17 21:55:02 +02:00
|
|
|
if (waitForResult) {
|
|
|
|
mWaitForActionResultLatch.await();
|
|
|
|
mWaitForActionResultLatch = null;
|
|
|
|
if (mAbortTransaction) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Action returned false: " + action);
|
2015-04-19 11:28:03 +02:00
|
|
|
break; // abort the transaction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
mConnectionLatch = null;
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.debug("Thread interrupted");
|
2015-04-25 23:47:28 +02:00
|
|
|
} catch (Throwable ex) {
|
2015-10-18 22:08:52 +02:00
|
|
|
LOG.error("Queue Dispatch Thread died: " + ex.getMessage(), ex);
|
2015-04-25 23:47:28 +02:00
|
|
|
mCrashed = true;
|
|
|
|
mConnectionLatch = null;
|
2015-04-19 11:28:03 +02:00
|
|
|
} finally {
|
2015-05-28 00:26:41 +02:00
|
|
|
mWaitForActionResultLatch = null;
|
2015-04-19 11:28:03 +02:00
|
|
|
mWaitCharacteristic = null;
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Queue Dispatch Thread terminated.");
|
2015-04-19 11:28:03 +02:00
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
public BtLEQueue(BluetoothAdapter bluetoothAdapter, GBDevice gbDevice, GattCallback externalGattCallback, Context context) {
|
|
|
|
mBluetoothAdapter = bluetoothAdapter;
|
|
|
|
mGbDevice = gbDevice;
|
2015-08-05 23:24:58 +02:00
|
|
|
internalGattCallback = new InternalGattCallback(externalGattCallback);
|
2015-04-19 02:37:29 +02:00
|
|
|
mContext = context;
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
dispatchThread.start();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
protected boolean isConnected() {
|
|
|
|
return mGbDevice.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to the given remote device. Note that this does not perform any device
|
|
|
|
* specific initialization. This should be done in the specific {@link DeviceSupport}
|
|
|
|
* class.
|
2015-04-19 11:28:03 +02:00
|
|
|
*
|
2015-04-25 23:47:28 +02:00
|
|
|
* @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
|
2015-04-19 02:37:29 +02:00
|
|
|
*/
|
|
|
|
public boolean connect() {
|
2015-04-25 23:47:28 +02:00
|
|
|
if (isConnected()) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Ingoring connect() because already connected.");
|
2015-04-25 23:47:28 +02:00
|
|
|
return false;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
2015-05-22 23:15:45 +02:00
|
|
|
synchronized (mGattMonitor) {
|
|
|
|
if (mBluetoothGatt != null) {
|
|
|
|
// Tribal knowledge says you're better off not reusing existing BlueoothGatt connections,
|
|
|
|
// so create a new one.
|
|
|
|
LOG.info("connect() requested -- disconnecting previous connection: " + mGbDevice.getName());
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Attempting to connect to " + mGbDevice.getName());
|
2015-04-19 02:37:29 +02:00
|
|
|
BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mGbDevice.getAddress());
|
2015-05-22 23:15:45 +02:00
|
|
|
synchronized (mGattMonitor) {
|
2015-08-04 22:03:10 +02:00
|
|
|
mBluetoothGatt = remoteDevice.connectGatt(mContext, true, internalGattCallback);
|
2015-10-03 00:21:31 +02:00
|
|
|
mBluetoothGatt.connect();
|
2015-05-22 23:15:45 +02:00
|
|
|
}
|
2015-08-04 22:03:10 +02:00
|
|
|
setDeviceConnectionState(State.CONNECTING);
|
|
|
|
return true;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
private void setDeviceConnectionState(State newState) {
|
|
|
|
mGbDevice.setState(newState);
|
|
|
|
mGbDevice.sendDeviceUpdateIntent(mContext);
|
|
|
|
if (mConnectionLatch != null) {
|
|
|
|
mConnectionLatch.countDown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disconnect() {
|
2015-05-22 23:15:45 +02:00
|
|
|
synchronized (mGattMonitor) {
|
2015-08-14 00:23:01 +02:00
|
|
|
BluetoothGatt gatt = mBluetoothGatt;
|
|
|
|
if (gatt != null) {
|
2015-05-22 23:15:45 +02:00
|
|
|
mBluetoothGatt = null;
|
2015-08-14 00:23:01 +02:00
|
|
|
LOG.info("Disconnecting BtLEQueue from GATT device");
|
|
|
|
gatt.disconnect();
|
|
|
|
gatt.close();
|
2015-06-07 15:31:42 +02:00
|
|
|
setDeviceConnectionState(State.NOT_CONNECTED);
|
2015-05-22 23:15:45 +02:00
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:54:46 +02:00
|
|
|
private void handleDisconnected(int status) {
|
2015-08-18 00:08:22 +02:00
|
|
|
internalGattCallback.reset();
|
2015-04-19 02:37:29 +02:00
|
|
|
mTransactions.clear();
|
2015-08-14 00:23:01 +02:00
|
|
|
mAbortTransaction = true;
|
2015-04-19 02:37:29 +02:00
|
|
|
if (mWaitForActionResultLatch != null) {
|
|
|
|
mWaitForActionResultLatch.countDown();
|
|
|
|
}
|
|
|
|
setDeviceConnectionState(State.NOT_CONNECTED);
|
2015-07-13 21:54:46 +02:00
|
|
|
|
2015-05-22 23:15:45 +02:00
|
|
|
// either we've been disconnected because the device is out of range
|
|
|
|
// or because of an explicit @{link #disconnect())
|
|
|
|
// To support automatic reconnection, we keep the mBluetoothGatt instance
|
2015-07-13 21:54:46 +02:00
|
|
|
// alive (we do not close() it). Unfortunately we sometimes have problems
|
|
|
|
// reconnecting automatically, so we try to fix this by re-creating mBluetoothGatt.
|
2015-08-14 00:23:01 +02:00
|
|
|
// Not sure if this actually works without re-initializing the device...
|
2015-07-25 21:52:52 +02:00
|
|
|
if (status != 0) {
|
2015-08-14 00:23:01 +02:00
|
|
|
if (!maybeReconnect()) {
|
|
|
|
disconnect(); // ensure that we start over cleanly next time
|
|
|
|
}
|
2015-07-13 21:54:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 00:23:01 +02:00
|
|
|
/**
|
|
|
|
* Depending on certain criteria, connects to the BluetoothGatt.
|
2015-09-24 14:45:21 +02:00
|
|
|
*
|
2015-08-14 00:23:01 +02:00
|
|
|
* @return true if a reconnection attempt was made, or false otherwise
|
|
|
|
*/
|
|
|
|
private boolean maybeReconnect() {
|
2015-07-13 21:54:46 +02:00
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
if (currentTime - lastReconnectTime >= MIN_MILLIS_BEFORE_RECONNECT) {
|
2015-08-14 00:23:01 +02:00
|
|
|
LOG.info("Automatic reconnection attempt...");
|
2015-07-13 21:54:46 +02:00
|
|
|
lastReconnectTime = currentTime;
|
2015-08-14 00:23:01 +02:00
|
|
|
return connect();
|
2015-07-13 21:54:46 +02:00
|
|
|
}
|
2015-08-14 00:23:01 +02:00
|
|
|
return false;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public void dispose() {
|
|
|
|
if (mDisposed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDisposed = true;
|
|
|
|
// try {
|
2015-04-19 11:28:03 +02:00
|
|
|
disconnect();
|
|
|
|
dispatchThread.interrupt();
|
|
|
|
dispatchThread = null;
|
2015-04-19 02:37:29 +02:00
|
|
|
// dispatchThread.join();
|
|
|
|
// } catch (InterruptedException ex) {
|
2015-05-12 06:28:11 +02:00
|
|
|
// LOG.error("Exception while disposing BtLEQueue", ex);
|
2015-04-19 02:37:29 +02:00
|
|
|
// }
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
/**
|
|
|
|
* Adds a transaction to the end of the queue.
|
2015-04-19 11:28:03 +02:00
|
|
|
*
|
2015-04-19 02:37:29 +02:00
|
|
|
* @param transaction
|
|
|
|
*/
|
|
|
|
public void add(Transaction transaction) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.debug("about to add: " + transaction);
|
2015-04-19 02:37:29 +02:00
|
|
|
if (!transaction.isEmpty()) {
|
|
|
|
mTransactions.add(transaction);
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public void clear() {
|
|
|
|
mTransactions.clear();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a list of supported GATT services on the connected device. This should be
|
|
|
|
* invoked only after {@code BluetoothGatt#discoverServices()} completes successfully.
|
|
|
|
*
|
|
|
|
* @return A {@code List} of supported services.
|
|
|
|
*/
|
|
|
|
public List<BluetoothGattService> getSupportedGattServices() {
|
|
|
|
if (mBluetoothGatt == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("BluetoothGatt is null => no services available.");
|
2015-04-19 02:37:29 +02:00
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
return mBluetoothGatt.getServices();
|
|
|
|
}
|
|
|
|
|
2015-05-22 23:15:45 +02:00
|
|
|
private boolean checkCorrectGattInstance(BluetoothGatt gatt, String where) {
|
|
|
|
if (gatt != mBluetoothGatt) {
|
|
|
|
LOG.info("Ignoring event from wrong BluetoothGatt instance: " + where);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
// Implements callback methods for GATT events that the app cares about. For example,
|
|
|
|
// connection change and services discovered.
|
2015-08-05 23:24:58 +02:00
|
|
|
private final class InternalGattCallback extends BluetoothGattCallback {
|
2015-09-24 14:45:21 +02:00
|
|
|
private
|
|
|
|
@Nullable
|
|
|
|
GattCallback mTransactionGattCallback;
|
2015-08-05 23:24:58 +02:00
|
|
|
private GattCallback mExternalGattCallback;
|
|
|
|
|
|
|
|
public InternalGattCallback(GattCallback externalGattCallback) {
|
|
|
|
mExternalGattCallback = externalGattCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransactionGattCallback(@Nullable GattCallback callback) {
|
|
|
|
mTransactionGattCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
private GattCallback getCallbackToUse() {
|
|
|
|
if (mTransactionGattCallback != null) {
|
|
|
|
return mTransactionGattCallback;
|
|
|
|
}
|
|
|
|
return mExternalGattCallback;
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
@Override
|
|
|
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("connection state change, newState: " + newState + getStatusString(status));
|
2015-05-24 23:12:54 +02:00
|
|
|
|
2015-05-22 23:15:45 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "connection state event")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:12:54 +02:00
|
|
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
2015-05-28 23:26:17 +02:00
|
|
|
LOG.warn("connection state event with error status " + status);
|
2015-05-24 23:12:54 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
switch (newState) {
|
2015-04-19 11:28:03 +02:00
|
|
|
case BluetoothProfile.STATE_CONNECTED:
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Connected to GATT server.");
|
2015-04-19 11:28:03 +02:00
|
|
|
setDeviceConnectionState(State.CONNECTED);
|
|
|
|
// Attempts to discover services after successful connection.
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Attempting to start service discovery:" +
|
2015-04-19 11:28:03 +02:00
|
|
|
mBluetoothGatt.discoverServices());
|
|
|
|
break;
|
|
|
|
case BluetoothProfile.STATE_DISCONNECTED:
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Disconnected from GATT server.");
|
2015-07-13 21:54:46 +02:00
|
|
|
handleDisconnected(status);
|
2015-04-19 11:28:03 +02:00
|
|
|
break;
|
|
|
|
case BluetoothProfile.STATE_CONNECTING:
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("Connecting to GATT server...");
|
2015-04-19 11:28:03 +02:00
|
|
|
setDeviceConnectionState(State.CONNECTING);
|
|
|
|
break;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "services discovered: " + getStatusString(status))) {
|
2015-05-22 23:15:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-04-19 02:37:29 +02:00
|
|
|
// only propagate the successful event
|
2015-08-05 23:24:58 +02:00
|
|
|
getCallbackToUse().onServicesDiscovered(gatt);
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("onServicesDiscovered received: " + status);
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("characteristic write: " + characteristic.getUuid() + getStatusString(status));
|
2015-05-22 23:15:45 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "characteristic write")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
|
|
|
getCallbackToUse().onCharacteristicWrite(gatt, characteristic, status);
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
checkWaitingCharacteristic(characteristic, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCharacteristicRead(BluetoothGatt gatt,
|
|
|
|
BluetoothGattCharacteristic characteristic,
|
|
|
|
int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("characteristic read: " + characteristic.getUuid() + getStatusString(status));
|
2015-05-22 23:15:45 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "characteristic read")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-10-04 00:00:53 +02:00
|
|
|
try {
|
|
|
|
getCallbackToUse().onCharacteristicRead(gatt, characteristic, status);
|
|
|
|
} catch (Throwable ex) {
|
2015-10-18 17:29:41 +02:00
|
|
|
LOG.error("onCharacteristicRead: " + ex.getMessage(), ex);
|
2015-10-04 00:00:53 +02:00
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
checkWaitingCharacteristic(characteristic, status);
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:39:36 +02:00
|
|
|
@Override
|
|
|
|
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("descriptor read: " + descriptor.getUuid() + getStatusString(status));
|
2015-05-24 14:39:36 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "descriptor read")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-10-04 00:00:53 +02:00
|
|
|
try {
|
|
|
|
getCallbackToUse().onDescriptorRead(gatt, descriptor, status);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("onDescriptorRead: " + ex.getMessage(), ex);
|
|
|
|
}
|
2015-05-24 14:39:36 +02:00
|
|
|
}
|
|
|
|
checkWaitingCharacteristic(descriptor.getCharacteristic(), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("descriptor write: " + descriptor.getUuid() + getStatusString(status));
|
2015-05-24 14:39:36 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "descriptor write")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-10-04 00:00:53 +02:00
|
|
|
try {
|
|
|
|
getCallbackToUse().onDescriptorWrite(gatt, descriptor, status);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("onDescriptorWrite: " + ex.getMessage(), ex);
|
|
|
|
}
|
2015-05-24 14:39:36 +02:00
|
|
|
}
|
|
|
|
checkWaitingCharacteristic(descriptor.getCharacteristic(), status);
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
@Override
|
|
|
|
public void onCharacteristicChanged(BluetoothGatt gatt,
|
|
|
|
BluetoothGattCharacteristic characteristic) {
|
2015-05-24 14:39:36 +02:00
|
|
|
LOG.debug("characteristic changed: " + characteristic.getUuid());
|
2015-05-22 23:15:45 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "characteristic changed")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-10-04 00:00:53 +02:00
|
|
|
try {
|
|
|
|
getCallbackToUse().onCharacteristicChanged(gatt, characteristic);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("onCharaceristicChanged: " + ex.getMessage(), ex);
|
|
|
|
}
|
2015-09-09 23:23:38 +02:00
|
|
|
} else {
|
|
|
|
LOG.info("No gattcallback registered, ignoring characteristic change");
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("remote rssi: " + rssi + getStatusString(status));
|
2015-05-22 23:15:45 +02:00
|
|
|
if (!checkCorrectGattInstance(gatt, "remote rssi")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
if (getCallbackToUse() != null) {
|
2015-10-04 00:00:53 +02:00
|
|
|
try {
|
|
|
|
getCallbackToUse().onReadRemoteRssi(gatt, rssi, status);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("onReadRemoteRssi: " + ex.getMessage(), ex);
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
private void checkWaitingCharacteristic(BluetoothGattCharacteristic characteristic, int status) {
|
|
|
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
2015-05-25 23:14:02 +02:00
|
|
|
LOG.debug("failed btle action, aborting transaction: " + characteristic.getUuid() + getStatusString(status));
|
2015-04-19 02:37:29 +02:00
|
|
|
mAbortTransaction = true;
|
|
|
|
}
|
|
|
|
if (characteristic != null && BtLEQueue.this.mWaitCharacteristic != null && characteristic.getUuid().equals(BtLEQueue.this.mWaitCharacteristic.getUuid())) {
|
|
|
|
if (mWaitForActionResultLatch != null) {
|
|
|
|
mWaitForActionResultLatch.countDown();
|
|
|
|
}
|
2015-05-17 21:55:02 +02:00
|
|
|
} else {
|
|
|
|
if (BtLEQueue.this.mWaitCharacteristic != null) {
|
|
|
|
LOG.error("checkWaitingCharacteristic: mismatched characteristic received: " + characteristic != null ? characteristic.getUuid().toString() : "(null)");
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 23:14:02 +02:00
|
|
|
|
2015-08-05 23:24:58 +02:00
|
|
|
private String getStatusString(int status) {
|
|
|
|
return status == BluetoothGatt.GATT_SUCCESS ? " (success)" : " (failed: " + status + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() {
|
2015-09-09 23:39:57 +02:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
LOG.debug("internal gatt callback set to null");
|
|
|
|
}
|
2015-08-05 23:24:58 +02:00
|
|
|
mTransactionGattCallback = null;
|
|
|
|
}
|
2015-09-24 14:45:21 +02:00
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|