@@ -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 \n External 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 ()
0 commit comments