リビジョン | 37d8c91782dafb38c0d292bc5154a380215df2c9 (tree) |
---|---|
日時 | 2020-03-30 23:50:39 |
作者 | MRSa <mrsa@myad...> |
コミッター | MRSa |
Bluetooth SPPのデータ受信部分を用意。
@@ -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 | +} |
@@ -1,16 +1,22 @@ | ||
1 | 1 | package net.osdn.gokigen.blecontrol.lib.ble.connect.eeg; |
2 | 2 | |
3 | 3 | import android.bluetooth.BluetoothDevice; |
4 | +import android.bluetooth.BluetoothSocket; | |
4 | 5 | import android.os.Build; |
5 | 6 | import android.util.Log; |
6 | 7 | |
7 | 8 | import androidx.annotation.NonNull; |
8 | 9 | import androidx.fragment.app.FragmentActivity; |
9 | 10 | |
11 | +import net.osdn.gokigen.blecontrol.lib.SimpleLogDumper; | |
10 | 12 | import net.osdn.gokigen.blecontrol.lib.ble.R; |
11 | 13 | import net.osdn.gokigen.blecontrol.lib.ble.connect.BleDeviceFinder; |
12 | 14 | import net.osdn.gokigen.blecontrol.lib.ble.connect.ITextDataUpdater; |
13 | 15 | |
16 | +import java.io.ByteArrayOutputStream; | |
17 | +import java.io.InputStream; | |
18 | +import java.util.UUID; | |
19 | + | |
14 | 20 | |
15 | 21 | public class MindWaveCommunication implements BleDeviceFinder.BleScanResult |
16 | 22 | { |
@@ -19,6 +25,7 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult | ||
19 | 25 | private final FragmentActivity context; |
20 | 26 | private final ITextDataUpdater dataUpdater; |
21 | 27 | private BleDeviceFinder deviceFinder = null; |
28 | + private boolean foundDevice = false; | |
22 | 29 | |
23 | 30 | public MindWaveCommunication(@NonNull FragmentActivity context, @NonNull ITextDataUpdater dataUpdater) |
24 | 31 | { |
@@ -42,6 +49,7 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult | ||
42 | 49 | if (deviceFinder != null) |
43 | 50 | { |
44 | 51 | // BLEデバイスをスキャンする |
52 | + foundDevice = false; | |
45 | 53 | deviceFinder.reset(); |
46 | 54 | deviceFinder.startScan(deviceName); |
47 | 55 | } |
@@ -69,21 +77,89 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult | ||
69 | 77 | } |
70 | 78 | |
71 | 79 | |
72 | - @Override | |
73 | - public void foundBleDevice(BluetoothDevice device) | |
80 | + | |
81 | + private void parseReceivedData(byte[] data) | |
74 | 82 | { |
83 | + // 受信データブロック1つ分 | |
75 | 84 | try |
76 | 85 | { |
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)) | |
79 | 93 | { |
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)) | |
82 | 135 | { |
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); | |
84 | 145 | } |
85 | -*/ | |
146 | + else | |
147 | + { | |
148 | + if (outputStream != null) | |
149 | + { | |
150 | + outputStream.write(byteData); | |
151 | + } | |
152 | + } | |
153 | + previousData = byteData; | |
86 | 154 | } |
155 | + catch (Exception e) | |
156 | + { | |
157 | + e.printStackTrace(); | |
158 | + } | |
159 | + } | |
160 | + try | |
161 | + { | |
162 | + btSocket.close(); | |
87 | 163 | } |
88 | 164 | catch (Exception e) |
89 | 165 | { |
@@ -91,4 +167,34 @@ public class MindWaveCommunication implements BleDeviceFinder.BleScanResult | ||
91 | 167 | } |
92 | 168 | } |
93 | 169 | |
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 | + } | |
94 | 200 | } |
@@ -1,6 +1,7 @@ | ||
1 | 1 | <resources> |
2 | 2 | <string name="blank"> </string> |
3 | 3 | <string name="app_name">FV-100 CHECKER</string> |
4 | + <string name="app_name2">BLEControl</string> | |
4 | 5 | <string name="nav_header_title">FV-100 CHECKER</string> |
5 | 6 | <string name="nav_header_subtitle"> </string> |
6 | 7 | <string name="nav_header_desc">Navigation header</string> |
@@ -1,6 +1,7 @@ | ||
1 | 1 | <resources> |
2 | 2 | <string name="blank"> </string> |
3 | 3 | <string name="app_name">FV-100 CHECKER</string> |
4 | + <string name="app_name2">BLEControl</string> | |
4 | 5 | <string name="nav_header_title">FV-100 CHECKER</string> |
5 | 6 | <string name="nav_header_subtitle"> </string> |
6 | 7 | <string name="nav_header_desc">Navigation header</string> |