1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-02 11:26:09 +02:00

reformat code with androidstudio

This commit is contained in:
Andreas Shimokawa 2015-04-19 11:28:03 +02:00
parent 09914e6da0
commit dd12bb11b2
16 changed files with 153 additions and 140 deletions

View File

@ -21,11 +21,11 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
public boolean isConnected() { public boolean isConnected() {
return gbDevice.isConnected(); return gbDevice.isConnected();
} }
protected boolean isInitialized() { protected boolean isInitialized() {
return gbDevice.isInitialized(); return gbDevice.isInitialized();
} }
@Override @Override
public GBDevice getDevice() { public GBDevice getDevice() {
return gbDevice; return gbDevice;

View File

@ -7,10 +7,10 @@ import android.content.Context;
* Provides support for a specific device. Has hooks to manage the life cycle * Provides support for a specific device. Has hooks to manage the life cycle
* of a device: instances of this interface will be created, initialized, and disposed * of a device: instances of this interface will be created, initialized, and disposed
* as needed. * as needed.
* * <p/>
* Implementations need to act accordingly, in order to establish, reestablish or close * Implementations need to act accordingly, in order to establish, reestablish or close
* the connection to the device. * the connection to the device.
* * <p/>
* This interface is agnostic to the kind transport, i.e. whether the device is connected * This interface is agnostic to the kind transport, i.e. whether the device is connected
* via Bluetooth, Bluetooth LE, Wifi or something else. * via Bluetooth, Bluetooth LE, Wifi or something else.
*/ */
@ -18,7 +18,7 @@ public interface DeviceSupport extends EventHandler {
public void initialize(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context); public void initialize(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context);
public boolean isConnected(); public boolean isConnected();
public boolean connect(); public boolean connect();
public void dispose(); public void dispose();

View File

@ -36,11 +36,11 @@ public class GBDevice {
public boolean isConnected() { public boolean isConnected() {
return state.ordinal() >= State.CONNECTED.ordinal(); return state.ordinal() >= State.CONNECTED.ordinal();
} }
public boolean isInitialized() { public boolean isInitialized() {
return state.ordinal() >= State.INITIALIZED.ordinal(); return state.ordinal() >= State.INITIALIZED.ordinal();
} }
public State getState() { public State getState() {
return state; return state;
} }

View File

@ -1,5 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.btle; package nodomain.freeyourgadget.gadgetbridge.btle;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -8,19 +13,14 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.AbstractDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.AbstractDeviceSupport;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.util.Log;
/** /**
*
* @see TransactionBuilder * @see TransactionBuilder
* @see BtLEQueue * @see BtLEQueue
*/ */
public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport implements GattCallback { public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport implements GattCallback {
private static final String TAG = "AbstractBTLEDeviceSupport"; private static final String TAG = "AbstractBTLEDeviceSupport";
private BtLEQueue mQueue; private BtLEQueue mQueue;
private HashMap<UUID, BluetoothGattCharacteristic> mAvailableCharacteristics; private HashMap<UUID, BluetoothGattCharacteristic> mAvailableCharacteristics;
private Set<UUID> mSupportedServices = new HashSet<>(4); private Set<UUID> mSupportedServices = new HashSet<>(4);
@ -35,6 +35,7 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
/** /**
* Subclasses should populate the given builder to initialize the device (if necessary). * Subclasses should populate the given builder to initialize the device (if necessary).
*
* @param builder * @param builder
* @return the same builder as passed as the argument * @return the same builder as passed as the argument
*/ */
@ -48,13 +49,14 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
mQueue.dispose(); mQueue.dispose();
} }
} }
/** /**
* Send commands like this to the device: * Send commands like this to the device:
* <p> * <p>
* <code>perform("sms notification").write(someCharacteristic, someByteArray).queue(getQueue());</code> * <code>perform("sms notification").write(someCharacteristic, someByteArray).queue(getQueue());</code>
* </p> * </p>
* TODO: support orchestration of multiple reads and writes depending on returned values * TODO: support orchestration of multiple reads and writes depending on returned values
*
* @see #performConnected(Transaction) * @see #performConnected(Transaction)
* @see #initializeDevice(TransactionBuilder) * @see #initializeDevice(TransactionBuilder)
*/ */
@ -73,7 +75,6 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
} }
/** /**
*
* @param transaction * @param transaction
* @throws IOException * @throws IOException
* @see {@link #performInitialized(String)} * @see {@link #performInitialized(String)}
@ -90,10 +91,11 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
public BtLEQueue getQueue() { public BtLEQueue getQueue() {
return mQueue; return mQueue;
} }
/** /**
* Subclasses should call this method to add services they support. * Subclasses should call this method to add services they support.
* Only supported services will be queried for characteristics. * Only supported services will be queried for characteristics.
*
* @param aSupportedService * @param aSupportedService
* @see #getCharacteristic(UUID) * @see #getCharacteristic(UUID)
*/ */
@ -104,6 +106,7 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
/** /**
* Returns the characteristic matching the given UUID. Only characteristics * Returns the characteristic matching the given UUID. Only characteristics
* are returned whose service is marked as supported. * are returned whose service is marked as supported.
*
* @param uuid * @param uuid
* @return the characteristic for the given UUID or <code>null</code> * @return the characteristic for the given UUID or <code>null</code>
* @see #addSupportedService(UUID) * @see #addSupportedService(UUID)
@ -122,7 +125,7 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
return; return;
} }
Set<UUID> supportedServices = getSupportedServices(); Set<UUID> supportedServices = getSupportedServices();
for (BluetoothGattService service : discoveredGattServices) { for (BluetoothGattService service : discoveredGattServices) {
if (supportedServices.contains(service.getUuid())) { if (supportedServices.contains(service.getUuid())) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
@ -146,27 +149,27 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
} }
@Override @Override
public void onServicesDiscovered(BluetoothGatt gatt) { public void onServicesDiscovered(BluetoothGatt gatt) {
gattServicesDiscovered(getQueue().getSupportedGattServices()); gattServicesDiscovered(getQueue().getSupportedGattServices());
} }
@Override @Override
public void onCharacteristicRead(BluetoothGatt gatt, public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) { BluetoothGattCharacteristic characteristic, int status) {
} }
@Override @Override
public void onCharacteristicWrite(BluetoothGatt gatt, public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) { BluetoothGattCharacteristic characteristic, int status) {
} }
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) { BluetoothGattCharacteristic characteristic) {
} }
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
} }
} }

View File

@ -7,7 +7,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
* The Bluedroid implementation only allows performing one GATT request at a time. * The Bluedroid implementation only allows performing one GATT request at a time.
* As they are asynchronous anyway, we encapsulate every GATT request (read and write) * As they are asynchronous anyway, we encapsulate every GATT request (read and write)
* inside a runnable action. * inside a runnable action.
* * <p/>
* These actions are then executed one after another, ensuring that every action's result * These actions are then executed one after another, ensuring that every action's result
* has been posted before invoking the next action. * has been posted before invoking the next action.
*/ */
@ -17,20 +17,22 @@ public abstract class BtLEAction {
public BtLEAction() { public BtLEAction() {
this(null); this(null);
} }
public BtLEAction(BluetoothGattCharacteristic characteristic) { public BtLEAction(BluetoothGattCharacteristic characteristic) {
this.characteristic = characteristic; this.characteristic = characteristic;
} }
public abstract boolean run(BluetoothGatt gatt); public abstract boolean run(BluetoothGatt gatt);
/** /**
* Returns the GATT characteristic being read/written/... * Returns the GATT characteristic being read/written/...
*
* @return the GATT characteristic, or <code>null</code> * @return the GATT characteristic, or <code>null</code>
*/ */
public BluetoothGattCharacteristic getCharacteristic() { public BluetoothGattCharacteristic getCharacteristic() {
return characteristic; return characteristic;
} }
public String toString() { public String toString() {
BluetoothGattCharacteristic characteristic = getCharacteristic(); BluetoothGattCharacteristic characteristic = getCharacteristic();
String uuid = characteristic == null ? "(null)" : characteristic.getUuid().toString(); String uuid = characteristic == null ? "(null)" : characteristic.getUuid().toString();

View File

@ -1,14 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.btle; package nodomain.freeyourgadget.gadgetbridge.btle;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import nodomain.freeyourgadget.gadgetbridge.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
@ -19,69 +10,79 @@ import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import nodomain.freeyourgadget.gadgetbridge.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
/** /**
* One queue/thread per connectable device. * One queue/thread per connectable device.
*/ */
public final class BtLEQueue { public final class BtLEQueue {
private static final String TAG = BtLEQueue.class.getSimpleName(); private static final String TAG = BtLEQueue.class.getSimpleName();
private GBDevice mGbDevice; private GBDevice mGbDevice;
private BluetoothAdapter mBluetoothAdapter; private BluetoothAdapter mBluetoothAdapter;
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 mAbortTransaction; private volatile boolean mAbortTransaction;
private Context mContext; private Context mContext;
private CountDownLatch mWaitForActionResultLatch; private CountDownLatch mWaitForActionResultLatch;
private CountDownLatch mConnectionLatch; private CountDownLatch mConnectionLatch;
private BluetoothGattCharacteristic mWaitCharacteristic; private BluetoothGattCharacteristic mWaitCharacteristic;
private GattCallback mExternalGattCallback; private GattCallback mExternalGattCallback;
private Thread dispatchThread = new Thread("Bluetooth GATT Dispatcher") { private Thread dispatchThread = new Thread("Bluetooth GATT Dispatcher") {
public void run() { public void run() {
while (!mDisposed) { while (!mDisposed) {
try { try {
Transaction transaction = mTransactions.take(); Transaction transaction = mTransactions.take();
if (!isConnected()) { if (!isConnected()) {
// TODO: request connection and initialization from the outside and wait until finished // TODO: request connection and initialization from the outside and wait until finished
// wait until the connection succeeds before running the actions // wait until the connection succeeds before running the actions
// Note that no automatic connection is performed. This has to be triggered // Note that no automatic connection is performed. This has to be triggered
// on the outside typically by the DeviceSupport. The reason is that // on the outside typically by the DeviceSupport. The reason is that
// devices have different kinds of initializations and this class has no // devices have different kinds of initializations and this class has no
// idea about them. // idea about them.
mConnectionLatch = new CountDownLatch(1); mConnectionLatch = new CountDownLatch(1);
mConnectionLatch.await(); mConnectionLatch.await();
mConnectionLatch = null; mConnectionLatch = null;
} }
mAbortTransaction = false; mAbortTransaction = false;
// Run all actions of the transaction until one doesn't succeed // Run all actions of the transaction until one doesn't succeed
for (BtLEAction action : transaction.getActions()) { for (BtLEAction action : transaction.getActions()) {
mWaitCharacteristic = action.getCharacteristic(); mWaitCharacteristic = action.getCharacteristic();
if (action.run(mBluetoothGatt)) { if (action.run(mBluetoothGatt)) {
mWaitForActionResultLatch = new CountDownLatch(1); mWaitForActionResultLatch = new CountDownLatch(1);
mWaitForActionResultLatch.await(); mWaitForActionResultLatch.await();
mWaitForActionResultLatch = null; mWaitForActionResultLatch = null;
if (mAbortTransaction) { if (mAbortTransaction) {
break; break;
} }
} else { } else {
Log.e(TAG, "Action returned false: " + action); Log.e(TAG, "Action returned false: " + action);
break; // abort the transaction break; // abort the transaction
} }
} }
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
mWaitForActionResultLatch = null; mWaitForActionResultLatch = null;
mConnectionLatch = null; mConnectionLatch = null;
} finally { } finally {
mWaitCharacteristic = null; mWaitCharacteristic = null;
} }
} }
Log.i(TAG, "Queue Dispatch Thread terminated."); Log.i(TAG, "Queue Dispatch Thread terminated.");
} }
}; };
public BtLEQueue(BluetoothAdapter bluetoothAdapter, GBDevice gbDevice, GattCallback externalGattCallback, Context context) { public BtLEQueue(BluetoothAdapter bluetoothAdapter, GBDevice gbDevice, GattCallback externalGattCallback, Context context) {
@ -89,10 +90,10 @@ public final class BtLEQueue {
mGbDevice = gbDevice; mGbDevice = gbDevice;
mExternalGattCallback = externalGattCallback; mExternalGattCallback = externalGattCallback;
mContext = context; mContext = context;
dispatchThread.start(); dispatchThread.start();
} }
protected boolean isConnected() { protected boolean isConnected() {
return mGbDevice.isConnected(); return mGbDevice.isConnected();
} }
@ -101,7 +102,7 @@ public final class BtLEQueue {
* Connects to the given remote device. Note that this does not perform any device * 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} * specific initialization. This should be done in the specific {@link DeviceSupport}
* class. * class.
* *
* @return true whether the connection attempt was successfully triggered * @return true whether the connection attempt was successfully triggered
*/ */
public boolean connect() { public boolean connect() {
@ -114,7 +115,7 @@ public final class BtLEQueue {
setDeviceConnectionState(result ? State.CONNECTING : State.NOT_CONNECTED); setDeviceConnectionState(result ? State.CONNECTING : State.NOT_CONNECTED);
return result; return result;
} }
private void setDeviceConnectionState(State newState) { private void setDeviceConnectionState(State newState) {
mGbDevice.setState(newState); mGbDevice.setState(newState);
mGbDevice.sendDeviceUpdateIntent(mContext); mGbDevice.sendDeviceUpdateIntent(mContext);
@ -139,24 +140,25 @@ public final class BtLEQueue {
} }
setDeviceConnectionState(State.NOT_CONNECTED); setDeviceConnectionState(State.NOT_CONNECTED);
} }
public void dispose() { public void dispose() {
if (mDisposed) { if (mDisposed) {
return; return;
} }
mDisposed = true; mDisposed = true;
// try { // try {
disconnect(); disconnect();
dispatchThread.interrupt(); dispatchThread.interrupt();
dispatchThread = null; dispatchThread = null;
// dispatchThread.join(); // dispatchThread.join();
// } catch (InterruptedException ex) { // } catch (InterruptedException ex) {
// Log.e(TAG, "Exception while disposing BtLEQueue", ex); // Log.e(TAG, "Exception while disposing BtLEQueue", ex);
// } // }
} }
/** /**
* Adds a transaction to the end of the queue. * Adds a transaction to the end of the queue.
*
* @param transaction * @param transaction
*/ */
public void add(Transaction transaction) { public void add(Transaction transaction) {
@ -164,11 +166,11 @@ public final class BtLEQueue {
mTransactions.add(transaction); mTransactions.add(transaction);
} }
} }
public void clear() { public void clear() {
mTransactions.clear(); mTransactions.clear();
} }
/** /**
* Retrieves a list of supported GATT services on the connected device. This should be * Retrieves a list of supported GATT services on the connected device. This should be
* invoked only after {@code BluetoothGatt#discoverServices()} completes successfully. * invoked only after {@code BluetoothGatt#discoverServices()} completes successfully.
@ -188,21 +190,21 @@ public final class BtLEQueue {
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) { switch (newState) {
case BluetoothProfile.STATE_CONNECTED: case BluetoothProfile.STATE_CONNECTED:
Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Connected to GATT server.");
setDeviceConnectionState(State.CONNECTED); setDeviceConnectionState(State.CONNECTED);
// Attempts to discover services after successful connection. // Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" + Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices()); mBluetoothGatt.discoverServices());
break; break;
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
Log.i(TAG, "Disconnected from GATT server."); Log.i(TAG, "Disconnected from GATT server.");
handleDisconnected(); handleDisconnected();
break; break;
case BluetoothProfile.STATE_CONNECTING: case BluetoothProfile.STATE_CONNECTING:
Log.i(TAG, "Connecting to GATT server..."); Log.i(TAG, "Connecting to GATT server...");
setDeviceConnectionState(State.CONNECTING); setDeviceConnectionState(State.CONNECTING);
break; break;
} }
} }
@ -223,7 +225,7 @@ public final class BtLEQueue {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "Writing characteristic " + characteristic.getUuid() + " succeeded."); Log.e(TAG, "Writing characteristic " + characteristic.getUuid() + " succeeded.");
} else { } else {
Log.e(TAG, "Writing characteristic " + characteristic.getUuid() + " failed: "+ status); Log.e(TAG, "Writing characteristic " + characteristic.getUuid() + " failed: " + status);
} }
if (mExternalGattCallback != null) { if (mExternalGattCallback != null) {
mExternalGattCallback.onCharacteristicWrite(gatt, characteristic, status); mExternalGattCallback.onCharacteristicWrite(gatt, characteristic, status);
@ -258,7 +260,7 @@ public final class BtLEQueue {
mExternalGattCallback.onReadRemoteRssi(gatt, rssi, status); mExternalGattCallback.onReadRemoteRssi(gatt, rssi, status);
} }
} }
private void checkWaitingCharacteristic(BluetoothGattCharacteristic characteristic, int status) { private void checkWaitingCharacteristic(BluetoothGattCharacteristic characteristic, int status) {
if (status != BluetoothGatt.GATT_SUCCESS) { if (status != BluetoothGatt.GATT_SUCCESS) {
mAbortTransaction = true; mAbortTransaction = true;

View File

@ -28,40 +28,40 @@ import android.bluetooth.BluetoothGattCharacteristic;
public interface GattCallback { public interface GattCallback {
/** /**
* @see BluetoothGattCallback#onConnectionStateChange(BluetoothGatt, int, int)
* @param gatt * @param gatt
* @param status * @param status
* @param newState * @param newState
* @see BluetoothGattCallback#onConnectionStateChange(BluetoothGatt, int, int)
*/ */
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState); public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState);
/** /**
* @see BluetoothGattCallback#onServicesDiscovered(BluetoothGatt, int)
* @param gatt * @param gatt
* @see BluetoothGattCallback#onServicesDiscovered(BluetoothGatt, int)
*/ */
public void onServicesDiscovered(BluetoothGatt gatt); public void onServicesDiscovered(BluetoothGatt gatt);
/** /**
* @see BluetoothGattCallback#onCharacteristicRead(BluetoothGatt, BluetoothGattCharacteristic, int)
* @param gatt * @param gatt
* @param characteristic * @param characteristic
* @param status * @param status
* @see BluetoothGattCallback#onCharacteristicRead(BluetoothGatt, BluetoothGattCharacteristic, int)
*/ */
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status); public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status);
/** /**
* @see BluetoothGattCallback#onCharacteristicWrite(BluetoothGatt, BluetoothGattCharacteristic, int)
* @param gatt * @param gatt
* @param characteristic * @param characteristic
* @param status * @param status
* @see BluetoothGattCallback#onCharacteristicWrite(BluetoothGatt, BluetoothGattCharacteristic, int)
*/ */
public void onCharacteristicWrite(BluetoothGatt gatt, public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status); BluetoothGattCharacteristic characteristic, int status);
/** /**
* @see BluetoothGattCallback#onCharacteristicChanged(BluetoothGatt, BluetoothGattCharacteristic)
* @param gatt * @param gatt
* @param characteristic * @param characteristic
* @see BluetoothGattCallback#onCharacteristicChanged(BluetoothGatt, BluetoothGattCharacteristic)
*/ */
public void onCharacteristicChanged(BluetoothGatt gatt, public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic); BluetoothGattCharacteristic characteristic);
@ -92,10 +92,10 @@ public interface GattCallback {
// public void onReliableWriteCompleted(BluetoothGatt gatt, int status); // public void onReliableWriteCompleted(BluetoothGatt gatt, int status);
/** /**
* @see BluetoothGattCallback#onReadRemoteRssi(BluetoothGatt, int, int)
* @param gatt * @param gatt
* @param rssi * @param rssi
* @param status * @param status
* @see BluetoothGattCallback#onReadRemoteRssi(BluetoothGatt, int, int)
*/ */
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status); public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status);

