|
4 | 4 | Allows you to watch for wi-fi changes on an Android device. |
5 | 5 |
|
6 | 6 | ```kotlin |
7 | | -class WiFiViewModel(context: Context) : ViewModel() { |
8 | | - val values = WifiLiveData( |
9 | | - WifiMonitor.WifiMonitorBuilder() |
10 | | - .context(context) |
11 | | - .build() |
12 | | - ) |
13 | | -} |
14 | | - |
15 | | -class MainActivity : AppCompatActivity() { |
16 | | - override fun onCreate(savedInstanceState: Bundle?) { |
17 | | - super.onCreate(savedInstanceState) |
18 | | - setContentView(R.layout.activity_main) |
19 | | - |
20 | | - val viewModel = WiFiViewModel(this) |
| 7 | +val wifiMonitor = WifiMonitor.Builder() |
| 8 | + .listener(wifiListener) |
| 9 | + .wifiManager(wifiManager) |
| 10 | + .permissionChecker(permissionChecker) |
| 11 | + .build(context) |
21 | 12 |
|
22 | | - viewModel.values.observe(this) { |
23 | | - println(it.band) |
24 | | - println(it.bssid) |
25 | | - println(it.state.name) |
26 | | - } |
| 13 | +val callbackFlow = wifiMonitor.monitor() |
27 | 14 |
|
28 | | - // Or more simply |
29 | | - val monitorFlow = WifiMonitor.WifiMonitorBuilder() |
30 | | - .context(context) |
31 | | - .build() |
32 | | - .monitor() |
33 | | - |
34 | | - // Or without passing the context |
35 | | - val monitorFlow = WifiMonitor.WifiMonitorBuilder() |
36 | | - .listener(wifiListener) |
37 | | - .wifiManager(wifiManager) |
38 | | - .permissionChecker(permissionChecker) |
39 | | - .build() |
40 | | - .monitor() |
41 | | - |
42 | | - // last known information |
43 | | - monitorFlow.first() |
44 | | - } |
45 | | -} |
| 15 | +// use the flow as you like |
46 | 16 |
|
47 | 17 | ``` |
48 | 18 |
|
@@ -83,4 +53,4 @@ When the wi-fi is connected and your user didn't grant the `ACCESS_FINE_LOCATION |
83 | 53 | When the phone is not connected to any Wi-Fi. |
84 | 54 |
|
85 | 55 | #### `UNKNOWN` |
86 | | -When the library could not recognize the Wi-Fi state. |
| 56 | +When the library could not recognize the Wi-Fi state. |
0 commit comments