本文介绍了移动热点名称和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在android studio中以编程方式获取移动热点的名称和密码.我该怎么做?

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);

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
   Toast.makeText(this,"SSID:"+wifiInfo.getSSID(),Toast.LENGTH_LONG).show();

此代码为我提供了我连接的wifi的SSID.我需要移动热点的名称.

This code gives me SSID of wifi I am connected to. I need name of my Mobile hotspot.

推荐答案

您可以使用反射在API< 26中获得热点的wifi配置.这不是推荐的方法,但是如果您需要它,那么就在这里.

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;  
    }   
  }

然后您可以使用 WifiConfiguration 对象获取其详细信息:

And then you can use the WifiConfiguration object to get its details:

currentConfig.SSID
currentConfig.preSharedKey

这篇关于移动热点名称和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:51