Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Added menu with option do redirect log to a file, email the log file … #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light"
Expand Down
132 changes: 129 additions & 3 deletions app/src/main/java/com/nordicsemi/nrfUARTv2/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@



import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.Date;

import java.io.File;

import com.nordicsemi.nrfUARTv2.UartService;

Expand All @@ -51,12 +53,15 @@
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
Expand All @@ -76,6 +81,9 @@ public class MainActivity extends Activity implements RadioGroup.OnCheckedChange
private static final int UART_PROFILE_DISCONNECTED = 21;
private static final int STATE_OFF = 10;

enum LoggingState {Disconnected, NotLogging, Logging};
static final String LogFileName = "nRF51.log";

TextView mRemoteRssiVal;
RadioGroup mRg;
private int mState = UART_PROFILE_DISCONNECTED;
Expand Down Expand Up @@ -208,6 +216,7 @@ public void run() {
listAdapter.add("["+currentDateTimeString+"] Connected to: "+ mDevice.getName());
messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
mState = UART_PROFILE_CONNECTED;
setLoggingState(LoggingState.NotLogging);
}
});
}
Expand All @@ -226,7 +235,7 @@ public void run() {
mState = UART_PROFILE_DISCONNECTED;
mService.close();
//setUiState();
setLoggingState(LoggingState.Disconnected);
}
});
}
Expand Down Expand Up @@ -260,7 +269,21 @@ public void run() {
mService.disconnect();
}


if(action.equals(UartService.ACTION_LOGGING_ENABLED)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
setLoggingState(LoggingState.Logging);
}
});
} else if(action.equals(UartService.ACTION_LOGGING_DISABLED)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
setLoggingState(LoggingState.NotLogging);
}
});
}
}
};