View File

@ -14,7 +14,7 @@ public class ReadAction extends BtLEAction {
public ReadAction(BluetoothGattCharacteristic characteristic) { public ReadAction(BluetoothGattCharacteristic characteristic) {
super(characteristic); super(characteristic);
} }
@Override @Override
public boolean run(BluetoothGatt gatt) { public boolean run(BluetoothGatt gatt) {
return gatt.readCharacteristic(getCharacteristic()); return gatt.readCharacteristic(getCharacteristic());

View File

@ -8,7 +8,7 @@ import java.util.Locale;
/** /**
* Groups a bunch of {@link BtLEAction actions} together, making sure * Groups a bunch of {@link BtLEAction actions} together, making sure
* that upon failure of one action, all subsequent actions are discarded. * that upon failure of one action, all subsequent actions are discarded.
* *
* @author TREND * @author TREND
*/ */
public class Transaction { public class Transaction {
@ -18,19 +18,19 @@ public class Transaction {
public Transaction(String taskName) { public Transaction(String taskName) {
this.mName = taskName; this.mName = taskName;
} }
public String getTaskName() { public String getTaskName() {
return mName; return mName;
} }
public void add(BtLEAction action) { public void add(BtLEAction action) {
mActions.add(action); mActions.add(action);
} }
public List<BtLEAction> getActions() { public List<BtLEAction> getActions() {
return Collections.unmodifiableList(mActions); return Collections.unmodifiableList(mActions);
} }
public boolean isEmpty() { public boolean isEmpty() {
return mActions.isEmpty(); return mActions.isEmpty();
} }

View File

@ -7,11 +7,11 @@ public class TransactionBuilder {
private static final String TAG = TransactionBuilder.class.getSimpleName(); private static final String TAG = TransactionBuilder.class.getSimpleName();
private Transaction mTransaction; private Transaction mTransaction;
public TransactionBuilder(String taskName) { public TransactionBuilder(String taskName) {
mTransaction = new Transaction(taskName); mTransaction = new Transaction(taskName);
} }
public TransactionBuilder read(BluetoothGattCharacteristic characteristic) { public TransactionBuilder read(BluetoothGattCharacteristic characteristic) {
if (characteristic == null) { if (characteristic == null) {
Log.w(TAG, "Unable to read characteristic: null"); Log.w(TAG, "Unable to read characteristic: null");
@ -20,7 +20,7 @@ public class TransactionBuilder {
ReadAction action = new ReadAction(characteristic); ReadAction action = new ReadAction(characteristic);
return add(action); return add(action);
} }
public TransactionBuilder write(BluetoothGattCharacteristic characteristic, byte[] data) { public TransactionBuilder write(BluetoothGattCharacteristic characteristic, byte[] data) {
if (characteristic == null) { if (characteristic == null) {
Log.w(TAG, "Unable to write characteristic: null"); Log.w(TAG, "Unable to write characteristic: null");
@ -29,19 +29,20 @@ public class TransactionBuilder {
WriteAction action = new WriteAction(characteristic, data); WriteAction action = new WriteAction(characteristic, data);
return add(action); return add(action);
} }
public TransactionBuilder wait(int millis) { public TransactionBuilder wait(int millis) {
WaitAction action = new WaitAction(millis); WaitAction action = new WaitAction(millis);
return add(action); return add(action);
} }
public TransactionBuilder add(BtLEAction action) { public TransactionBuilder add(BtLEAction action) {
mTransaction.add(action); mTransaction.add(action);
return this; return this;
} }
/** /**
* To be used as the final step to execute the transaction by the given queue. * To be used as the final step to execute the transaction by the given queue.
*
* @param queue * @param queue
*/ */
public void queue(BtLEQueue queue) { public void queue(BtLEQueue queue) {

View File

@ -7,7 +7,7 @@ public class WaitAction extends BtLEAction {
private int mMillis; private int mMillis;
public WaitAction(int millis) { public WaitAction(int millis) {
mMillis = millis; mMillis = millis;
} }
@Override @Override

View File

@ -17,7 +17,7 @@ public class WriteAction extends BtLEAction {
super(characteristic); super(characteristic);
this.value = value; this.value = value;
} }
@Override @Override
public boolean run(BluetoothGatt gatt) { public boolean run(BluetoothGatt gatt) {
if (getCharacteristic().setValue(value)) { if (getCharacteristic().setValue(value)) {

View File

@ -184,7 +184,7 @@ public class MiBandService {
*/ */
private static Map<UUID,String> MIBAND_DEBUG; private static Map<UUID, String> MIBAND_DEBUG;
static { static {
MIBAND_DEBUG = new HashMap<>(); MIBAND_DEBUG = new HashMap<>();

View File

@ -1,14 +1,15 @@
package nodomain.freeyourgadget.gadgetbridge.miband; package nodomain.freeyourgadget.gadgetbridge.miband;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBCommand; import nodomain.freeyourgadget.gadgetbridge.GBCommand;
import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;
public class MiBandSupport extends AbstractBTLEDeviceSupport { public class MiBandSupport extends AbstractBTLEDeviceSupport {
@ -28,7 +29,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
public boolean useAutoConnect() { public boolean useAutoConnect() {
return true; return true;
} }
private byte[] getDefaultNotification() { private byte[] getDefaultNotification() {
final int vibrateTimes = 1; final int vibrateTimes = 1;
final long vibrateDuration = 250l; final long vibrateDuration = 250l;
@ -47,7 +48,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} }
private byte[] getNotification(long vibrateDuration, int vibrateTimes, int flashTimes, int flashColour, int originalColour, long flashDuration) { private byte[] getNotification(long vibrateDuration, int vibrateTimes, int flashTimes, int flashColour, int originalColour, long flashDuration) {
byte[] vibrate = new byte[]{ (byte) 8, (byte) 1 }; byte[] vibrate = new byte[]{(byte) 8, (byte) 1};
byte r = 6; byte r = 6;
byte g = 0; byte g = 0;
byte b = 6; byte b = 6;
@ -72,6 +73,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
/** /**
* Part of device initialization process. Do not call manually. * Part of device initialization process. Do not call manually.
*
* @param builder * @param builder
* @return * @return
*/ */
@ -84,6 +86,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
/** /**
* Part of device initialization process. Do not call manually. * Part of device initialization process. Do not call manually.
*
* @param builder * @param builder
* @return * @return
*/ */
@ -161,7 +164,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onCharacteristicWrite(BluetoothGatt gatt, public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) { BluetoothGattCharacteristic characteristic, int status) {
UUID characteristicUUID = characteristic.getUuid(); UUID characteristicUUID = characteristic.getUuid();
if (MiBandService.UUID_CHARACTERISTIC_PAIR.equals(characteristicUUID)) { if (MiBandService.UUID_CHARACTERISTIC_PAIR.equals(characteristicUUID)) {
handlePairResult(characteristic.getValue(), status); handlePairResult(characteristic.getValue(), status);

View File

@ -17,6 +17,7 @@ public class UserInfo {
/** /**
* Creates a default user info. * Creates a default user info.
*
* @param btAddress the address of the MI Band to connect to. * @param btAddress the address of the MI Band to connect to.
*/ */
public static UserInfo getDefault(String btAddress) { public static UserInfo getDefault(String btAddress) {
@ -25,6 +26,7 @@ public class UserInfo {
/** /**
* Creates a user info with the given data * Creates a user info with the given data
*
* @param address the address of the MI Band to connect to. * @param address the address of the MI Band to connect to.
*/ */
public UserInfo(String address, String alias, int gender, int age, int height, int weight, int type) { public UserInfo(String address, String alias, int gender, int age, int height, int weight, int type) {
@ -53,7 +55,7 @@ public class UserInfo {
sequence[8] = (byte) (type & 0xff); sequence[8] = (byte) (type & 0xff);
for (int u = 9; u < 19; u++) for (int u = 9; u < 19; u++)
sequence[u] = alias.getBytes()[u-9]; sequence[u] = alias.getBytes()[u - 9];
byte[] crcSequence = new byte[19]; byte[] crcSequence = new byte[19];
for (int u = 0; u < crcSequence.length; u++) for (int u = 0; u < crcSequence.length; u++)
@ -80,7 +82,7 @@ public class UserInfo {
sum = (byte) ((sum & 0xff) & 0x01); sum = (byte) ((sum & 0xff) & 0x01);
crc = (byte) ((crc & 0xff) >>> 1); crc = (byte) ((crc & 0xff) >>> 1);
if (sum != 0) { if (sum != 0) {
crc = (byte)((crc & 0xff) ^ 0x8c); crc = (byte) ((crc & 0xff) ^ 0x8c);
} }
extract = (byte) ((extract & 0xff) >>> 1); extract = (byte) ((extract & 0xff) >>> 1);
} }

View File

@ -25,7 +25,7 @@ public class PebbleSupport extends AbstractBTDeviceSupport {
protected GBDeviceIoThread createDeviceIOThread() { protected GBDeviceIoThread createDeviceIOThread() {
return new PebbleIoThread(getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext()); return new PebbleIoThread(getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext());
} }
@Override @Override
public boolean useAutoConnect() { public boolean useAutoConnect() {
return false; return false;