diff --git a/App5/app/src/main/AndroidManifest.xml b/App5/app/src/main/AndroidManifest.xml index 371b2a6..07511a7 100644 --- a/App5/app/src/main/AndroidManifest.xml +++ b/App5/app/src/main/AndroidManifest.xml @@ -2,10 +2,11 @@ + - - + + = moshi.adapter() val response: Response? = jsonAdapter.fromJson(jjResponse.toString()) if (response?.error != "") { - println("error getting characteristic value: ${response?.error}") + println("error putting characteristic value: ${response?.error}") return null } cc.value = value diff --git a/App5/app/src/main/java/tech/bobalus/app5/MainActivity.kt b/App5/app/src/main/java/tech/bobalus/app5/MainActivity.kt index 87c1a25..84e09b9 100644 --- a/App5/app/src/main/java/tech/bobalus/app5/MainActivity.kt +++ b/App5/app/src/main/java/tech/bobalus/app5/MainActivity.kt @@ -1,9 +1,15 @@ package tech.bobalus.app5 import android.content.Context +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.net.NetworkRequest +import android.net.wifi.WifiInfo import android.net.wifi.WifiManager import android.os.Build import android.os.Bundle +import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.annotation.RequiresApi @@ -11,9 +17,11 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier +import androidx.core.content.ContentProviderCompat.requireContext import tech.bobalus.app5.ui.screens.MainScreen import tech.bobalus.app5.ui.theme.App5Theme + class MainActivity : ComponentActivity() { private val sdk: HkSdk = HkSdk @@ -21,18 +29,37 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - requestPermissions(arrayOf("android.permission.CHANGE_WIFI_MULTICAST_STATE"), 1) - requestPermissions(arrayOf("android.permission.INTERNET"), 1) - requestPermissions(arrayOf("android.permission.ACCESS_NETWORK_STATE"), 1) - - val wifi = getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager - val multicastLock = wifi.createMulticastLock("multicastLock") + val wifiManager = getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager + val multicastLock = wifiManager.createMulticastLock("multicastLock") multicastLock.setReferenceCounted(true) multicastLock.acquire() HkSdk.configure("app5", filesDir.absolutePath) HkSdk.start() + val networkRequest = NetworkRequest.Builder() + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .build() + + // https://stackoverflow.com/a/66572922 + val networkCallback = object : ConnectivityManager.NetworkCallback() { + // network is available for use + override fun onAvailable(network: Network) { + super.onAvailable(network) + HkSdk.start() + Toast.makeText(applicationContext, "Wifi is on!", Toast.LENGTH_SHORT).show() + } + + // lost network connection + override fun onLost(network: Network) { + super.onLost(network) + HkSdk.stop() + Toast.makeText(applicationContext, "Wifi is lost!", Toast.LENGTH_SHORT).show() + } + } + val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + connectivityManager.requestNetwork(networkRequest, networkCallback) + setContent { App5Theme { Surface(