-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
request control and reset
- Loading branch information
Showing
13 changed files
with
383 additions
and
243 deletions.
There are no files selected for viewing
73 changes: 0 additions & 73 deletions
73
app/src/main/java/svenmeier/coxswain/bluetooth/BlueUtils.java
This file was deleted.
Oops, something went wrong.
160 changes: 160 additions & 0 deletions
160
app/src/main/java/svenmeier/coxswain/bluetooth/BlueWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package svenmeier.coxswain.bluetooth; | ||
|
||
import android.annotation.TargetApi; | ||
import android.bluetooth.BluetoothGatt; | ||
import android.bluetooth.BluetoothGattCallback; | ||
import android.bluetooth.BluetoothGattCharacteristic; | ||
import android.bluetooth.BluetoothGattDescriptor; | ||
import android.bluetooth.BluetoothGattService; | ||
import android.os.Build; | ||
import android.support.annotation.CallSuper; | ||
import android.util.Log; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
import java.util.UUID; | ||
|
||
import svenmeier.coxswain.Coxswain; | ||
|
||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
public class BlueWriter extends BluetoothGattCallback { | ||
|
||
public static final UUID SERVICE_HEART_RATE = uuid(0x180D); | ||
public static final UUID CHARACTERISTIC_HEART_RATE_MEASUREMENT = uuid(0x2A37); | ||
|
||
public static final UUID SERVICE_DEVICE_INFORMATION = uuid(0x180A); | ||
public static final UUID CHARACTERISTIC_SOFTWARE_REVISION = uuid(0x2A28); | ||
|
||
public static final UUID SERVICE_FITNESS_MACHINE = uuid(0x1826); | ||
public static final UUID CHARACTERISTIC_ROWER_DATA = uuid(0x2AD1); | ||
public static final UUID CHARACTERISTIC_CONTROL_POINT = uuid(0x2AD9); | ||
|
||
public static final UUID CLIENT_CHARACTERISTIC_DESCIPRTOR = uuid(0x2902); | ||
|
||
private Queue<Request> requests = new ArrayDeque<>(); | ||
|
||
private Request current = null; | ||
|
||
private interface Request { | ||
|
||
void request(); | ||
} | ||
|
||
private void request(Request request) { | ||
requests.add(request); | ||
|
||
requestNext(); | ||
} | ||
|
||
private void requestNext() { | ||
if (current != null) { | ||
return; | ||
} | ||
|
||
current = requests.poll(); | ||
if (current != null) { | ||
current.request(); | ||
} | ||
} | ||
|
||
public BluetoothGattCharacteristic get(BluetoothGatt gatt, UUID service, UUID characteristic) { | ||
BluetoothGattService s = gatt.getService(service); | ||
if (s == null) { | ||
return null; | ||
} | ||
|
||
BluetoothGattCharacteristic c = s.getCharacteristic(characteristic); | ||
return c; | ||
} | ||
|
||
@CallSuper | ||
@Override | ||
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { | ||
if (current != null) { | ||
current = null; | ||
requestNext(); | ||
} | ||
} | ||
|
||
@CallSuper | ||
@Override | ||
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { | ||
if (current != null) { | ||
current = null; | ||
requestNext(); | ||
} | ||
} | ||
|
||
@CallSuper | ||
@Override | ||
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { | ||
if (current != null) { | ||
current = null; | ||
requestNext(); | ||
} | ||
} | ||
|
||
public void enableNotification(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { | ||
|
||
request(new Request() { | ||
@Override | ||
public void request() { | ||
gatt.setCharacteristicNotification(characteristic, true); | ||
|
||
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_DESCIPRTOR); | ||
if (descriptor != null) { | ||
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); | ||
if (gatt.writeDescriptor(descriptor) == false) { | ||
Log.e(Coxswain.TAG, "bluetooth enabled notification failed"); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void enableIndication(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { | ||
|
||
request(new Request() { | ||
@Override | ||
public void request() { | ||
gatt.setCharacteristicNotification(characteristic, true); | ||
|
||
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_DESCIPRTOR); | ||
if (descriptor != null) { | ||
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); | ||
if (gatt.writeDescriptor(descriptor) == false) { | ||
Log.e(Coxswain.TAG, "bluetooth enabled indication failed"); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void write(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final byte value) { | ||
|
||
request(new Request() { | ||
@Override | ||
public void request() { | ||
characteristic.setValue(value, BluetoothGattCharacteristic.FORMAT_UINT8, 0); | ||
if (gatt.writeCharacteristic(characteristic) == false) { | ||
Log.e(Coxswain.TAG, "bluetooth write failed"); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void read(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { | ||
request(new Request() { | ||
@Override | ||
public void request() { | ||
if (gatt.readCharacteristic(characteristic) == false) { | ||
Log.e(Coxswain.TAG, "bluetooth read failed"); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public static final UUID uuid(int id) { | ||
return UUID.fromString(String.format("%08X-0000-1000-8000-00805f9b34fb", id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.