OSDN Git Service

データを取得するための準備。
authorMRSa <mrsa@myad.jp>
Mon, 22 Feb 2021 14:59:03 +0000 (23:59 +0900)
committerMRSa <mrsa@myad.jp>
Mon, 22 Feb 2021 14:59:03 +0000 (23:59 +0900)
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/bluetooth/connection/BluetoothDeviceFinder.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

index 89c5157..33dec9c 100644 (file)
@@ -132,6 +132,30 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         applicationStatus.status = MyApplicationStatus.Status.Searching
     }
 
+    private fun disconnectToEEG()
+    {
+        try
+        {
+            val thread = Thread {
+                try
+                {
+                    bluetoothConnection.disconnect()
+                }
+                catch (e: Exception)
+                {
+                    e.printStackTrace()
+                }
+            }
+            thread.start()
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+        applicationStatus.status = MyApplicationStatus.Status.Initialized
+    }
+
+
     private fun updateStatus(currentStatus : MyApplicationStatus.Status)
     {
         try
@@ -199,6 +223,20 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         initializeBluetooth()
     }
 
+    override fun onPause()
+    {
+        super.onPause()
+        try
+        {
+            disconnectToEEG()
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
+
     private fun initializeBluetooth()
     {
         try
index 9a45116..da4d269 100644 (file)
@@ -10,7 +10,7 @@ import android.bluetooth.le.ScanResult
 import android.content.Context
 import android.util.Log
 
-class BluetoothDeviceFinder(private val context: Activity, private val scanResult: IBluetoothScanResult) : BluetoothAdapter.LeScanCallback
+class BluetoothDeviceFinder(private val context: Activity, private val scanResult: IBluetoothScanResult) : BluetoothAdapter.LeScanCallback, ScanCallback()
 {
 
     companion object
@@ -33,38 +33,8 @@ class BluetoothDeviceFinder(private val context: Activity, private val scanResul
             Log.v(TAG, " stopScan()")
             val btMgr: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
             btMgr.adapter.cancelDiscovery()
-            scanner?.flushPendingScanResults((object: ScanCallback() {
-                override fun onScanFailed(errorCode: Int)
-                {
-
-                }
-
-                override fun onScanResult(callbackType: Int, result: ScanResult?)
-                {
-
-                }
-
-                override fun onBatchScanResults(results: MutableList<ScanResult>?)
-                {
-
-                }
-            }))
-            scanner?.stopScan(object: ScanCallback() {
-                override fun onScanFailed(errorCode: Int)
-                {
-
-                }
-
-                override fun onScanResult(callbackType: Int, result: ScanResult?)
-                {
-
-                }
-
-                override fun onBatchScanResults(results: MutableList<ScanResult>?)
-                {
-
-                }
-            })
+            scanner?.flushPendingScanResults(this)
+            scanner?.stopScan(this)
         }
         catch (e : Exception)
         {
@@ -110,55 +80,13 @@ class BluetoothDeviceFinder(private val context: Activity, private val scanResul
             }
             //adapter.startDiscovery()
             scanner = adapter.bluetoothLeScanner
-            scanner?.startScan(object: ScanCallback() {
-                override fun onScanFailed(errorCode: Int)
-                {
-                    //super.onScanFailed(errorCode)
-                    Log.v(TAG, " onScanFailed : $errorCode")
-                }
-
-                override fun onScanResult(callbackType: Int, result: ScanResult?)
-                {
-                    //super.onScanResult(callbackType, result)
-                    val device = result?.device
-                    Log.v(TAG, " onScanResult($callbackType, ${device?.name}) ")
-                    val findDevice = (device?.name)?.contains(targetDeviceName)
-                    if ((findDevice != null)&&(findDevice))
-                    {
-                        Log.v(TAG, " FIND DEVICE : $targetDeviceName")
-                        scanResult.foundBluetoothDevice(device)
-                        scanner?.stopScan(object : ScanCallback() {
-                            override fun onScanFailed(errorCode: Int)
-                            {
-                                Log.v(TAG, " stopScan::onScanFailed : $errorCode")
-                            }
-                            override fun onScanResult(callbackType: Int, result: ScanResult?)
-                            {
-                                Log.v(TAG, " stopScan::onScanResult : $callbackType")
-                            }
-                            override fun onBatchScanResults(results: MutableList<ScanResult>?)
-                            {
-                                Log.v(TAG, " stopScan::onBatchScanResults ")
-                            }
-                        })
-                    }
-                }
-
-                override fun onBatchScanResults(results: MutableList<ScanResult>?)
-                {
-                    //super.onBatchScanResults(results)
-                    Log.v(TAG, " onBatchScanResults ")
-                }
-            })
+            scanner?.startScan(this)
         }
         catch (e: Exception)
         {
             e.printStackTrace()
-            Log.v(TAG, "Bluetooth LE SCAN EXCEPTION...")
-            //messageToShow.showMessage(R.string.scan_fail_via_bluetooth)
+            Log.v(TAG, "Bluetooth SCAN EXCEPTION...")
         }
-        //Log.v(TAG, "Bluetooth SCAN STOPPED.")
-        //scanResult.notFindBluetoothDevice()
     }
 
     override fun onLeScan(device: BluetoothDevice, rssi: Int, scanRecord: ByteArray?)
@@ -180,4 +108,29 @@ class BluetoothDeviceFinder(private val context: Activity, private val scanResul
             e.printStackTrace()
         }
     }
+
+    override fun onScanFailed(errorCode: Int)
+    {
+        Log.v(TAG, " onScanFailed : $errorCode")
+    }
+
+    override fun onScanResult(callbackType: Int, result: ScanResult?)
+    {
+        //super.onScanResult(callbackType, result)
+        val device = result?.device
+        Log.v(TAG, " onScanResult($callbackType, ${device?.name}) ")
+        val findDevice = (device?.name)?.contains(targetDeviceName)
+        if ((findDevice != null)&&(findDevice))
+        {
+            Log.v(TAG, " FIND DEVICE : $targetDeviceName")
+            scanResult.foundBluetoothDevice(device)
+            scanner?.stopScan(this)
+        }
+    }
+
+    override fun onBatchScanResults(results: MutableList<ScanResult>?)
+    {
+        //super.onBatchScanResults(results)
+        Log.v(TAG, " onBatchScanResults ")
+    }
 }
index 700f23b..0730341 100644 (file)
@@ -59,6 +59,11 @@ class MindWaveConnection(private val activity : Activity, private val dataReceiv
         }
     }
 
+    fun disconnect()
+    {
+        foundDevice = false
+    }
+
     private fun registerReceiver()
     {
         try
index 0d15941..5adfb76 100644 (file)
@@ -46,11 +46,22 @@ class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
     {
         if (data != null)
         {
-            Log.v(TAG, " receivedSummaryData() : ${data.size} bytes.")
+            // Log.v(TAG, " receivedSummaryData() : ${data.size} bytes.")
             if (!currentSummaryData.update(data))
             {
                 // parse failure...
                 Log.v(TAG, " FAIL : PARSE EEG SUMMARY DATA (" + data.size + ")")
+                return
+            }
+            try
+            {
+                val attention = currentSummaryData.getAttention()
+                val mediation = currentSummaryData.getMediation()
+                Log.v(TAG, "  ATTENTION : $attention   MEDIATION : $mediation")
+            }
+            catch (e : Exception)
+            {
+                e.printStackTrace()
             }
         }
     }