OSDN Git Service

Bluetoothの使用準備を整えた。
authorMRSa <mrsa@myad.jp>
Thu, 18 Feb 2021 15:36:46 +0000 (00:36 +0900)
committerMRSa <mrsa@myad.jp>
Thu, 18 Feb 2021 15:36:46 +0000 (00:36 +0900)
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MyApplicationStatus.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaSetupBluetoothSPP.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/operation/ThetaOptionSetControl.kt

index 8312fde..8374b50 100644 (file)
@@ -72,6 +72,16 @@ class MainActivity : PluginActivity()
                         // ダミー処理 (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
                     {
                         // ダミー処理 (初期化完了)
@@ -110,17 +120,26 @@ class MainActivity : PluginActivity()
                 MyApplicationStatus.Status.Searching -> {
                     Log.v(TAG, " SEARCHING")
                     thetaHardwareControl.controlLED(LedTarget.LED3, 250, LedColor.GREEN)  // WIFIランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
                 }
                 MyApplicationStatus.Status.Connected -> {
                     Log.v(TAG, " CONNECTED")
-                    thetaHardwareControl.controlLED(LedTarget.LED3,  0, LedColor.GREEN)  // WIFIランプ
-                    thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)    // 赤ランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED3,  0, LedColor.GREEN)   // WIFIランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
+                    thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
                 }
                 MyApplicationStatus.Status.Scanning -> {
                     Log.v(TAG, " SCANNING")
-                    thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)  // WIFIランプ
-                    thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)    // 赤ランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
+                    thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
+                }
+                MyApplicationStatus.Status.Syncing -> {
+                    Log.v(TAG, " SYNCING")
+                    thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
+                    thetaHardwareControl.controlLED(LedTarget.LED6, 0, LedColor.BLUE)     // Liveランプ (ON)
+                    thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
                 }
                 MyApplicationStatus.Status.FailedInitialize -> {
                     Log.v(TAG, " FAILED INITIALIZE")
@@ -169,8 +188,7 @@ class MainActivity : PluginActivity()
                     val setupBluetooth = ThetaSetupBluetoothSPP("http://localhost:8080")
                     setupBluetooth.setupBluetoothSPP(object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
                     {
-                        Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
-
+                        Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr ($result)")
 
                         if (result == 0)
                         {
index ba6757b..66b1133 100644 (file)
@@ -12,5 +12,6 @@ data class MyApplicationStatus(val defaultStatus : Status = Status.Undefined)
         Searching,
         Connected,
         Scanning,
+        Syncing,
     }
 }
index 18c8da7..591e231 100644 (file)
@@ -7,60 +7,143 @@ import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaOptionGetControl
 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaOptionSetControl
 import org.json.JSONObject
 
-class ThetaSetupBluetoothSPP(executeUrl : String = "http://192.168.1.1")
+class ThetaSetupBluetoothSPP(executeUrl : String = "http://192.168.1.1") : IOperationCallback
 {
     private val getOption = ThetaOptionGetControl(executeUrl)
     private val setOption = ThetaOptionSetControl(executeUrl)
+    private var readyCallback : IOperationCallback? = null
+
+    companion object
+    {
+        private val TAG = ThetaSetupBluetoothSPP::class.java.simpleName
+    }
 
     fun setupBluetoothSPP(callback : IOperationCallback?)
     {
-        getOption.getOptions("[ \"_bluetoothRole\", \"_bluetoothPower\", \"_bluetoothClassicEnable\" ]", object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
+        this.readyCallback = callback
+        getOption.getOptions("[ \"_bluetoothRole\", \"_bluetoothPower\", \"_bluetoothClassicEnable\" ]", this)
+    }
+
+    private fun setBluetoothPowerOn(bluetoothPower : String?)
+    {
+        if (bluetoothPower == null)
+        {
+            readyCallback?.operationExecuted(-1, "")
+            return
+        }
+        try
         {
-            Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
-            if (resultStr == null)
+            if (!bluetoothPower.contains("ON"))
             {
-                callback?.operationExecuted(-1, resultStr)
-                return
+                //
+                Log.v(TAG, " --- Bluetooth Power ON ---")
+                setOption.setOptions("\"_bluetoothPower\" : \"ON\"", object : IOperationCallback {
+                    override fun operationExecuted(result: Int, resultStr: String?)
+                    {
+                        readyCallback?.operationExecuted(result, resultStr)
+                    }
+                })
             }
-            try
-            {
-                val stateObject = JSONObject(resultStr).getJSONObject("results").getJSONObject("options")
-                try
-                {
-                    // Bluetoothの状態を確認
-                    val bluetoothClassicEnable = stateObject.getBoolean("_bluetoothClassicEnable")
-                    val bluetoothPower = stateObject.getString("_bluetoothPower")
-                    val bluetoothRole = stateObject.getString("_bluetoothRole")
+            readyCallback?.operationExecuted(0, "OK")
+            return
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, "")
+    }
 
-                    if (bluetoothRole.contains("Central"))
+    private fun setBluetoothClassicEnable(bluetoothClassicEnable: Boolean, bluetoothPower: String?)
+    {
+        try
+        {
+            if (!bluetoothClassicEnable)
+            {
+                //
+                Log.v(TAG, " --- Bluetooth Classic Enable ---")
+                setOption.setOptions("\"_bluetoothClassicEnable\" : \"true\"", object : IOperationCallback {
+                    override fun operationExecuted(result: Int, resultStr: String?)
                     {
-                        // Central: ON ⇒ OFF にする
-                        //setOption.setOptions("")
-                        Log.v(TAG, " --- CHANGE TO 'Peripheral' ---")
+                        if (resultStr != null)
+                        {
+                            Log.v(TAG, "_bluetoothClassicEnable : true  $resultStr ")
+                            setBluetoothPowerOn(bluetoothPower)
+                        }
+                        else
+                        {
+                            readyCallback?.operationExecuted(-1, resultStr)
+                        }
                     }
-                    Log.v(TAG, " BLUETOOTH CLASSIC : $bluetoothClassicEnable  POWER: $bluetoothPower  ROLE: $bluetoothRole")
+                })
+            }
+            else
+            {
+                setBluetoothPowerOn(bluetoothPower)
+            }
+            return
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, "")
+    }
 
+    override fun operationExecuted(result: Int, resultStr: String?)
+    {
+        Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
+        if (resultStr == null)
+        {
+            readyCallback?.operationExecuted(-1, resultStr)
+            return
+        }
+        try
+        {
+            val stateObject = JSONObject(resultStr).getJSONObject("results").getJSONObject("options")
+            try
+            {
+                // Bluetoothの状態を確認
+                val bluetoothClassicEnable = stateObject.getBoolean("_bluetoothClassicEnable")
+                val bluetoothPower = stateObject.getString("_bluetoothPower")
+                val bluetoothRole = stateObject.getString("_bluetoothRole")
+
+                Log.v(TAG, " BLUETOOTH CLASSIC : $bluetoothClassicEnable  POWER: $bluetoothPower  ROLE: $bluetoothRole")
 
+                if (bluetoothRole.contains("Central"))
+                {
+                    // Central: ON ⇒ OFF にする
+                    Log.v(TAG, " --- CHANGE TO 'Peripheral' ---")
+                    setOption.setOptions("\"_bluetoothRole\" : \"Peripheral\"", object : IOperationCallback {
+                        override fun operationExecuted(result: Int, resultStr: String?)
+                        {
+                            if (resultStr != null)
+                            {
+                                Log.v(TAG, "_bluetoothRole : Peripheral  $resultStr ")
+                                setBluetoothClassicEnable(bluetoothClassicEnable, bluetoothPower)
+                            }
+                            else
+                            {
+                                readyCallback?.operationExecuted(-1, resultStr)
+                            }
+                        }
+                    })
                 }
-                catch (e: Exception)
+                else
                 {
-                    e.printStackTrace()
+                    setBluetoothClassicEnable(bluetoothClassicEnable, bluetoothPower)
                 }
-
-
-
-
-
-                callback?.operationExecuted(result, resultStr)
+                return
             }
-            catch (e : Exception)
+            catch (e: Exception)
             {
                 e.printStackTrace()
             }
-        }})
-    }
-    companion object
-    {
-        private val TAG = ThetaSetupBluetoothSPP::class.java.simpleName
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, resultStr)
     }
 }
index 570079a..9e0f037 100644 (file)
@@ -24,7 +24,7 @@ class ThetaOptionSetControl(private val executeUrl : String = "http://192.168.1.
                     val result: String? = httpClient.httpPostWithHeader(setOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
                     if ((result != null) && (result.isNotEmpty()))
                     {
-                        Log.v(TAG, " setOptions() : $result (${setOptionsUrl})")
+                        Log.v(TAG, " setOptions() : $options : $result (${setOptionsUrl})")
                         callBack?.operationExecuted(0, result)
                     }
                     else