OSDN Git Service

データが報告されてくるようになった。
authorMRSa <mrsa@myad.jp>
Mon, 22 Feb 2021 14:32:08 +0000 (23:32 +0900)
committerMRSa <mrsa@myad.jp>
Mon, 22 Feb 2021 14:32:08 +0000 (23:32 +0900)
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/bluetooth/connection/eeg/MindWaveConnection.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/brainwave/BrainwaveDataHolder.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaSetupBluetoothSPP.kt

index dd572e9..89c5157 100644 (file)
@@ -10,7 +10,6 @@ import com.theta360.pluginlibrary.receiver.KeyReceiver
 import com.theta360.pluginlibrary.values.LedColor
 import com.theta360.pluginlibrary.values.LedTarget
 import jp.osdn.gokigen.thetathoughtshutter.R.layout
-import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.BluetoothDeviceFinder
 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult
 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
 import jp.osdn.gokigen.thetathoughtshutter.brainwave.BrainwaveDataHolder
@@ -23,8 +22,7 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
 {
     private val thetaHardwareControl = ThetaHardwareControl(this)
     private val applicationStatus : MyApplicationStatus = MyApplicationStatus()
-    //private val bluetoothFinder = BluetoothDeviceFinder(this, this)
-    private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder())
+    private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder(), this)
 
     companion object
     {
@@ -74,25 +72,23 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
                 }
                 if (keyCode == KeyReceiver.KEYCODE_MEDIA_RECORD) // Modeボタン
                 {
-                    if (applicationStatus.status == MyApplicationStatus.Status.Searching)
-                    {
-                        // ダミー処理 (EEG接続完了)
-                        applicationStatus.status = MyApplicationStatus.Status.Connected
-                    }
-                    else if (applicationStatus.status == MyApplicationStatus.Status.Scanning)
-                    {
-                        // ダミー処理 (高まっている状態)
-                        applicationStatus.status = MyApplicationStatus.Status.Syncing
-                    }
-                    else if (applicationStatus.status == MyApplicationStatus.Status.Syncing)
-                    {
-                        // ダミー処理 (高まるのを待っている状態)
-                        applicationStatus.status = MyApplicationStatus.Status.Scanning
-                    }
-                    else
-                    {
-                        // ダミー処理 (初期化完了)
-                        applicationStatus.status = MyApplicationStatus.Status.Initialized
+                    when (applicationStatus.status) {
+                        MyApplicationStatus.Status.Searching -> {
+                            // ダミー処理 (EEG接続完了)
+                            applicationStatus.status = MyApplicationStatus.Status.Connected
+                        }
+                        MyApplicationStatus.Status.Scanning -> {
+                            // ダミー処理 (高まっている状態)
+                            applicationStatus.status = MyApplicationStatus.Status.Syncing
+                        }
+                        MyApplicationStatus.Status.Syncing -> {
+                            // ダミー処理 (高まるのを待っている状態)
+                            applicationStatus.status = MyApplicationStatus.Status.Scanning
+                        }
+                        else -> {
+                            // ダミー処理 (初期化完了)
+                            applicationStatus.status = MyApplicationStatus.Status.Initialized
+                        }
                     }
                 }
 /*
@@ -198,16 +194,11 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         super.onResume()
         if (isApConnected)
         {
-
+            Log.v(TAG , " isApConnected : $isApConnected")
         }
         initializeBluetooth()
     }
 
-    override fun onPause()
-    {
-        super.onPause()
-    }
-
     private fun initializeBluetooth()
     {
         try
index edaf6da..700f23b 100644 (file)
@@ -35,7 +35,7 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
     {
         override fun onReceive(context: Context, intent: Intent)
         {
-            onReceiveBroadcastOfConnection(this, context, intent)
+            onReceiveBroadcastOfConnection(this, intent)
         }
     }
 
@@ -125,10 +125,10 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
 
     private fun serialCommunicationMain(btSocket: BluetoothSocket)
     {
-        Log.v(TAG, "serialCommunicationMain ")
         var inputStream: InputStream? = null
         try
         {
+            Log.v(TAG, "serialCommunicationMain connect")
             btSocket.connect()
             inputStream = btSocket.inputStream
         }
@@ -138,6 +138,7 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
         }
         if (inputStream == null)
         {
+            Log.v(TAG, "serialCommunicationMain INPUT STREAM IS NULL...")
             return
         }
         if (loggingFlag)
@@ -152,6 +153,8 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
                 e.printStackTrace()
             }
         }
+        Log.v(TAG, " serialCommunicationMain : SERIAL COMMUNICATION STARTED.")
+
 
         // シリアルデータの受信メイン部分
         var previousData = 0xff.toByte()
@@ -161,7 +164,7 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
             try
             {
                 val data: Int = inputStream.read()
-                Log.v(TAG, " RECEIVED ")
+                //Log.v(TAG, " RECEIVED ")
                 val byteData = (data and 0xff).toByte()
                 if (previousData == byteData && byteData == 0xaa.toByte())
                 {
@@ -192,7 +195,6 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
         }
     }
 
-
     override fun foundBluetoothDevice(device: BluetoothDevice)
     {
         try
@@ -210,6 +212,16 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
                 connectBluetoothDevice(device)
                 return
             }
+            else if (device.bondState == BluetoothDevice.BOND_NONE)
+            {
+                // まだペアリングしていない...
+                Log.v(TAG, " NOT PAIRED, START PAIRING : ${device.name}")
+                isPairing = true
+                targetDevice = device
+                device.setPin(byteArrayOf(0x30, 0x30, 0x30, 0x30))
+                device.createBond()
+                return
+            }
 
             if ((!foundDevice)&&(!isPairing))
             {
@@ -225,78 +237,7 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
         {
             e.printStackTrace()
         }
-        //return (device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")))
-    }
-
-
-/*
-    private fun prepareBluetoothDevice(device: BluetoothDevice) : BluetoothSocket?
-    {
-        try
-        {
-            device.setPin(byteArrayOf(0x30,0x30, 0x30, 0x30))
-
-            val result = device.createBond()
-            if (!result)
-            {
-                // ペアリング失敗
-                return (null)
-            }
-
-
-        }
-        catch (e : Exception)
-        {
-            e.printStackTrace()
-        }
-        return (device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")))
-    }
-
-    override fun foundBluetoothDevice(device: BluetoothDevice)
-    {
-        try
-        {
-            Log.v(TAG, " foundBluetoothDevice : ${device.name}")
-            if (foundDevice)
-            {
-                // デバイスがすでに見つかっている
-                Log.v(TAG, " ALREADY FIND BLUETOOTH DEVICE. : $device.name")
-                deviceFinder.stopScan()
-                return
-            }
-            foundDevice = true
-            deviceFinder.stopScan()
-
-            val btSocket = prepareBluetoothDevice(device) // device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
-            val thread = Thread {
-                try
-                {
-                    if (btSocket != null)
-                    {
-                        serialCommunicationMain(btSocket)
-                    }
-                }
-                catch (e: Exception)
-                {
-                    e.printStackTrace()
-                }
-            }
-            if (btSocket != null)
-            {
-                thread.start()
-            }
-            else
-            {
-                Log.v(TAG, " btSocket is NULL.")
-            }
-            scanResult?.foundBluetoothDevice(device)
-        }
-        catch (e: Exception)
-        {
-            e.printStackTrace()
-        }
     }
-*/
 
     private fun connectBluetoothDevice(device : BluetoothDevice?)
     {
@@ -325,13 +266,15 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
             }
             if (btSocket != null)
             {
+                scanResult?.foundBluetoothDevice(device)
                 thread.start()
+                unregisterReceiver()
             }
             else
             {
                 Log.v(TAG, " btSocket is NULL.")
             }
-            scanResult?.foundBluetoothDevice(device)
+            //scanResult?.foundBluetoothDevice(device)
         }
         catch (e: Exception)
         {
@@ -350,7 +293,7 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
      *
      *
      */
-    private fun onReceiveBroadcastOfConnection(receiver: BroadcastReceiver, context: Context, intent: Intent)
+    private fun onReceiveBroadcastOfConnection(receiver: BroadcastReceiver, intent: Intent)
     {
         val action = intent.action
         if (action == null)
index 47eb174..0d15941 100644 (file)
@@ -1,11 +1,15 @@
 package jp.osdn.gokigen.thetathoughtshutter.brainwave
 
 import android.util.Log
+import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
 import java.util.*
 
 class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
 {
-    private val TAG = toString()
+    companion object
+    {
+        private val TAG = BrainwaveDataHolder::class.java.simpleName
+    }
 
     private var valueBuffer: IntArray
     private var currentSummaryData = BrainwaveSummaryData()
@@ -21,15 +25,19 @@ class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
 
     override fun receivedRawData(value: Int)
     {
-        Log.v(TAG, " receivedRawData() : $value");
-        try {
+        //Log.v(TAG, " receivedRawData() : $value");
+        try
+        {
             valueBuffer[currentPosition] = value
             currentPosition++
-            if (currentPosition == maxBufferSize) {
+            if (currentPosition == maxBufferSize)
+            {
                 currentPosition = 0
                 bufferIsFull = true
             }
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
@@ -38,6 +46,7 @@ class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
     {
         if (data != null)
         {
+            Log.v(TAG, " receivedSummaryData() : ${data.size} bytes.")
             if (!currentSummaryData.update(data))
             {
                 // parse failure...
index 591e231..5e40a43 100644 (file)
@@ -1,6 +1,5 @@
 package jp.osdn.gokigen.thetathoughtshutter.theta
 
-import android.graphics.Color
 import android.util.Log
 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.IOperationCallback
 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaOptionGetControl