问题
I have to get the name and password of my mobile hotspot programmatically in android studio. How do I do it?
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Toast.makeText(this,"SSID:"+wifiInfo.getSSID(),Toast.LENGTH_LONG).show();
This code gives me SSID of wifi I am connected to. I need name of my Mobile hotspot.
回答1:
You can get the wificonfiguration of your hotspot in API<26 using reflection. Its not the recommended way but if you need it bad, then here it is.
private WifiConfiguration currentConfig;
private WifiConfiguration getWifiApConfiguration() {
try {
Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
return (WifiConfiguration) method.invoke(wifiManager);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return null;
}
}
And then you can use the WifiConfiguration object to get its details:
currentConfig.SSID
currentConfig.preSharedKey
来源:https://stackoverflow.com/questions/54553782/mobile-hotspot-name-and-password