Skip to content

Commit

Permalink
version 6.8
Browse files Browse the repository at this point in the history
request control and reset
  • Loading branch information
svenmeier committed Feb 11, 2020
1 parent 04b1f78 commit e752229
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 243 deletions.
73 changes: 0 additions & 73 deletions app/src/main/java/svenmeier/coxswain/bluetooth/BlueUtils.java

This file was deleted.

160 changes: 160 additions & 0 deletions app/src/main/java/svenmeier/coxswain/bluetooth/BlueWriter.java
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));
}
}
50 changes: 23 additions & 27 deletions app/src/main/java/svenmeier/coxswain/bluetooth/BluetoothHeart.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void open() {
}

String name = context.getString(R.string.bluetooth_heart);
IntentFilter filter = BluetoothActivity.start(context, name, BlueUtils.SERVICE_HEART_RATE.toString());
IntentFilter filter = BluetoothActivity.start(context, name, BlueWriter.SERVICE_HEART_RATE.toString());
context.registerReceiver(this, filter);
registered = true;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public void close()
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private class GattConnection extends BluetoothGattCallback implements Connection, Runnable {
private class GattConnection extends BlueWriter implements Connection, Runnable {

private final String address;

Expand Down Expand Up @@ -380,31 +380,25 @@ public synchronized void close() {
}

@Override
public synchronized void onServicesDiscovered(BluetoothGatt gatt, int status) {
public synchronized void onServicesDiscovered(final BluetoothGatt gatt, int status) {
if (connected == null) {
return;
}

BluetoothGattService service = gatt.getService(BlueUtils.SERVICE_HEART_RATE);
if (service == null) {
Log.d(Coxswain.TAG, "bluetooth no heart rate");
heartRateMeasurement = get(gatt, SERVICE_HEART_RATE, CHARACTERISTIC_HEART_RATE_MEASUREMENT);
if (heartRateMeasurement == null) {
Log.d(Coxswain.TAG, "bluetooth no heart rate measurement");
} else {
heartRateMeasurement = service.getCharacteristic(BlueUtils.CHARACTERISTIC_HEART_RATE_MEASUREMENT);
if (heartRateMeasurement == null) {
Log.d(Coxswain.TAG, "bluetooth no heart rate measurement");
} else {
if (BlueUtils.enableNotification(gatt, heartRateMeasurement)) {
toast(context.getString(R.string.bluetooth_heart_connected, gatt.getDevice().getAddress()));
return;
}
Log.d(Coxswain.TAG, "bluetooth no heart rate measurement notification");
}
enableNotification(gatt, heartRateMeasurement);
}

heartRateMeasurement = null;
toast(context.getString(R.string.bluetooth_heart_not_found, gatt.getDevice().getAddress()));
if (heartRateMeasurement == null) {
toast(context.getString(R.string.bluetooth_heart_not_found, gatt.getDevice().getAddress()));

select();
select();
} else {
toast(context.getString(R.string.bluetooth_heart_connected, gatt.getDevice().getAddress()));
}
}

@Override
Expand All @@ -414,15 +408,17 @@ public synchronized void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGa
return;
}

int heartRate;
Fields fields = new Fields(characteristic, Fields.UINT8);
if (fields.flag(0)) {
heartRate = fields.get(Fields.UINT16);
} else {
heartRate = fields.get(Fields.UINT8);
}
if (characteristic.getUuid().equals(heartRateMeasurement.getUuid())) {
int heartRate;
Fields fields = new Fields(characteristic, Fields.UINT8);
if (fields.flag(0)) {
heartRate = fields.get(Fields.UINT16);
} else {
heartRate = fields.get(Fields.UINT8);
}

onHeartRate(heartRate);
onHeartRate(heartRate);
}
}

/**
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/svenmeier/coxswain/bluetooth/Fields.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Build;

import java.util.Calendar;

import svenmeier.coxswain.bluetooth.BlueUtils;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class Fields {

Expand Down
Loading

0 comments on commit e752229

Please sign in to comment.