47 lines
1.7 KiB
Kotlin
47 lines
1.7 KiB
Kotlin
package com.omixlab.mosis.unity
|
|
|
|
import android.util.Log
|
|
import android.content.ComponentName
|
|
import android.content.Context.BIND_AUTO_CREATE
|
|
import android.content.Intent
|
|
import android.content.ServiceConnection
|
|
import android.os.IBinder
|
|
import androidx.core.content.ContextCompat.startForegroundService
|
|
import com.omixlab.mosis.IMosisService
|
|
import com.unity3d.player.UnityPlayer
|
|
|
|
class MyKotlinPlugin {
|
|
companion object {
|
|
val instance: MyKotlinPlugin by lazy { MyKotlinPlugin() }
|
|
@JvmStatic
|
|
fun StartMosisService() {
|
|
instance.startRemoteService()
|
|
}
|
|
}
|
|
var remote_service: IMosisService? = null
|
|
val connection = object : ServiceConnection {
|
|
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
|
Log.d("MosisTest", "Service Connected")
|
|
remote_service = IMosisService.Stub.asInterface(service)
|
|
Log.d("MosisTest", "Number: ${remote_service?.number}")
|
|
serviceConnected(service)
|
|
}
|
|
override fun onServiceDisconnected(arg0: ComponentName) {
|
|
Log.d("MosisTest", "Service Disconnected")
|
|
}
|
|
}
|
|
fun startRemoteService() {
|
|
val intent = Intent("com.omixlab.mosis.SERVICE")
|
|
intent.setPackage("com.omixlab.mosis")
|
|
val currentActivity = UnityPlayer.currentActivity
|
|
try {
|
|
startForegroundService(currentActivity, intent)
|
|
val result = currentActivity.bindService(intent, connection, BIND_AUTO_CREATE)
|
|
Log.d("MosisTest", "Bind result: $result")
|
|
} catch (e: Exception) {
|
|
Log.e("MosisTest", "Bind failed", e)
|
|
}
|
|
}
|
|
external fun serviceConnected(binder: IBinder)
|
|
}
|