Skip to content

Commit 7056f83

Browse files
author
JumpMaster
committed
Updates to DataLogger
1 parent 28c5b70 commit 7056f83

7 files changed

Lines changed: 65 additions & 63 deletions

File tree

app/src/main/java/com/cooper/wheellog/BluetoothLeService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class BluetoothLeService extends Service {
3737

3838
private final static String TAG = BluetoothLeService.class.getSimpleName();
3939
private static final boolean autoConnect = true;
40-
private static final boolean DEBUG = true;
40+
private static final boolean DEBUG = false;
4141

4242
private BluetoothManager mBluetoothManager;
4343
private BluetoothAdapter mBluetoothAdapter;
@@ -58,7 +58,6 @@ public static IntentFilter makeBluetoothUpdateIntentFilter() {
5858
intentFilter.addAction(Constants.ACTION_BLUETOOTH_DATA_AVAILABLE);
5959
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
6060
intentFilter.addAction(Constants.ACTION_REQUEST_SERIAL_DATA);
61-
intentFilter.addAction(Constants.ACTION_PEBBLE_SERVICE_STARTED);
6261
return intentFilter;
6362
}
6463

app/src/main/java/com/cooper/wheellog/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class Constants {
99
public static final String ACTION_BLUETOOTH_DATA_AVAILABLE = "com.cooper.wheellog.bluetoothDataAvailable";
1010
public static final String ACTION_WHEEL_DATA_AVAILABLE = "com.cooper.wheellog.wheelDataAvailable";
1111
public static final String ACTION_REQUEST_SERIAL_DATA = "com.cooper.wheellog.requestSerialData";
12-
public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
13-
public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";
12+
// public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
13+
// public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";
1414

1515
public static final UUID PEBBLE_APP_UUID = UUID.fromString("185c8ae9-7e72-451a-a1c7-8f1e81df9a3d");
1616

app/src/main/java/com/cooper/wheellog/DataLogger.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ public IBinder onBind(Intent intent) {
5757
@Override
5858
public int onStartCommand(Intent intent, int flags, int startId) {
5959
instance = this;
60-
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.UK);
60+
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
6161
registerReceiver(mBluetoothUpdateReceiver, BluetoothLeService.makeBluetoothUpdateIntentFilter());
6262

63-
File root = android.os.Environment.getExternalStorageDirectory();
64-
//LOGI("\nExternal file system root: "+root);
63+
if (isExternalStorageReadable() && isExternalStorageWritable()) {
6564

66-
checkExternalMedia();
65+
File dir = getDownloadsStorageDir();
6766

68-
File dir = new File (root.getAbsolutePath() + "/Download");
69-
dir.mkdirs();
70-
file = new File(dir, "wheelLog.txt");
71-
fileExists = file.exists();
67+
SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
68+
String fileName = sdFormatter.format(new Date()) + ".csv";
7269

73-
Log.d(TAG, "DataLogger Started");
70+
file = new File(dir, fileName);
71+
fileExists = file.exists();
72+
73+
Log.d(TAG, "DataLogger Started");
74+
return START_STICKY;
75+
}
76+
stopSelf();
7477
return START_STICKY;
7578
}
7679

@@ -81,24 +84,27 @@ public void onDestroy() {
8184
Log.d(TAG, "DataLogger stopped");
8285
}
8386

84-
private void checkExternalMedia(){
85-
boolean mExternalStorageAvailable;
86-
boolean mExternalStorageWritable;
87+
/* Checks if external storage is available for read and write */
88+
public boolean isExternalStorageWritable() {
89+
String state = Environment.getExternalStorageState();
90+
return Environment.MEDIA_MOUNTED.equals(state);
91+
}
92+
93+
/* Checks if external storage is available to at least read */
94+
public boolean isExternalStorageReadable() {
8795
String state = Environment.getExternalStorageState();
96+
return Environment.MEDIA_MOUNTED.equals(state) ||
97+
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
98+
}
8899

89-
if (Environment.MEDIA_MOUNTED.equals(state)) {
90-
// Can read and write the media
91-
mExternalStorageAvailable = mExternalStorageWritable = true;
92-
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
93-
// Can only read the media
94-
mExternalStorageAvailable = true;
95-
mExternalStorageWritable = false;
96-
} else {
97-
// Can't read or write
98-
mExternalStorageAvailable = mExternalStorageWritable = false;
100+
public File getDownloadsStorageDir() {
101+
// Get the directory for the user's public pictures directory.
102+
File file = new File(Environment.getExternalStoragePublicDirectory(
103+
Environment.DIRECTORY_DOWNLOADS), "wheelLog");
104+
if (!file.mkdirs()) {
105+
Log.e(TAG, "Directory not created");
99106
}
100-
LOGI("\n\nExternal Media: readable="
101-
+mExternalStorageAvailable+" writable="+mExternalStorageWritable);
107+
return file;
102108
}
103109

104110
private void writeToSDFile(){
@@ -109,14 +115,15 @@ private void writeToSDFile(){
109115
if (!fileExists)
110116
{
111117
fileExists = true;
112-
pw.println("date,speed,voltage,current,distance,fan_status");
118+
pw.println("date,speed,voltage,current,battery_level,distance,temperature,fan_status");
113119
}
114120

115-
pw.println(String.format("%s,%f,%f,%f,%f,%d",
121+
pw.println(String.format(Locale.US, "%s,%f,%f,%f,%d,%f,%d,%d",
116122
sdf.format(new Date()),
117123
Wheel.getInstance().getSpeedDouble(),
118124
Wheel.getInstance().getVoltageDouble(),
119125
Wheel.getInstance().getCurrentDouble(),
126+
Wheel.getInstance().getBatteryLevel(),
120127
Wheel.getInstance().getCurrentDistanceDouble(),
121128
Wheel.getInstance().getTemperature(),
122129
Wheel.getInstance().getFanStatus()

app/src/main/java/com/cooper/wheellog/DeviceListAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static class ViewHolder {
2222

2323
public DeviceListAdapter(AppCompatActivity appCompatActivity) {
2424
super();
25-
mLeDevices = new ArrayList<BluetoothDevice>();
25+
mLeDevices = new ArrayList<>();
2626
mInflator = appCompatActivity.getLayoutInflater();
2727
}
2828

@@ -36,9 +36,9 @@ public BluetoothDevice getDevice(int position) {
3636
return mLeDevices.get(position);
3737
}
3838

39-
public void clear() {
40-
mLeDevices.clear();
41-
}
39+
// public void clear() {
40+
// mLeDevices.clear();
41+
// }
4242

4343
@Override
4444
public int getCount() {

app/src/main/java/com/cooper/wheellog/MainActivity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.widget.TextView;
2222
import android.widget.Toast;
2323

24+
import java.util.Locale;
25+
2426
public class MainActivity extends Activity {
2527

2628
private static final int DEVICE_SCAN_REQUEST = 10;
@@ -87,14 +89,14 @@ public void onReceive(Context context, Intent intent) {
8789
} else if (Constants.ACTION_WHEEL_DATA_AVAILABLE.equals(action)) {
8890
textViewSpeed.setText(String.format("%s KPH", Wheel.getInstance().getSpeedDouble()));
8991
textViewVoltage.setText(String.format("%sV", Wheel.getInstance().getVoltageDouble()));
90-
textViewTemperature.setText(String.format("%d°C", Wheel.getInstance().getTemperature()));
92+
textViewTemperature.setText(String.format(Locale.US, "%d°C", Wheel.getInstance().getTemperature()));
9193
textViewCurrent.setText(String.format("%sW", Wheel.getInstance().getCurrentDouble()));
92-
textViewBattery.setText(String.format("%d%%", Wheel.getInstance().getBatteryLevel()));
94+
textViewBattery.setText(String.format(Locale.US, "%d%%", Wheel.getInstance().getBatteryLevel()));
9395
textViewFanStatus.setText(Wheel.getInstance().getFanStatus() == 0 ? "Off" : "On");
9496
textViewMaxSpeed.setText(String.format("%s KPH", Wheel.getInstance().getMaxSpeedDouble()));
9597
textViewCurrentDistance.setText(String.format("%s KM", Wheel.getInstance().getCurrentDistanceDouble()));
9698
textViewTotalDistance.setText(String.format("%s KM", Wheel.getInstance().getTotalDistanceDouble()));
97-
textViewVersion.setText(String.format("%d", Wheel.getInstance().getVersion()));
99+
textViewVersion.setText(String.format(Locale.US, "%d", Wheel.getInstance().getVersion()));
98100
textViewName.setText(Wheel.getInstance().getName());
99101
textViewType.setText(Wheel.getInstance().getType());
100102
textViewSerial.setText(Wheel.getInstance().getSerial());
@@ -130,10 +132,12 @@ private void setConnectionState(int connectionState) {
130132
mBluetoothLeService.writeBluetoothGattCharacteristic(data);
131133
break;
132134
case BluetoothLeService.STATE_CONNECTING:
135+
Log.d(TAG, "Bluetooth connecting");
133136
buttonScan.setEnabled(false);
134137
buttonConnect.setText(R.string.waiting_for_device);
135138
break;
136139
case BluetoothLeService.STATE_DISCONNECTED:
140+
Log.d(TAG, "Bluetooth disconnected");
137141
buttonScan.setEnabled(true);
138142
buttonConnect.setText(R.string.connect);
139143
break;

app/src/main/java/com/cooper/wheellog/ScanActivity.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
import android.os.Bundle;
1313
import android.os.Handler;
1414
import android.provider.Settings;
15+
import android.support.annotation.NonNull;
1516
import android.support.v4.app.ActivityCompat;
1617
import android.support.v4.content.ContextCompat;
1718
import android.text.TextUtils;
18-
import android.view.Menu;
19-
import android.view.MenuItem;
2019
import android.view.View;
2120
import android.widget.AdapterView;
2221
import android.widget.Toast;
@@ -143,11 +142,7 @@ public void run() {
143142

144143
private boolean checkPermission(){
145144
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
146-
if (result == PackageManager.PERMISSION_GRANTED){
147-
return true;
148-
} else {
149-
return false;
150-
}
145+
return result == PackageManager.PERMISSION_GRANTED;
151146
}
152147

153148
private void requestPermission(){
@@ -159,7 +154,7 @@ private void requestPermission(){
159154
}
160155

161156
@Override
162-
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
157+
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
163158
switch (requestCode) {
164159
case PERMISSION_REQUEST_CODE:
165160
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

app/src/main/java/com/cooper/wheellog/Wheel.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.cooper.wheellog;
22

3+
import java.util.Locale;
34
import java.util.concurrent.TimeUnit;
45

56
public class Wheel {
67
private static int speed;
78
private static long totalDistance;
89
private static int current;
910
private static int temperature;
10-
private static int currentMode;
11+
// private static int currentMode;
1112
private static int battery;
1213
private static int voltage;
1314
private static long currentDistance;
1415
private static int currentTime;
1516
private static int maxSpeed;
1617
private static int fanStatus;
17-
private static int connectionState = BluetoothLeService.STATE_DISCONNECTED;
18+
private static int wheelConnectionState = BluetoothLeService.STATE_DISCONNECTED;
1819
private static String mDeviceNameString;
1920
private static String mUnicycleType;
2021
private static int mVersion;
@@ -34,10 +35,10 @@ public static Wheel getInstance() {
3435
public int getTemperature() { return temperature; }
3536
public int getBatteryLevel() { return battery; }
3637
public int getFanStatus() { return fanStatus; }
37-
public int getConnectionState() { return connectionState; }
38-
public int getMaxSpeed() { return maxSpeed; }
38+
public int getConnectionState() { return wheelConnectionState; }
39+
// public int getMaxSpeed() { return maxSpeed; }
3940
public int getVersion() { return mVersion; }
40-
public int getCurrentTime() { return currentTime; };
41+
// public int getCurrentTime() { return currentTime; };
4142

4243
public String getName() { return mDeviceNameString; }
4344
public String getType() { return mUnicycleType; }
@@ -48,7 +49,7 @@ public String getCurrentTimeString() {
4849
TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(currentTime));
4950
long seconds = TimeUnit.SECONDS.toSeconds(currentTime) -
5051
TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(currentTime));
51-
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
52+
return String.format(Locale.US, "%02d:%02d:%02d", hours, minutes, seconds);
5253
}
5354

5455
public double getSpeedDouble() { return (double) speed / 10.0F; }
@@ -58,7 +59,7 @@ public String getCurrentTimeString() {
5859
public double getCurrentDistanceDouble() { return (double) currentDistance / 1000.0F; }
5960
public double getTotalDistanceDouble() { return (double) totalDistance / 1000.0F; }
6061

61-
public void setConnectionState(boolean connected) { connectionState = connected ? 1 : 0; }
62+
public void setConnectionState(boolean connected) { wheelConnectionState = connected ? 1 : 0; }
6263

6364
private int byteArrayInt2(byte low, byte high) {
6465
return (low & 255) + ((high & 255) * 256);
@@ -81,10 +82,10 @@ public int decodeResponse(byte[] data) {
8182
totalDistance = byteArrayInt4(data[6], data[7], data[8], data[9]);
8283
current = byteArrayInt2(data[10], data[11]);
8384
temperature = byteArrayInt2(data[12], data[13]) / 100;
84-
currentMode = -1;
85-
if ((data[15] & 255) == 224) {
86-
currentMode = data[14];
87-
}
85+
// currentMode = -1;
86+
// if ((data[15] & 255) == 224) {
87+
// currentMode = data[14];
88+
// }
8889

8990
if (voltage < 500) {
9091
battery = 10;
@@ -116,17 +117,13 @@ public int decodeResponse(byte[] data) {
116117
}
117118
try {
118119
mVersion = Integer.parseInt(ss[ss.length - 1]);
119-
} catch (Exception e) {
120+
} catch (Exception ignored) {
120121
}
121122
return Constants.REQUEST_SERIAL_DATA;
122123
} else if ((data[16] & 255) == 179) {
123124
byte[] sndata = new byte[18];
124-
for (int i = 0; i < 14; i++) {
125-
sndata[i] = data[i + 2];
126-
}
127-
for (int i = 14; i < 17; i++) {
128-
sndata[i] = data[i + 3];
129-
}
125+
System.arraycopy(data, 2, sndata, 0, 14);
126+
System.arraycopy(data, 17, sndata, 14, 3);
130127
sndata[17] = (byte) 0;
131128
mUnicycleSN = new String(sndata);
132129
}

0 commit comments

Comments
 (0)