• R/O
  • HTTP
  • SSH
  • HTTPS

ThetaThoughtShutter: Commit

Ricoh Theta x Nurosky Shutter


Commit MetaInfo

Révision82a012abf18e99f6e27665a2939e0ae2bee0b657 (tree)
l'heure2021-02-21 23:36:48
AuteurMRSa <mrsa@myad...>
CommiterMRSa

Message de Log

ペアリング途中。

Change Summary

Modification

--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -23,7 +23,6 @@
2323 <activity android:name=".MainActivity">
2424 <intent-filter>
2525 <action android:name="android.intent.action.MAIN" />
26-
2726 <category android:name="android.intent.category.LAUNCHER" />
2827 </intent-filter>
2928 </activity>
--- a/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/bluetooth/connection/eeg/MindWaveConnection.kt
+++ b/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/bluetooth/connection/eeg/MindWaveConnection.kt
@@ -3,6 +3,10 @@ package jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg
33 import android.app.Activity
44 import android.bluetooth.BluetoothDevice
55 import android.bluetooth.BluetoothSocket
6+import android.content.BroadcastReceiver
7+import android.content.Context
8+import android.content.Intent
9+import android.content.IntentFilter
610 import android.util.Log
711 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult
812 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.BluetoothDeviceFinder
@@ -13,23 +17,35 @@ import java.io.InputStream
1317 import java.util.*
1418 import kotlin.experimental.and
1519
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
1721 {
1822 companion object
1923 {
2024 private val TAG = MindWaveConnection::class.java.simpleName
2125 }
2226
23- private val deviceFinder = BluetoothDeviceFinder(context, this)
27+ private val deviceFinder = BluetoothDeviceFinder(activity, this)
2428 private var fileLogger: BrainwaveFileLogger? = null
2529 private var foundDevice = false
30+ private var isPairing = false
2631 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+ }
2741
2842 fun connect(deviceName: String, loggingFlag: Boolean = false)
2943 {
3044 Log.v(TAG, " BrainWaveMobileCommunicator::connect() : $deviceName Logging : $loggingFlag")
3145 try
3246 {
47+ registerReceiver()
48+
3349 this.loggingFlag = loggingFlag
3450
3551 // Bluetooth のサービスを取得、BLEデバイスをスキャンする
@@ -43,6 +59,33 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav
4359 }
4460 }
4561
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+
4689 private fun parseReceivedData(data: ByteArray)
4790 {
4891 // 受信データブロック1つ分
@@ -154,21 +197,29 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav
154197 {
155198 try
156199 {
157- Log.v(TAG, " foundBluetoothDevice : ${device.name}")
200+ Log.v(TAG, " foundBluetoothDevice : ${device.name} : ${device.bondState}")
158201
159202 deviceFinder.stopScan()
160203
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)
167205 {
168- // ペアリング失敗
206+ // すでにペアリング済み
207+ Log.v(TAG, " ALREADY PAIRED ")
208+ isPairing = true
209+ targetDevice = device
210+ connectBluetoothDevice(device)
211+ return
169212 }
170213
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()
172223 }
173224 catch (e : Exception)
174225 {
@@ -247,10 +298,101 @@ class MindWaveConnection(context : Activity, private val dataReceiver: IBrainwav
247298 }
248299 */
249300
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+
250342 override fun notFindBluetoothDevice()
251343 {
252344 Log.v(TAG, " notFindBluetoothDevice()")
253345 scanResult?.notFindBluetoothDevice()
254346 deviceFinder.stopScan()
255347 }
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+ }
256398 }
Afficher sur ancien navigateur de dépôt.