OSDN Git Service

Bluetoothの初期化準備中。
authorMRSa <mrsa@myad.jp>
Wed, 17 Feb 2021 14:50:56 +0000 (23:50 +0900)
committerMRSa <mrsa@myad.jp>
Wed, 17 Feb 2021 14:50:56 +0000 (23:50 +0900)
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaSetupBluetoothSPP.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/operation/ThetaOptionGetControl.kt

index cd793b4..8312fde 100644 (file)
@@ -10,6 +10,8 @@ import com.theta360.pluginlibrary.values.LedColor
 import com.theta360.pluginlibrary.values.LedTarget
 import jp.osdn.gokigen.thetathoughtshutter.R.layout
 import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaHardwareControl
+import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaSetupBluetoothSPP
+import jp.osdn.gokigen.thetathoughtshutter.theta.operation.IOperationCallback
 import java.lang.Exception
 
 class MainActivity : PluginActivity()
@@ -90,6 +92,7 @@ class MainActivity : PluginActivity()
 
             }
         })
+
         updateStatus(applicationStatus.status)
     }
 
@@ -148,6 +151,7 @@ class MainActivity : PluginActivity()
         {
 
         }
+        initializeBluetooth()
     }
 
     override fun onPause()
@@ -155,6 +159,39 @@ class MainActivity : PluginActivity()
         super.onPause()
     }
 
+    private fun initializeBluetooth()
+    {
+        try
+        {
+            val thread = Thread {
+                try
+                {
+                    val setupBluetooth = ThetaSetupBluetoothSPP("http://localhost:8080")
+                    setupBluetooth.setupBluetoothSPP(object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
+                    {
+                        Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
+
+
+                        if (result == 0)
+                        {
+                            // Bluetoothの初期化終了
+                            applicationStatus.status = MyApplicationStatus.Status.Initialized
+                            updateStatus(applicationStatus.status)
+                        }
+                    }})
+                }
+                catch (e: Exception)
+                {
+                    e.printStackTrace()
+                }
+            }
+            thread.start()
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+    }
 }
 
 //
index 03ac5f8..18c8da7 100644 (file)
@@ -1,20 +1,62 @@
 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
 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaOptionSetControl
+import org.json.JSONObject
 
 class ThetaSetupBluetoothSPP(executeUrl : String = "http://192.168.1.1")
 {
     private val getOption = ThetaOptionGetControl(executeUrl)
     private val setOption = ThetaOptionSetControl(executeUrl)
 
-    fun setupBluetoothSPP()
+    fun setupBluetoothSPP(callback : IOperationCallback?)
     {
         getOption.getOptions("[ \"_bluetoothRole\", \"_bluetoothPower\", \"_bluetoothClassicEnable\" ]", object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
         {
             Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
+            if (resultStr == null)
+            {
+                callback?.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")
+
+                    if (bluetoothRole.contains("Central"))
+                    {
+                        // Central: ON ⇒ OFF にする
+                        //setOption.setOptions("")
+                        Log.v(TAG, " --- CHANGE TO 'Peripheral' ---")
+                    }
+                    Log.v(TAG, " BLUETOOTH CLASSIC : $bluetoothClassicEnable  POWER: $bluetoothPower  ROLE: $bluetoothRole")
+
+
+                }
+                catch (e: Exception)
+                {
+                    e.printStackTrace()
+                }
+
+
+
+
+
+                callback?.operationExecuted(result, resultStr)
+            }
+            catch (e : Exception)
+            {
+                e.printStackTrace()
+            }
         }})
     }
     companion object
index 4554b44..10c75eb 100644 (file)
@@ -20,7 +20,7 @@ class ThetaOptionGetControl(private val executeUrl : String = "http://192.168.1.
                 try
                 {
                     val setOptionsUrl = "${executeUrl}/osc/commands/execute"
-                    val postData = "{\"name\":\"camera.getOptions\",\"parameters\":{\"timeout\":0, \"optionNames\": {$options}}}"
+                    val postData = "{\"name\":\"camera.getOptions\",\"parameters\":{\"timeout\":0, \"optionNames\": $options}}"
                     val result: String? = httpClient.httpPostWithHeader(setOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
                     if ((result != null) && (result.isNotEmpty()))
                     {