• R/O
  • HTTP
  • SSH
  • HTTPS

BLEControl: コミット

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


コミットメタ情報

リビジョン37d8c91782dafb38c0d292bc5154a380215df2c9 (tree)
日時2020-03-30 23:50:39
作者MRSa <mrsa@myad...>
コミッターMRSa

ログメッセージ

Bluetooth SPPのデータ受信部分を用意。

変更サマリ

差分

--- /dev/null
+++ b/app/src/main/java/net/osdn/gokigen/blecontrol/lib/SimpleLogDumper.java
@@ -0,0 +1,78 @@
1+package net.osdn.gokigen.blecontrol.lib;
2+
3+import android.app.Activity;
4+import android.os.Environment;
5+import android.util.Log;
6+
7+import androidx.annotation.NonNull;
8+
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 SimpleLogDumper
18+{
19+ private static final String TAG = SimpleLogDumper.class.getSimpleName();
20+
21+ /**
22+ * デバッグ用:ログにバイト列を出力する
23+ *
24+ */
25+ public static void dump_bytes(String header, byte[] data)
26+ {
27+ if (data == null)
28+ {
29+ Log.v(TAG, "DATA IS NULL");
30+ return;
31+ }
32+ if (data.length > 8192)
33+ {
34+ Log.v(TAG, " --- DUMP DATA IS TOO LONG... " + data.length + " bytes.");
35+ return;
36+ }
37+
38+ int index = 0;
39+ StringBuffer message;
40+ message = new StringBuffer();
41+ for (byte item : data)
42+ {
43+ index++;
44+ message.append(String.format("%02x ", item));
45+ if (index >= 16)
46+ {
47+ Log.v(TAG, header + " " + message);
48+ index = 0;
49+ message = new StringBuffer();
50+ }
51+ }
52+ if (index != 0)
53+ {
54+ Log.v(TAG, header + " " + message);
55+ }
56+ System.gc();
57+ }
58+
59+ public static void binaryOutputToFile(@NonNull Activity activity, String fileNamePrefix, byte[] rx_body)
60+ {
61+ try
62+ {
63+ Calendar calendar = Calendar.getInstance();
64+ String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
65+ final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
66+ String outputFileName = fileNamePrefix + "_" + extendName + ".bin";
67+ String filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
68+ FileOutputStream outputStream = new FileOutputStream(filepath);
69+ outputStream.write(rx_body, 0, rx_body.length);
70+ outputStream.flush();
71+ outputStream.close();
72+ }
73+ catch (Exception e)
74+ {
75+ e.printStackTrace();
76+ }
77+ }
78+}
--- 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
@@ -1,16 +1,22 @@
11 package net.osdn.gokigen.blecontrol.lib.ble.connect.eeg;
22
33 import android.bluetooth.BluetoothDevice;
4+import android.bluetooth.BluetoothSocket;
45 import android.os.Build;
56 import android.util.Log;
67
78 import androidx.annotation.NonNull;
89 import androidx.fragment.app.FragmentActivity;
910
11+import net.osdn.gokigen.blecontrol.lib.SimpleLogDumper;
1012 import net.osdn.gokigen.blecontrol.lib.ble.R;
1113 import net.osdn.gokigen.blecontrol.lib.ble.connect.BleDeviceFinder;
1214 import net.osdn.gokigen.blecontrol.lib.ble.connect.ITextDataUpdater;
1315
16+import java.io.ByteArrayOutputStream;
17+import java.io.InputStream;
18+import java.util.UUID;
19+
1420
1521 public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
1622 {
@@ -19,6 +25,7 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
1925 private final FragmentActivity context;
2026 private final ITextDataUpdater dataUpdater;
2127 private BleDeviceFinder deviceFinder = null;
28+ private boolean foundDevice = false;
2229
2330 public MindWaveCommunication(@NonNull FragmentActivity context, @NonNull ITextDataUpdater dataUpdater)
2431 {
@@ -42,6 +49,7 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
4249 if (deviceFinder != null)
4350 {
4451 // BLEデバイスをスキャンする
52+ foundDevice = false;
4553 deviceFinder.reset();
4654 deviceFinder.startScan(deviceName);
4755 }
@@ -69,21 +77,89 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
6977 }
7078
7179
72- @Override
73- public void foundBleDevice(BluetoothDevice device)
80+
81+ private void parseReceivedData(byte[] data)
7482 {
83+ // 受信データブロック1つ分
7584 try
7685 {
77- addText(" ");
78- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
86+ if (data.length <= 3)
87+ {
88+ // ヘッダ部しか入っていない...無視する
89+ return;
90+ }
91+ byte length = data[2];
92+ if (data.length < (length + 2))
7993 {
80-/*
81- if (communicator != null)
94+ // データが最小サイズに満たない...無視する
95+ return;
96+ }
97+
98+ SimpleLogDumper.dump_bytes("RECV SPP : ", data);
99+ }
100+ catch (Exception e)
101+ {
102+ e.printStackTrace();
103+ }
104+ }
105+
106+
107+ private void serialCommunicationMain(final BluetoothSocket btSocket)
108+ {
109+ InputStream inputStream = null;
110+ try
111+ {
112+ btSocket.connect();
113+ inputStream = btSocket.getInputStream();
114+ }
115+ catch (Exception e)
116+ {
117+
118+ Log.e(TAG, "Fail to accept.", e);
119+ }
120+ if (inputStream == null)
121+ {
122+ return;
123+ }
124+
125+ // シリアルデータの受信メイン部分
126+ byte previousData = (byte) 0xff;
127+ ByteArrayOutputStream outputStream = null;
128+ while (foundDevice)
129+ {
130+ try
131+ {
132+ int data = inputStream.read();
133+ byte byteData = (byte) (data & 0xff);
134+ if ((previousData == byteData)&&(byteData == (byte) 0xaa))
82135 {
83- communicator.startCommunicate(device);
136+ // 先頭データを見つけた。 (0xaa 0xaa がヘッダ)
137+ if (outputStream != null)
138+ {
139+ parseReceivedData(outputStream.toByteArray());
140+ outputStream = null;
141+ }
142+ outputStream = new ByteArrayOutputStream();
143+ outputStream.write((byte) 0xaa);
144+ outputStream.write((byte) 0xaa);
84145 }
85-*/
146+ else
147+ {
148+ if (outputStream != null)
149+ {
150+ outputStream.write(byteData);
151+ }
152+ }
153+ previousData = byteData;
86154 }
155+ catch (Exception e)
156+ {
157+ e.printStackTrace();
158+ }
159+ }
160+ try
161+ {
162+ btSocket.close();
87163 }
88164 catch (Exception e)
89165 {
@@ -91,4 +167,34 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult
91167 }
92168 }
93169
170+ @Override
171+ public void foundBleDevice(BluetoothDevice device)
172+ {
173+ try
174+ {
175+ foundDevice = true;
176+ final BluetoothSocket btSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
177+ Thread thread = new Thread(new Runnable() {
178+ @Override
179+ public void run() {
180+ try
181+ {
182+ serialCommunicationMain(btSocket);
183+ }
184+ catch (Exception e)
185+ {
186+ e.printStackTrace();
187+ }
188+ }
189+ });
190+ if (btSocket != null)
191+ {
192+ thread.start();
193+ }
194+ }
195+ catch (Exception e)
196+ {
197+ e.printStackTrace();
198+ }
199+ }
94200 }
--- a/app/src/main/res/values-ja/strings.xml
+++ b/app/src/main/res/values-ja/strings.xml
@@ -1,6 +1,7 @@
11 <resources>
22 <string name="blank"> </string>
33 <string name="app_name">FV-100 CHECKER</string>
4+ <string name="app_name2">BLEControl</string>
45 <string name="nav_header_title">FV-100 CHECKER</string>
56 <string name="nav_header_subtitle"> </string>
67 <string name="nav_header_desc">Navigation header</string>
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,6 +1,7 @@
11 <resources>
22 <string name="blank"> </string>
33 <string name="app_name">FV-100 CHECKER</string>
4+ <string name="app_name2">BLEControl</string>
45 <string name="nav_header_title">FV-100 CHECKER</string>
56 <string name="nav_header_subtitle"> </string>
67 <string name="nav_header_desc">Navigation header</string>
旧リポジトリブラウザで表示