Skip to content

Commit

Permalink
watch wifi state
Browse files Browse the repository at this point in the history
  • Loading branch information
shabunin committed Mar 8, 2023
1 parent 1e779d0 commit 7bcbf99
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
5 changes: 3 additions & 2 deletions App5/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand Down
6 changes: 5 additions & 1 deletion App5/app/src/main/java/tech/bobalus/app5/HkSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ object HkSdk : MobileReceiver, ViewModel() {
println("mdns discovery started")
running = true
}
fun stop() {
controller?.stopDiscovery()
running = false
}

@OptIn(ExperimentalStdlibApi::class)
override fun onCharacteristic(p0: String?) {
Expand Down Expand Up @@ -456,7 +460,7 @@ object HkSdk : MobileReceiver, ViewModel() {
val jsonAdapter: JsonAdapter<Response> = 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
Expand Down
39 changes: 33 additions & 6 deletions App5/app/src/main/java/tech/bobalus/app5/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
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
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
@RequiresApi(Build.VERSION_CODES.M)
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(
Expand Down

0 comments on commit 7bcbf99

Please sign in to comment.