Ricoh Theta x Nurosky Shutter
Révision | 82a012abf18e99f6e27665a2939e0ae2bee0b657 (tree) |
---|---|
l'heure | 2021-02-21 23:36:48 |
Auteur | MRSa <mrsa@myad...> |
Commiter | MRSa |
ペアリング途中。
@@ -23,7 +23,6 @@ | ||
23 | 23 | <activity android:name=".MainActivity"> |
24 | 24 | <intent-filter> |
25 | 25 | <action android:name="android.intent.action.MAIN" /> |
26 | - | |
27 | 26 | <category android:name="android.intent.category.LAUNCHER" /> |
28 | 27 | </intent-filter> |
29 | 28 | </activity> |
@@ -3,6 +3,10 @@ package jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg | ||
3 | 3 | import android.app.Activity |
4 | 4 | import android.bluetooth.BluetoothDevice |
5 | 5 | import android.bluetooth.BluetoothSocket |
6 | +import android.content.BroadcastReceiver | |
7 | +import android.content.Context | |
8 | +import android.content.Intent | |
9 | +import android.content.IntentFilter | |
6 | 10 | import android.util.Log |
7 | 11 | import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult |
8 | 12 | import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.BluetoothDeviceFinder |
@@ -13,23 +17,35 @@ import java.io.InputStream | ||
13 | 17 | import java.util.* |
14 | 18 | import kotlin.experimental.and |
15 | 19 | |
16 | -class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwaveDataReceiver, private val scanResult: IBluetoothScanResult? = null) : IBluetoothScanResult | |
20 | +class MindWaveConnection(private val activity : Activity, private val dataReceiver: IBrainwaveDataReceiver, private val scanResult: IBluetoothScanResult? = null) : IBluetoothScanResult | |
17 | 21 | { |
18 | 22 | companion object |
19 | 23 | { |
20 | 24 | private val TAG = MindWaveConnection::class.java.simpleName |
21 | 25 | } |
22 | 26 | |
23 | - private val deviceFinder = BluetoothDeviceFinder(context, this) | |
27 | + private val deviceFinder = BluetoothDeviceFinder(activity, this) | |
24 | 28 | private var fileLogger: BrainwaveFileLogger? = null |
25 | 29 | private var foundDevice = false |
30 | + private var isPairing = false | |
26 | 31 | private var loggingFlag = false |
32 | + private var targetDevice: BluetoothDevice? = null | |
33 | + | |
34 | + private var connectionReceiver = object : BroadcastReceiver() | |
35 | + { | |
36 | + override fun onReceive(context: Context, intent: Intent) | |
37 | + { | |
38 | + onReceiveBroadcastOfConnection(this, context, intent) | |
39 | + } | |
40 | + } | |
27 | 41 | |
28 | 42 | fun connect(deviceName: String, loggingFlag: Boolean = false) |
29 | 43 | { |
30 | 44 | Log.v(TAG, " BrainWaveMobileCommunicator::connect() : $deviceName Logging : $loggingFlag") |
31 | 45 | try |
32 | 46 | { |
47 | + registerReceiver() | |
48 | + | |
33 | 49 | this.loggingFlag = loggingFlag |
34 | 50 | |
35 | 51 | // Bluetooth のサービスを取得、BLEデバイスをスキャンする |
@@ -43,6 +59,33 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav | ||
43 | 59 | } |
44 | 60 | } |
45 | 61 | |
62 | + private fun registerReceiver() | |
63 | + { | |
64 | + try | |
65 | + { | |
66 | + val filter = IntentFilter() | |
67 | + filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST) | |
68 | + filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED) | |
69 | + activity.registerReceiver(connectionReceiver, filter) | |
70 | + } | |
71 | + catch (e : Exception) | |
72 | + { | |
73 | + e.printStackTrace() | |
74 | + } | |
75 | + } | |
76 | + | |
77 | + private fun unregisterReceiver() | |
78 | + { | |
79 | + try | |
80 | + { | |
81 | + activity.unregisterReceiver(connectionReceiver) | |
82 | + } | |
83 | + catch (e : Exception) | |
84 | + { | |
85 | + e.printStackTrace() | |
86 | + } | |
87 | + } | |
88 | + | |
46 | 89 | private fun parseReceivedData(data: ByteArray) |
47 | 90 | { |
48 | 91 | // 受信データブロック1つ分 |
@@ -154,21 +197,29 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav | ||
154 | 197 | { |
155 | 198 | try |
156 | 199 | { |
157 | - Log.v(TAG, " foundBluetoothDevice : ${device.name}") | |
200 | + Log.v(TAG, " foundBluetoothDevice : ${device.name} : ${device.bondState}") | |
158 | 201 | |
159 | 202 | deviceFinder.stopScan() |
160 | 203 | |
161 | - // TODO: 見つかったデバイスにペアリングする | |
162 | - | |
163 | - device.setPin(byteArrayOf(0x30,0x30, 0x30, 0x30)) | |
164 | - | |
165 | - val result = device.createBond() | |
166 | - if (!result) | |
204 | + if (device.bondState == BluetoothDevice.BOND_BONDED) | |
167 | 205 | { |
168 | - // ペアリング失敗 | |
206 | + // すでにペアリング済み | |
207 | + Log.v(TAG, " ALREADY PAIRED ") | |
208 | + isPairing = true | |
209 | + targetDevice = device | |
210 | + connectBluetoothDevice(device) | |
211 | + return | |
169 | 212 | } |
170 | 213 | |
171 | - | |
214 | + if ((!foundDevice)&&(!isPairing)) | |
215 | + { | |
216 | + Log.v(TAG, "START PAIRING ${device.name}") | |
217 | + device.setPin(byteArrayOf(0x30, 0x30, 0x30, 0x30)) | |
218 | + targetDevice = device | |
219 | + device.createBond() | |
220 | + isPairing = true | |
221 | + } | |
222 | + deviceFinder.stopScan() | |
172 | 223 | } |
173 | 224 | catch (e : Exception) |
174 | 225 | { |
@@ -247,10 +298,101 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav | ||
247 | 298 | } |
248 | 299 | */ |
249 | 300 | |
301 | + private fun connectBluetoothDevice(device : BluetoothDevice?) | |
302 | + { | |
303 | + try | |
304 | + { | |
305 | + Log.v(TAG, "connectBluetoothDevice() : ${device?.name}") | |
306 | + if (device == null) | |
307 | + { | |
308 | + Log.v(TAG, " DEVICE IS NULL...") | |
309 | + return | |
310 | + } | |
311 | + | |
312 | + val btSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")) | |
313 | + val thread = Thread { | |
314 | + try | |
315 | + { | |
316 | + if (btSocket != null) | |
317 | + { | |
318 | + serialCommunicationMain(btSocket) | |
319 | + } | |
320 | + } | |
321 | + catch (e: Exception) | |
322 | + { | |
323 | + e.printStackTrace() | |
324 | + } | |
325 | + } | |
326 | + if (btSocket != null) | |
327 | + { | |
328 | + thread.start() | |
329 | + } | |
330 | + else | |
331 | + { | |
332 | + Log.v(TAG, " btSocket is NULL.") | |
333 | + } | |
334 | + scanResult?.foundBluetoothDevice(device) | |
335 | + } | |
336 | + catch (e: Exception) | |
337 | + { | |
338 | + e.printStackTrace() | |
339 | + } | |
340 | + } | |
341 | + | |
250 | 342 | override fun notFindBluetoothDevice() |
251 | 343 | { |
252 | 344 | Log.v(TAG, " notFindBluetoothDevice()") |
253 | 345 | scanResult?.notFindBluetoothDevice() |
254 | 346 | deviceFinder.stopScan() |
255 | 347 | } |
348 | + | |
349 | + /** | |
350 | + * | |
351 | + * | |
352 | + */ | |
353 | + private fun onReceiveBroadcastOfConnection(receiver: BroadcastReceiver, context: Context, intent: Intent) | |
354 | + { | |
355 | + val action = intent.action | |
356 | + if (action == null) | |
357 | + { | |
358 | + Log.v(TAG, "intent.getAction() : null") | |
359 | + return | |
360 | + } | |
361 | + try | |
362 | + { | |
363 | + if (action == BluetoothDevice.ACTION_PAIRING_REQUEST) | |
364 | + { | |
365 | + val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)!! | |
366 | + Log.v(TAG, " onReceiveBroadcastOfConnection : BluetoothDevice.ACTION_PAIRING_REQUEST device: ${device.name}") | |
367 | + try | |
368 | + { | |
369 | + targetDevice?.setPin(byteArrayOf(0x30,0x30, 0x30, 0x30)) | |
370 | + device.setPin(byteArrayOf(0x30,0x30, 0x30, 0x30)) | |
371 | + receiver.abortBroadcast() | |
372 | + } | |
373 | + catch (e : Exception) | |
374 | + { | |
375 | + e.printStackTrace() | |
376 | + } | |
377 | + } | |
378 | + else if (action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) | |
379 | + { | |
380 | + val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)!! | |
381 | + val bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1) | |
382 | + Log.v(TAG, " onReceiveBroadcastOfConnection : BluetoothDevice.ACTION_BOND_STATE_CHANGED ($bondState) device: ${device.name}") | |
383 | + if (bondState == BluetoothDevice.BOND_BONDED) | |
384 | + { | |
385 | + // ペアリング完了! | |
386 | + Log.v(TAG, " ----- Device is Paired! ${device.name}") | |
387 | + foundDevice = true | |
388 | + connectBluetoothDevice(device) | |
389 | + } | |
390 | + } | |
391 | + } | |
392 | + catch (e: Exception) | |
393 | + { | |
394 | + Log.w(TAG, "onReceiveBroadcastOfConnection() EXCEPTION" + e.message) | |
395 | + e.printStackTrace() | |
396 | + } | |
397 | + } | |
256 | 398 | } |