Expand All @@ -276,6 +299,8 @@ private static IntentFilter makeGattUpdateIntentFilter() {
intentFilter.addAction(UartService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(UartService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(UartService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(UartService.ACTION_LOGGING_ENABLED);
intentFilter.addAction(UartService.ACTION_LOGGING_DISABLED);
intentFilter.addAction(UartService.DEVICE_DOES_NOT_SUPPORT_UART);
return intentFilter;
}
Expand Down Expand Up @@ -335,6 +360,71 @@ public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
optionsMenu = menu;
setLoggingState(LoggingState.Disconnected);
return true;
}

Menu optionsMenu;

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

switch(id) {
case R.id.action_clear_log:
new File(Environment.getExternalStorageDirectory(), LogFileName).delete();
setLoggingState(mLoggingState);
return true;

case R.id.action_toggle_log:
if(mService != null) {
if (mService.isLogging()) {
mService.closeLogFile();
} else {
try {
mService.openLogFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + LogFileName);
} catch(Exception ex) {
Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
return true;

case R.id.action_mail_log:
File fileToShare = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), LogFileName);
if(fileToShare.exists()) {
try {
//Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapp.fileprovider", fileToShare);
Uri uri = Uri.fromFile(fileToShare);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.putExtra(Intent.EXTRA_SUBJECT, "nRF UART log file");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Log file attached");
intent.setData(Uri.parse("mailto:"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(intent);
//activity.finish();
} catch (Exception e) {
System.out.println("is exception raises during sending mail" + e);
}
return true;
}
}

return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
Expand Down Expand Up @@ -375,6 +465,42 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {

}

private boolean getLogFileExists() {
return new File(Environment.getExternalStorageDirectory().getAbsolutePath(), LogFileName).exists();
}
LoggingState mLoggingState = LoggingState.Disconnected;

private void setLoggingState(LoggingState state) {
MenuItem clearLogItem = optionsMenu.findItem(R.id.action_clear_log);
MenuItem toggleLogItem = optionsMenu.findItem(R.id.action_toggle_log);
MenuItem mailLogItem = optionsMenu.findItem(R.id.action_mail_log);

boolean logFileExists = getLogFileExists();
switch (state) {
default:
case Disconnected:
clearLogItem.setEnabled(logFileExists);
toggleLogItem.setEnabled(false);
toggleLogItem.setChecked(false);
mailLogItem.setEnabled(logFileExists);
break;

case Logging:
clearLogItem.setEnabled(false);
toggleLogItem.setEnabled(true);
toggleLogItem.setChecked(true);
mailLogItem .setEnabled(false);
break;

case NotLogging:
clearLogItem.setEnabled(logFileExists);
toggleLogItem.setEnabled(true);
toggleLogItem.setChecked(false);
mailLogItem .setEnabled(logFileExists);
break;
}
mLoggingState = state;
}

private void showMessage(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
Expand Down
72 changes: 64 additions & 8 deletions app/src/main/java/com/nordicsemi/nrfUARTv2/UartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.text.format.Time;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -67,6 +71,10 @@ public class UartService extends Service {
"com.nordicsemi.nrfUART.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
"com.nordicsemi.nrfUART.ACTION_DATA_AVAILABLE";
public final static String ACTION_LOGGING_ENABLED =
"com.nordicsemi.nrfUART.ACTION_LOGGING_ENABLED";
public final static String ACTION_LOGGING_DISABLED =
"com.nordicsemi.nrfUART.ACTION_LOGGING_DISABLED";
public final static String EXTRA_DATA =
"com.nordicsemi.nrfUART.EXTRA_DATA";
public final static String DEVICE_DOES_NOT_SUPPORT_UART =
Expand Down Expand Up @@ -140,17 +148,32 @@ private void broadcastUpdate(final String action) {

private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);

// This is handling for the notification on TX Character of NUS service
if (TX_CHAR_UUID.equals(characteristic.getUuid())) {

// Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
intent.putExtra(EXTRA_DATA, characteristic.getValue());
if(logFile != null) {
// Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
if (TX_CHAR_UUID.equals(characteristic.getUuid())) {
byte[] value = characteristic.getValue();
if (logFile != null) {
try {
logFile.write(value);
logFile.write(10);// Line-feed
} catch (IOException ex) {
}
return;
}
}

} else {

final Intent intent = new Intent(action);

// This is special handling for the Heart Rate Measurement profile. Data parsing is
// carried out as per profile specifications:
// http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
if (TX_CHAR_UUID.equals(characteristic.getUuid())) {
intent.putExtra(EXTRA_DATA, characteristic.getValue());
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

public class LocalBinder extends Binder {
Expand Down Expand Up @@ -249,6 +272,7 @@ public boolean connect(final String address) {
* callback.
*/
public void disconnect() {
closeLogFile();
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
Expand All @@ -262,6 +286,7 @@ public void disconnect() {
* released properly.
*/
public void close() {
closeLogFile();
if (mBluetoothGatt == null) {
return;
}
Expand Down Expand Up @@ -363,4 +388,35 @@ public List<BluetoothGattService> getSupportedGattServices() {

return mBluetoothGatt.getServices();
}

FileOutputStream logFile;

public void openLogFile(String path) throws IOException {
logFile = new FileOutputStream(path, true);
Time now = new Time();
now.setToNow();
String endMessage = "=== Start logging " + now.toString() + "===\n";
logFile.write(endMessage.getBytes("UTF-8"));
broadcastUpdate(ACTION_LOGGING_ENABLED);
}

public void closeLogFile() {
if(logFile != null) {
try {
Time now = new Time();
now.setToNow();
String endMessage = "=== Finished logging " + now.toString() + "===\n";
logFile.write(endMessage.getBytes("UTF-8"));
logFile.close();
}
catch(IOException e) {
}
logFile = null;
broadcastUpdate(ACTION_LOGGING_DISABLED);
}
}

public boolean isLogging() {
return logFile != null;
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_toggle_log" android:title="@string/action_toggle_log" android:checkable="true"
android:orderInCategory="100" />
<item android:id="@+id/action_mail_log" android:title="@string/action_mail_log"
android:orderInCategory="100" />
<item android:id="@+id/action_clear_log" android:title="@string/action_clear_log"
android:orderInCategory="100" />
</menu>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@
<string name="ble_not_supported">Bluetooth Low Energy not supported</string>
<string name="scan">Scan</string>
<string name="cancel">Cancel</string>

<string name="action_toggle_log">Log to file</string>
<string name="action_clear_log">Clear log</string>
<string name="action_mail_log">Mail log</string>
</resources>