• R/O
  • HTTP
  • SSH
  • HTTPS

BLEControl: コミット

BLE通信を行うためのアプリ。


コミットメタ情報

リビジョン38d36c1a2911b2e0f6cd708b3dd0f428066cda81 (tree)
日時2020-04-07 23:30:28
作者MRSa <mrsa@myad...>
コミッターMRSa

ログメッセージ

サマリーデータについて、ロギング(ファイル出力)できるようにした。

変更サマリ

差分

--- a/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ble/connect/eeg/MindWaveCommunication.java
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ble/connect/eeg/MindWaveCommunication.java
@@ -8,10 +8,10 @@ import android.util.Log;
88 import androidx.annotation.NonNull;
99 import androidx.fragment.app.FragmentActivity;
1010
11-import net.osdn.gokigen.blecontrol.lib.SimpleLogDumper;
1211 import net.osdn.gokigen.blecontrol.lib.ble.R;
1312 import net.osdn.gokigen.blecontrol.lib.ble.connect.BleDeviceFinder;
1413 import net.osdn.gokigen.blecontrol.lib.ble.connect.ITextDataUpdater;
14+import net.osdn.gokigen.blecontrol.lib.data.brainwave.BrainwaveFileLogger;
1515 import net.osdn.gokigen.blecontrol.lib.data.brainwave.IBrainwaveDataReceiver;
1616
1717 import java.io.ByteArrayOutputStream;
@@ -27,7 +27,9 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
2727 private final ITextDataUpdater dataUpdater;
2828 private final IBrainwaveDataReceiver dataReceiver;
2929 private BleDeviceFinder deviceFinder = null;
30+ private BrainwaveFileLogger fileLogger = null;
3031 private boolean foundDevice = false;
32+ private boolean loggingFlag = false;
3133
3234 public MindWaveCommunication(@NonNull FragmentActivity context, @NonNull ITextDataUpdater dataUpdater, @NonNull IBrainwaveDataReceiver dataReceiver)
3335 {
@@ -40,12 +42,13 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
4042 }
4143 }
4244
43- public void connect(@NonNull String deviceName)
45+ public void connect(@NonNull String deviceName, boolean loggingFlag)
4446 {
45- Log.v(TAG, " BrainWaveMobileCommunicator::connect() : " + deviceName);
46- setText(context.getString(R.string.start_query) + " '" + deviceName + "' ");
47+ Log.v(TAG, " BrainWaveMobileCommunicator::connect() : " + deviceName + " Logging : " + loggingFlag);
48+ setText(context.getString(R.string.start_query) + " '" + deviceName + "'");
4749 try
4850 {
51+ this.loggingFlag = loggingFlag;
4952 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
5053 {
5154 // BLE のサービスを取得
@@ -79,8 +82,6 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
7982 dataUpdater.addText(message);
8083 }
8184
82-
83-
8485 private void parseReceivedData(byte[] data)
8586 {
8687 // 受信データブロック1つ分
@@ -109,7 +110,11 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
109110 return;
110111 }
111112 dataReceiver.receivedSummaryData(data);
112- //SimpleLogDumper.dump_bytes("RECV SPP [" + data.length + "] ", data);
113+ if (fileLogger != null)
114+ {
115+ // ファイルにサマリーデータを出力する
116+ fileLogger.outputSummaryData(data);
117+ }
113118 }
114119 catch (Exception e)
115120 {
@@ -136,6 +141,19 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
136141 return;
137142 }
138143
144+ if (loggingFlag)
145+ {
146+ try
147+ {
148+ // ログ出力を指示されていた場合...ファイル出力クラスを作成しておく
149+ fileLogger = new BrainwaveFileLogger(context);
150+ }
151+ catch (Exception e)
152+ {
153+ e.printStackTrace();
154+ }
155+ }
156+
139157 // シリアルデータの受信メイン部分
140158 byte previousData = (byte) 0xff;
141159 ByteArrayOutputStream outputStream = null;
--- a/app/src/main/java/net/osdn/gokigen/blecontrol/lib/data/brainwave/BrainwaveDataHolder.java
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/data/brainwave/BrainwaveDataHolder.java
@@ -81,6 +81,11 @@ public class BrainwaveDataHolder implements IBrainwaveDataReceiver
8181 {
8282 return (Arrays.copyOfRange(valueBuffer, 0, endPosition));
8383 }
84+ if (currentPosition == 0)
85+ {
86+ endPosition = (maxBufferSize - 1);
87+ return (Arrays.copyOfRange(valueBuffer, (endPosition - size), endPosition));
88+ }
8489
8590 int remainSize = size - (currentPosition - 1);
8691 int [] size0 = Arrays.copyOfRange(valueBuffer, 0, (currentPosition - 1));
@@ -88,8 +93,8 @@ public class BrainwaveDataHolder implements IBrainwaveDataReceiver
8893
8994 replyData = new int[size];
9095
91- System.arraycopy(size0, 0, replyData, 0, size0.length);
92- System.arraycopy(size1, 0, replyData, size0.length, size1.length);
96+ System.arraycopy(size1, 0, replyData, 0, size1.length);
97+ System.arraycopy(size0, 0, replyData, size1.length, size0.length);
9398 }
9499 catch (Exception e)
95100 {
--- /dev/null
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/data/brainwave/BrainwaveFileLogger.java
@@ -0,0 +1,56 @@
1+package net.osdn.gokigen.blecontrol.lib.data.brainwave;
2+
3+import android.os.Environment;
4+
5+import androidx.annotation.NonNull;
6+import androidx.fragment.app.FragmentActivity;
7+
8+import net.osdn.gokigen.blecontrol.lib.SimpleLogDumper;
9+import net.osdn.gokigen.blecontrol.lib.ble.R;
10+
11+import java.io.File;
12+import java.io.FileOutputStream;
13+import java.text.SimpleDateFormat;
14+import java.util.Calendar;
15+import java.util.Locale;
16+
17+public class BrainwaveFileLogger
18+{
19+ private FileOutputStream outputStream;
20+
21+ public BrainwaveFileLogger(@NonNull FragmentActivity context)
22+ {
23+ try
24+ {
25+ String fileNamePrefix = context.getString(R.string.app_name2) + "_EEG";
26+ Calendar calendar = Calendar.getInstance();
27+ String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
28+ final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/";
29+ String outputFileName = fileNamePrefix + "_" + extendName + ".bin";
30+ String filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
31+ outputStream = new FileOutputStream(filepath);
32+ }
33+ catch (Exception e)
34+ {
35+ e.printStackTrace();
36+ outputStream = null;
37+ }
38+ }
39+
40+ public void outputSummaryData(byte[] data)
41+ {
42+ try
43+ {
44+ SimpleLogDumper.dump_bytes("RECV [" + data.length + "] ", data);
45+ if ((outputStream != null)&&(data.length >= 36))
46+ {
47+ outputStream.write(data, 0, 36);
48+ outputStream.flush();
49+ }
50+ }
51+ catch (Exception e)
52+ {
53+ e.printStackTrace();
54+ }
55+ }
56+}
--- a/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ui/brainwave/BrainwaveConnection.java
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ui/brainwave/BrainwaveConnection.java
@@ -3,6 +3,7 @@ package net.osdn.gokigen.blecontrol.lib.ui.brainwave;
33 import android.util.Log;
44 import android.view.View;
55 import android.widget.ImageButton;
6+import android.widget.Switch;
67
78 import androidx.annotation.NonNull;
89 import androidx.annotation.Nullable;
@@ -22,14 +23,16 @@ public class BrainwaveConnection implements View.OnClickListener, ITextDataUpdat
2223 private final BrainwaveMobileViewModel viewModel;
2324 private final MindWaveCommunication communicator;
2425 private final SnackBarMessage messageToShow;
26+ private final Switch loggingSwitch;
2527
26- BrainwaveConnection(@NonNull FragmentActivity context, @NonNull SelectDevice deviceSelection, @NonNull BrainwaveMobileViewModel viewModel, @NonNull IBrainwaveDataReceiver dataReceiver)
28+ BrainwaveConnection(@NonNull FragmentActivity context, @NonNull SelectDevice deviceSelection, @NonNull BrainwaveMobileViewModel viewModel, @NonNull IBrainwaveDataReceiver dataReceiver, @Nullable Switch loggingSwitch)
2729 {
2830 this.context = context;
2931 this.deviceSelection = deviceSelection;
3032 this.viewModel = viewModel;
3133 this.communicator = new MindWaveCommunication(context, this, dataReceiver);
3234 this.messageToShow = new SnackBarMessage(context, false);
35+ this.loggingSwitch = loggingSwitch;
3336 }
3437
3538 @Override
@@ -50,18 +53,24 @@ public class BrainwaveConnection implements View.OnClickListener, ITextDataUpdat
5053
5154 private void connectToEEG(@Nullable final String selectedDevice)
5255 {
56+ boolean logging = false;
5357 if (selectedDevice == null)
5458 {
5559 Log.v(TAG, " DEVICE is NULL.");
5660 return;
5761 }
62+ if (loggingSwitch != null)
63+ {
64+ logging = loggingSwitch.isChecked();
65+ }
5866 try
5967 {
68+ final boolean loggingFlag = logging;
6069 Log.v(TAG, " CONNECT TO EEG. : " + selectedDevice);
6170 Thread thread = new Thread(new Runnable() {
6271 @Override
6372 public void run() {
64- communicator.connect(selectedDevice);
73+ communicator.connect(selectedDevice, loggingFlag);
6574 }
6675 });
6776 thread.start();
--- a/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ui/brainwave/BrainwaveMobileFragment.java
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/ui/brainwave/BrainwaveMobileFragment.java
@@ -10,6 +10,7 @@ import android.widget.AdapterView;
1010 import android.widget.ArrayAdapter;
1111 import android.widget.Button;
1212 import android.widget.Spinner;
13+import android.widget.Switch;
1314 import android.widget.TextView;
1415
1516 import androidx.annotation.Nullable;
@@ -55,7 +56,8 @@ public class BrainwaveMobileFragment extends Fragment implements BrainwaveConnec
5556 prepareDeviceSelection(context, root);
5657
5758 // Connect Button
58- final BrainwaveConnection eegConnection = new BrainwaveConnection(context, this, brainwaveViewModel, dataHolder);
59+ final Switch loggingSwitch = root.findViewById(R.id.switch_logging);
60+ final BrainwaveConnection eegConnection = new BrainwaveConnection(context, this, brainwaveViewModel, dataHolder, loggingSwitch);
5961 final Button queryButton = root.findViewById(R.id.connect_to_eeg);
6062 if (queryButton != null)
6163 {
--- a/app/src/main/res/layout/fragment_brainwave.xml
+++ b/app/src/main/res/layout/fragment_brainwave.xml
@@ -75,6 +75,26 @@
7575 app:srcCompat="@drawable/ic_texture_black_24dp"
7676 android:visibility="invisible" />
7777
78+ <TextView
79+ android:id="@+id/label_logging"
80+ android:layout_width="wrap_content"
81+ android:layout_height="wrap_content"
82+ android:layout_gravity="center_horizontal"
83+ android:textAlignment="center"
84+ android:layout_marginRight="4sp"
85+ android:layout_marginEnd="4sp"
86+ android:text="@string/label_logging"
87+ android:textSize="8pt" />
88+
89+ <Switch
90+ android:id="@+id/switch_logging"
91+ android:layout_width="wrap_content"
92+ android:layout_height="wrap_content"
93+ android:layout_gravity="center_horizontal"
94+ android:textAlignment="center"
95+ android:layout_marginRight="4sp"
96+ android:layout_marginEnd="4sp"
97+ />
7898 </LinearLayout>
7999
80100 <View
--- a/app/src/main/res/values-ja/strings.xml
+++ b/app/src/main/res/values-ja/strings.xml
@@ -72,4 +72,5 @@
7272 <string name="value_title_midGamma">mid Gamma(41&#8211;49.75Hz): </string>
7373 <string name="value_title_attention">Attention: </string>
7474 <string name="value_title_mediation">Mediation: </string>
75+ <string name="label_logging"> Logging</string>
7576 </resources>
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -71,4 +71,5 @@
7171 <string name="value_title_midGamma">mid Gamma(41&#8211;49.75Hz): </string>
7272 <string name="value_title_attention">Attention: </string>
7373 <string name="value_title_mediation">Mediation: </string>
74+ <string name="label_logging"> Logging</string>
7475 </resources>
旧リポジトリブラウザで表示