--- /dev/null
+package jp.osdn.gokigen.thetaview.bluetooth
+
+
+import android.util.Log
+import jp.osdn.gokigen.thetaview.camera.theta.operation.IOperationCallback
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionGetControl
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionSetControl
+import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
+import org.json.JSONObject
+
+class ThetaSetupBluetoothSPP(private val sessionIdProvider: IThetaSessionIdProvider, executeUrl : String = "http://192.168.1.1") : IOperationCallback
+{
+ private val getOption = ThetaOptionGetControl(sessionIdProvider, executeUrl)
+ private val setOption = ThetaOptionSetControl(sessionIdProvider, executeUrl)
+ private var readyCallback : IOperationCallback? = null
+
+ companion object
+ {
+ private val TAG = ThetaSetupBluetoothSPP::class.java.simpleName
+ }
+
+ fun setupBluetoothSPP(callback : IOperationCallback?)
+ {
+ this.readyCallback = callback
+ getOption.getOptions("[ \"_bluetoothRole\", \"_bluetoothPower\", \"_bluetoothClassicEnable\" ]", this)
+ }
+
+ private fun setBluetoothPowerOn(bluetoothPower : String?)
+ {
+ if (bluetoothPower == null)
+ {
+ readyCallback?.operationExecuted(-1, "")
+ return
+ }
+ try
+ {
+ if (!bluetoothPower.contains("ON"))
+ {
+ //
+ Log.v(TAG, " --- Bluetooth Power ON ---")
+ setOption.setOptions("\"_bluetoothPower\" : \"ON\"", (sessionIdProvider.sessionId.isEmpty()), object : IOperationCallback {
+ override fun operationExecuted(result: Int, resultStr: String?)
+ {
+ readyCallback?.operationExecuted(result, resultStr)
+ }
+ })
+ }
+ readyCallback?.operationExecuted(0, "OK")
+ return
+ }
+ catch (e: Exception)
+ {
+ e.printStackTrace()
+ }
+ readyCallback?.operationExecuted(-1, "")
+ }
+
+ private fun setBluetoothClassicEnable(bluetoothClassicEnable: Boolean, bluetoothPower: String?)
+ {
+ try
+ {
+ if (!bluetoothClassicEnable)
+ {
+ //
+ Log.v(TAG, " --- Bluetooth Classic Enable ---")
+ setOption.setOptions("\"_bluetoothClassicEnable\" : \"true\"",(sessionIdProvider.sessionId.isEmpty()), object : IOperationCallback {
+ override fun operationExecuted(result: Int, resultStr: String?)
+ {
+ if (resultStr != null)
+ {
+ Log.v(TAG, "_bluetoothClassicEnable : true $resultStr ")
+ setBluetoothPowerOn(bluetoothPower)
+ }
+ else
+ {
+ readyCallback?.operationExecuted(-1, resultStr)
+ }
+ }
+ })
+ }
+ 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\"", (sessionIdProvider.sessionId.isEmpty()), 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)
+ }
+ }
+ })
+ }
+ else
+ {
+ setBluetoothClassicEnable(bluetoothClassicEnable, bluetoothPower)
+ }
+ return
+ }
+ catch (e: Exception)
+ {
+ e.printStackTrace()
+ }
+ }
+ catch (e : Exception)
+ {
+ e.printStackTrace()
+ }
+ readyCallback?.operationExecuted(-1, resultStr)
+ }
+}
--- /dev/null
+package jp.osdn.gokigen.thetaview.camera.theta.operation
+
+import android.util.Log
+import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
+import jp.osdn.gokigen.thetaview.utils.communication.SimpleHttpClient
+
+class ThetaOptionGetControl(private val sessionIdProvider: IThetaSessionIdProvider, private val executeUrl : String = "http://192.168.1.1")
+{
+ private val httpClient = SimpleHttpClient()
+
+ /**
+ *
+ *
+ */
+ fun getOptions(options: String, callBack: IOperationCallback? = null)
+ {
+ //Log.v(TAG, "getOptions() MSG : $options")
+ try
+ {
+ val thread = Thread {
+ try
+ {
+ val setOptionsUrl = "${executeUrl}/osc/commands/execute"
+ 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()))
+ {
+ Log.v(TAG, " getOptions() : $result (${setOptionsUrl})")
+ callBack?.operationExecuted(0, result)
+ }
+ else
+ {
+ Log.v(TAG, "getOptions() reply is null or empty. $postData (${setOptionsUrl})")
+ callBack?.operationExecuted(-1, "")
+ }
+ }
+ catch (e: Exception)
+ {
+ Log.v(TAG, "getOptions() Exception : $options")
+ e.printStackTrace()
+ callBack?.operationExecuted(-1, e.localizedMessage)
+ }
+ }
+ thread.start()
+ }
+ catch (e: Exception)
+ {
+ e.printStackTrace()
+ callBack?.operationExecuted(-1, e.localizedMessage)
+ }
+ }
+
+ companion object
+ {
+ private val TAG = ThetaOptionGetControl::class.java.simpleName
+ private const val timeoutMs = 1500
+ }
+
+}
\ No newline at end of file
import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
import jp.osdn.gokigen.thetaview.utils.communication.SimpleHttpClient
-class ThetaOptionUpdateControl(private val sessionIdProvider: IThetaSessionIdProvider)
+class ThetaOptionSetControl(private val sessionIdProvider: IThetaSessionIdProvider, private val executeUrl : String = "http://192.168.1.1")
{
private val httpClient = SimpleHttpClient()
val thread = Thread {
try
{
- val setOptionsUrl = "http://192.168.1.1/osc/commands/execute"
+ val setOptionsUrl = "${executeUrl}/osc/commands/execute"
val postData = if (useOSCv2) "{\"name\":\"camera.setOptions\",\"parameters\":{\"timeout\":0, \"options\": {$options}}}" else "{\"name\":\"camera.setOptions\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"options\": { $options }}}"
val result: String? = httpClient.httpPostWithHeader(setOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
if ((result != null) && (result.isNotEmpty()))
companion object
{
- private val TAG = ThetaOptionUpdateControl::class.java.simpleName
+ private val TAG = ThetaOptionSetControl::class.java.simpleName
private const val timeoutMs = 1500
}
}