First functioning version of BLE Datasource - runs ok with BangleJs OpenSeizure Widget

This commit is contained in:
Graham Jones
2020-08-11 21:22:49 +01:00
parent 8b46e1b42d
commit 4bafd15d7e
4 changed files with 66 additions and 12 deletions

View File

@@ -0,0 +1,20 @@
{
"version": 1,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "uk.org.openseizuredetector",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"properties": [],
"versionCode": 72,
"versionName": "3.4.0",
"enabled": true,
"outputFile": "app-release.apk"
}
]
}

View File

@@ -57,8 +57,8 @@ public abstract class SdDataSource {
private Timer mStatusTimer; private Timer mStatusTimer;
private Timer mSettingsTimer; private Timer mSettingsTimer;
private Timer mFaultCheckTimer; private Timer mFaultCheckTimer;
private Time mDataStatusTime; protected Time mDataStatusTime;
private boolean mWatchAppRunningCheck = false; protected boolean mWatchAppRunningCheck = false;
private int mAppRestartTimeout = 10; // Timeout before re-starting watch app (sec) if we have not received private int mAppRestartTimeout = 10; // Timeout before re-starting watch app (sec) if we have not received
// data after mDataUpdatePeriod // data after mDataUpdatePeriod
private int mFaultTimerPeriod = 30; // Fault Timer Period in sec private int mFaultTimerPeriod = 30; // Fault Timer Period in sec
@@ -339,7 +339,7 @@ public abstract class SdDataSource {
* doAnalysis() - analyse the data if the accelerometer data array mAccData * doAnalysis() - analyse the data if the accelerometer data array mAccData
* and populate the output data structure mSdData * and populate the output data structure mSdData
*/ */
private void doAnalysis() { protected void doAnalysis() {
// FIXME - Use specified sampleFreq, not this hard coded one // FIXME - Use specified sampleFreq, not this hard coded one
mSampleFreq = 25; mSampleFreq = 25;
double freqRes = 1.0*mSampleFreq/mSdData.mNsamp; double freqRes = 1.0*mSampleFreq/mSdData.mNsamp;

View File

@@ -36,6 +36,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log; import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
@@ -52,6 +53,7 @@ import java.util.UUID;
* waits to be notified of data being available. * waits to be notified of data being available.
*/ */
public class SdDataSourceBLE extends SdDataSource { public class SdDataSourceBLE extends SdDataSource {
private int MAX_RAW_DATA = 125; // 5 seconds at 25 Hz.
private String TAG = "SdDataSourceBLE"; private String TAG = "SdDataSourceBLE";
private BluetoothManager mBluetoothManager; private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter; private BluetoothAdapter mBluetoothAdapter;
@@ -59,6 +61,9 @@ public class SdDataSourceBLE extends SdDataSource {
private BluetoothGatt mBluetoothGatt; private BluetoothGatt mBluetoothGatt;
private int mConnectionState = STATE_DISCONNECTED; private int mConnectionState = STATE_DISCONNECTED;
private int nRawData = 0;
private double[] rawData = new double[MAX_RAW_DATA];
private static final int STATE_DISCONNECTED = 0; private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1; private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2; private static final int STATE_CONNECTED = 2;
@@ -77,11 +82,11 @@ public class SdDataSourceBLE extends SdDataSource {
public static String SERV_DEV_INFO = "0000180a-0000-1000-8000-00805f9b34fb"; public static String SERV_DEV_INFO = "0000180a-0000-1000-8000-00805f9b34fb";
public static String SERV_HEART_RATE = "0000180d-0000-1000-8000-00805f9b34fb"; public static String SERV_HEART_RATE = "0000180d-0000-1000-8000-00805f9b34fb";
public static String SERV_OSD = "xxxxxxxxxxxxxxxxxxxx"; public static String SERV_OSD = "a19585e9-0001-39d0-015f-b3e2b9a0c854";
public static String CHAR_HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb"; public static String CHAR_HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb";
public static String CHAR_MANUF_NAME = "00002a29-0000-1000-8000-00805f9b34fb"; public static String CHAR_MANUF_NAME = "00002a29-0000-1000-8000-00805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"; public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
public static String CHAR_OSD_ACC_DATA = "xxxxxxxxxxxxxxxxxx"; public static String CHAR_OSD_ACC_DATA = "a19585e9-0002-39d0-015f-b3e2b9a0c854";
public final static UUID UUID_HEART_RATE_MEASUREMENT = UUID.fromString(CHAR_HEART_RATE_MEASUREMENT); public final static UUID UUID_HEART_RATE_MEASUREMENT = UUID.fromString(CHAR_HEART_RATE_MEASUREMENT);
@@ -234,6 +239,7 @@ public class SdDataSourceBLE extends SdDataSource {
public void onDataReceived(BluetoothGattCharacteristic characteristic) { public void onDataReceived(BluetoothGattCharacteristic characteristic) {
// FIXME - collect data until we have enough to do analysis, then use onDataReceived to process it. // FIXME - collect data until we have enough to do analysis, then use onDataReceived to process it.
//Log.v(TAG,"onDataReceived: Characteristic="+characteristic.getUuid().toString());
if (characteristic.getUuid().toString().equals(CHAR_HEART_RATE_MEASUREMENT)) { if (characteristic.getUuid().toString().equals(CHAR_HEART_RATE_MEASUREMENT)) {
int flag = characteristic.getProperties(); int flag = characteristic.getProperties();
int format = -1; int format = -1;
@@ -247,8 +253,38 @@ public class SdDataSourceBLE extends SdDataSource {
final int heartRate = characteristic.getIntValue(format, 1); final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate)); Log.d(TAG, String.format("Received heart rate: %d", heartRate));
} }
if (characteristic.getUuid().toString().equals(CHAR_OSD_ACC_DATA)) { else if (characteristic.getUuid().toString().equals(CHAR_OSD_ACC_DATA)) {
Log.v(TAG,"Received OSD ACC DATA"); //Log.v(TAG,"Received OSD ACC DATA"+characteristic.getValue());
byte[] rawDataBytes = characteristic.getValue();
Log.v(TAG, "CHAR_OSD_ACC_DATA: numSamples = " + rawDataBytes.length+" nRawData="+nRawData);
for (int i = 0; i < rawDataBytes.length;i++) {
if (nRawData < MAX_RAW_DATA) {
rawData[nRawData] = 1000 * rawDataBytes[i] / 64; // Scale to mg
nRawData++;
} else {
Log.i(TAG, "RawData Buffer Full - processing data");
// FIXME - Create JSON object to send to the SdDataSource analysis routine onDataReceived().
// Re-start collecting raw data.
mSdData.watchAppRunning = true;
for (i = 0; i < rawData.length; i++) {
mSdData.rawData[i] = rawData[i];
}
mSdData.mNsamp = rawData.length;
//mNSamp = accelVals.length();
mWatchAppRunningCheck = true;
mDataStatusTime = new Time(Time.getCurrentTimezone());
// FIXME - A fiddle to make the startup activity close - really need to send battery percentage every now and then.
mSdData.haveSettings = true;
doAnalysis();
nRawData = 0;
}
}
}
else {
Log.v(TAG,"Unrecognised Characteristic Updated "+
characteristic.getUuid().toString());
} }
} }
@@ -265,7 +301,7 @@ public class SdDataSourceBLE extends SdDataSource {
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) { BluetoothGattCharacteristic characteristic) {
Log.v(TAG,"Characteristic "+characteristic.getUuid()+" changed"); Log.v(TAG,"onCharacteristicChanged(): Characteristic "+characteristic.getUuid()+" changed");
onDataReceived(characteristic); onDataReceived(characteristic);
} }
}; };
@@ -286,15 +322,13 @@ public class SdDataSourceBLE extends SdDataSource {
Log.v(TAG,"setCharacteristicNotification - Requesting notifications"); Log.v(TAG,"setCharacteristicNotification - Requesting notifications");
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement. // Tell the device we want notifications? The sample from Google said we only need this for Heart Rate, but the
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { // BangleJS widget did not work without it so do it for everything.
Log.v(TAG,"setCharacteristicNotification - running extra code for heart rate");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor( BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor); mBluetoothGatt.writeDescriptor(descriptor);
} }
}
/** /**
* 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