4

I have created a WI-FI Hotspot in my app and then I try to show a toast message when a device connects to my hotspot.

I have tried this :

private var onClientConnected = object : BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
         Toast.makeText(applicationContext,"Client Connected!",Toast.LENGTH_LONG).show()
    }
}

and register it using this:

registerReceiver(onClientConnected, IntentFilter("android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED"))

But at run time when a device connects to hotspot. I get no Toast message. Is there anything that I have missed or doing wrong?

Jazz
  • 214
  • 2
  • 10
  • Don't think there is a built in method to determine the number of devices connected to your hotspot. However, this might be helpful - [Android Wifi Hotspot Manager](https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class), let me know if that helps – Nero Jan 02 '19 at 11:50
  • @Nero Thanks, But I don't want to get numbers of connected device. I want to show a toast msg when a device connects to hotspot. – Jazz Jan 02 '19 at 11:56
  • but you might be able to use that around your logic? It might not be a future proof answer but you can run the method every 10 seconds or so to determine if the number of connected devices has increased. If it has, that means a new device has been connected. I am still searching for correct answer which is more robust. – Nero Jan 02 '19 at 11:59
  • @Nero Thank for your help, but my project is in kotlin and the link you shared is in java. – Jazz Jan 02 '19 at 12:38
  • I'm not sure what's wrong. Check permissions and try to expose the BroadcastReceiver inside the manifest (see https://stackoverflow.com/a/33983335/603270) – shkschneider Jan 02 '19 at 13:42

0 Answers0