Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpMaster committed Jul 24, 2016
1 parent 4151efe commit 9f11de1
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 223 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.cooper.wheellog"
minSdkVersion 18
targetSdkVersion 24
versionCode 2
versionName "0.9"
versionCode 6
versionName "1.0.1"
}
buildTypes {
release {
Expand All @@ -22,7 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.getpebble:pebblekit:3.0.0'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:gridlayout-v7:24.0.0'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support:gridlayout-v7:24.1.1'
}
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
</intent-filter>
</activity>
<activity android:name=".ScanActivity" android:theme="@style/AppTheme.Transparent" />
<service android:name=".PebbleConnectivity" />
<service android:name=".PebbleService" />
<service android:name=".BluetoothLeService" />
<service android:name=".DataLogger" />
<service android:name=".LoggingService" />

<receiver
android:name="PebbleBroadcastReceiver"
Expand Down
78 changes: 39 additions & 39 deletions app/src/main/java/com/cooper/wheellog/BluetoothLeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ public void onReceive(Context context, Intent intent) {
((WheelLog) getApplicationContext()).setConnectionState(false);
updateNotification(R.string.wheel_disconnected);
} else if (Constants.ACTION_BLUETOOTH_CONNECTING.equals(action)) {
((WheelLog) getApplicationContext()).setConnectionState(false);
mConnectionState = STATE_CONNECTING;
if (intent.hasExtra(Constants.INTENT_EXTRA_BLE_AUTO_CONNECT))
updateNotification(R.string.wheel_searching);
else
updateNotification(R.string.wheel_connecting);

} else if (Constants.ACTION_REQUEST_NAME_DATA.equals(action)) {
} else if (Constants.ACTION_REQUEST_KINGSONG_NAME_DATA.equals(action)) {

byte[] data = new byte[20];
data[0] = (byte) -86;
Expand All @@ -78,7 +79,7 @@ public void onReceive(Context context, Intent intent) {
data[18] = (byte) 90;
data[19] = (byte) 90;
writeBluetoothGattCharacteristic(data);
} else if (Constants.ACTION_REQUEST_SERIAL_DATA.equals(action)) {
} else if (Constants.ACTION_REQUEST_KINGSONG_SERIAL_DATA.equals(action)) {

byte[] data = new byte[20];
data[0] = (byte) -86;
Expand All @@ -88,7 +89,7 @@ public void onReceive(Context context, Intent intent) {
data[18] = (byte) 90;
data[19] = (byte) 90;
writeBluetoothGattCharacteristic(data);
} else if (Constants.ACTION_REQUEST_HORN.equals(action)) {
} else if (Constants.ACTION_REQUEST_KINGSONG_HORN.equals(action)) {

byte[] data = new byte[20];
data[0] = (byte) -86;
Expand All @@ -102,16 +103,19 @@ public void onReceive(Context context, Intent intent) {
}
};

public static IntentFilter makeBluetoothUpdateIntentFilter() {
public static IntentFilter makeIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Constants.ACTION_BLUETOOTH_CONNECTING);
intentFilter.addAction(Constants.ACTION_BLUETOOTH_CONNECTED);
intentFilter.addAction(Constants.ACTION_BLUETOOTH_DISCONNECTED);
intentFilter.addAction(Constants.ACTION_BLUETOOTH_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_REQUEST_SERIAL_DATA);
intentFilter.addAction(Constants.ACTION_REQUEST_NAME_DATA);
intentFilter.addAction(Constants.ACTION_REQUEST_HORN);
intentFilter.addAction(Constants.ACTION_REQUEST_KINGSONG_SERIAL_DATA);
intentFilter.addAction(Constants.ACTION_REQUEST_KINGSONG_NAME_DATA);
intentFilter.addAction(Constants.ACTION_REQUEST_KINGSONG_HORN);
intentFilter.addAction(Constants.ACTION_LOGGING_SERVICE_STARTED);
intentFilter.addAction(Constants.ACTION_PEBBLE_SERVICE_STARTED);
intentFilter.addAction(Constants.ACTION_WHEEL_TYPE_DEFINED);
return intentFilter;
}

Expand Down Expand Up @@ -158,29 +162,31 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
LOGI("onServicesDiscovered called");
if (status == BluetoothGatt.GATT_SUCCESS) {
LOGI("onServicesDiscovered called, status == BluetoothGatt.GATT_SUCCESS");
BluetoothGattService targetService = gatt.getService(UUID.fromString(Constants.KINGSONG_SERVICE_UUID));
if (targetService == null) {

BluetoothGattService targetService;
targetService = gatt.getService(UUID.fromString(Constants.KINGSONG_SERVICE_UUID));
if (targetService != null) {
((WheelLog) getApplicationContext()).setWheelType(Constants.WHEEL_TYPE_KINGSONG);
broadcastUpdate(Constants.ACTION_WHEEL_TYPE_DEFINED);
} else {
LOGI("targetService == null");
disconnect();
return;
}
BluetoothGattCharacteristic characteristic = targetService.getCharacteristic(UUID.fromString(Constants.KINGSONG_READ_CHARACTER_UUID));
BluetoothGattCharacteristic writeCharacteristic = targetService.getCharacteristic(UUID.fromString(Constants.KINGSONG_READ_CHARACTER_UUID));
if (writeCharacteristic == null) {
LOGI("not found target notify writecharacter");
return;
}
LOGI("writeCharacteristic write =" + writeCharacteristic.getWriteType());
if (characteristic == null) {
LOGI("not found target notify character");

BluetoothGattCharacteristic notifyCharacteristic = targetService.getCharacteristic(UUID.fromString(Constants.KINGSONG_NOTITY_CHARACTER_UUID));
if (notifyCharacteristic == null) {
LOGI("not found target notify notifyCharacteristic");
disconnect();
return;
}
LOGI("writeCharacteristic write =" + writeCharacteristic.getWriteType());
LOGI("found target notify character");
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(Constants.KINGSONG_DESCRIPTER_UUID));
gatt.setCharacteristicNotification(notifyCharacteristic, true);
BluetoothGattDescriptor descriptor = notifyCharacteristic.getDescriptor(UUID.fromString(Constants.KINGSONG_DESCRIPTER_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
mConnectionState = STATE_CONNECTED;
broadcastUpdate(Constants.ACTION_BLUETOOTH_CONNECTED);
return;
}
LOGI("onServicesDiscovered called, status == BluetoothGatt.GATT_FAILURE");
Expand All @@ -190,28 +196,19 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
LOGI("onCharacteristicChanged called " + characteristic.getUuid().toString());
if (characteristic.getUuid().toString().equals(Constants.KINGSONG_READ_CHARACTER_UUID)) {
byte[] value = characteristic.getValue();
// StringBuilder stringBuilder = new StringBuilder(20);
// for (int i = 0; i < value.length; i += 1) {
// Object[] objArr = new Object[1];
// objArr[0] = Byte.valueOf(value[i]);
// stringBuilder.append(String.format("%02X ", objArr));
// }
// LOGI("received data = " + stringBuilder);
((WheelLog) getApplicationContext()).decodeResponse(value);

Intent intent = new Intent(Constants.ACTION_WHEEL_DATA_AVAILABLE);
sendBroadcast(intent);

if (((WheelLog) getApplicationContext()).getWheelType() == Constants.WHEEL_TYPE_KINGSONG) {
if (characteristic.getUuid().toString().equals(Constants.KINGSONG_READ_CHARACTER_UUID)) {
byte[] value = characteristic.getValue();
((WheelLog) getApplicationContext()).decodeResponse(value);
}
}
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
LOGI("onDescriptorWrite " + String.valueOf(status));
if (status == BluetoothGatt.GATT_SUCCESS)
broadcastUpdate(Constants.ACTION_BLUETOOTH_CONNECTED);
}
};

Expand Down Expand Up @@ -259,7 +256,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

Notification notification = buildNotification(R.string.wheel_disconnected);

registerReceiver(messageReceiver, makeBluetoothUpdateIntentFilter());
registerReceiver(messageReceiver, makeIntentFilter());
startForeground(notification_id, notification);

return START_STICKY;
Expand All @@ -268,9 +265,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
super.onDestroy();
if (mBluetoothGatt != null &&
mConnectionState != STATE_DISCONNECTED)
mBluetoothGatt.disconnect();
close();
unregisterReceiver(messageReceiver);
stopForeground(true);
close();
}

private final IBinder mBinder = new LocalBinder();
Expand Down Expand Up @@ -336,7 +336,6 @@ public boolean connect(final String address) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}

mBluetoothGatt = device.connectGatt(this, autoConnect, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
Expand All @@ -360,6 +359,7 @@ public void disconnect() {
if (mConnectionState != STATE_CONNECTED)
mConnectionState = STATE_DISCONNECTED;
mBluetoothGatt.disconnect();
broadcastUpdate(Constants.ACTION_BLUETOOTH_DISCONNECTED);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/com/cooper/wheellog/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ public class Constants {
public static final String ACTION_BLUETOOTH_DISCONNECTED = "com.cooper.wheellog.bluetoothDisconnected";
public static final String ACTION_BLUETOOTH_DATA_AVAILABLE = "com.cooper.wheellog.bluetoothDataAvailable";
public static final String ACTION_WHEEL_DATA_AVAILABLE = "com.cooper.wheellog.wheelDataAvailable";
public static final String ACTION_REQUEST_SERIAL_DATA = "com.cooper.wheellog.requestSerialData";
public static final String ACTION_REQUEST_NAME_DATA = "com.cooper.wheellog.requestNameData";
public static final String ACTION_REQUEST_HORN = "com.cooper.wheellog.requestHorn";
// public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
// public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";
public static final String ACTION_REQUEST_KINGSONG_SERIAL_DATA = "com.cooper.wheellog.requestSerialData";
public static final String ACTION_REQUEST_KINGSONG_NAME_DATA = "com.cooper.wheellog.requestNameData";
public static final String ACTION_REQUEST_KINGSONG_HORN = "com.cooper.wheellog.requestHorn";
public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";
public static final String ACTION_WHEEL_TYPE_DEFINED = "com.cooper.wheellog.wheelTypeDefined";

public static final String KINGSONG_DESCRIPTER_UUID = "00002902-0000-1000-8000-00805f9b34fb";
public static final String KINGSONG_READ_CHARACTER_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";
public static final String KINGSONG_NOTITY_CHARACTER_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";
public static final String KINGSONG_SERVICE_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";

// public static final String GOTWAY_DESCRIPTER_UUID = "00002902-0000-1000-8000-00805f9b34fb"; //UPDATED
Expand All @@ -29,7 +31,8 @@ public class Constants {
public static final int PEBBLE_KEY_PLAY_HORN_MP3 = 10010;

public static final String INTENT_EXTRA_LAUNCHED_FROM_PEBBLE = "launched_from_pebble";
public static final String INTENT_EXTRA_BLE_AUTO_CONNECT = "direct_ble_connection";
public static final String INTENT_EXTRA_BLE_AUTO_CONNECT = "ble_auto_connect";
public static final String INTENT_EXTRA_LOGGING_FILE_LOCATION = "logging_file_location";

public static final int REQUEST_SERIAL_DATA = 10;
public static final int WHEEL_TYPE_KINGSONG = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
Expand All @@ -19,11 +20,11 @@
import java.util.Date;
import java.util.Locale;

public class DataLogger extends Service
public class LoggingService extends Service
{
private static final boolean DEBUG = false;
private final static String TAG = DataLogger.class.getSimpleName();
private static DataLogger instance = null;
private final static String TAG = LoggingService.class.getSimpleName();
private static LoggingService instance = null;
SimpleDateFormat sdf;
private boolean fileExists;
private File file;
Expand Down Expand Up @@ -59,7 +60,7 @@ public IBinder onBind(Intent intent) {
public int onStartCommand(Intent intent, int flags, int startId) {
instance = this;
sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss.SSS", Locale.US);
registerReceiver(mBluetoothUpdateReceiver, BluetoothLeService.makeBluetoothUpdateIntentFilter());
registerReceiver(mBluetoothUpdateReceiver, new IntentFilter(Constants.ACTION_WHEEL_DATA_AVAILABLE));

if (isExternalStorageReadable() && isExternalStorageWritable()) {

Expand All @@ -70,6 +71,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {

file = new File(dir, fileName);
fileExists = file.exists();

Intent serviceStartedIntent = new Intent(Constants.ACTION_LOGGING_SERVICE_STARTED);
serviceStartedIntent.putExtra(Constants.INTENT_EXTRA_LOGGING_FILE_LOCATION, file.getAbsolutePath());
sendBroadcast(serviceStartedIntent);

wheelLog = (WheelLog) getApplicationContext();
Log.d(TAG, "DataLogger Started");
return START_STICKY;
Expand Down
Loading

0 comments on commit 9f11de1

Please sign in to comment.