【问题标题】:Create Wifi Hotspot Configuration in android在android中创建Wifi热点配置
【发布时间】:2013-06-17 16:56:25
【问题描述】:

我正在使用 android 4.1.1 ...我正在制作一个应用程序,允许用户使用 Wifi Hotspot 创建自己的网络,然后客户端可以连接到它并共享数据。我已经在 android 中成功创建了 Wifi 热点,但我无法为此目的进行配置。有没有办法通过编码在android上配置Wifi热点??

【问题讨论】:

  • ,...查看我的回复。

标签: android wifi android-wifi tethering


【解决方案1】:

这个答案可能已经过时了!

if(wifiManager.isWifiEnabled())
{
    wifiManager.setWifiEnabled(false); 
}

WifiConfiguration netConfig = new WifiConfiguration();

netConfig.SSID = "MyAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

try{
    Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
    boolean apstatus=(Boolean) setWifiApMethod.invoke(wifiManager, netConfig,true);

    Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled"); 
    while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){};
    Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState"); 
    int apstate=(Integer)getWifiApStateMethod.invoke(wifiManager);
    Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
    netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);
    Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");

} catch (Exception e) {
    Log.e(this.getClass().toString(), "", e);
}

【讨论】:

  • 此 Wifi 热点是否有 setWifiApConfiguration,以便我可以使用代码连接到自定义 wifi 接入点。
  • 不适用于 Android 6 和 5.1 以上版本。我添加了 ACTION_MANAGE_WRITE_SETTINGS 权限,但仍然出现Exception in softap start java.lang.IllegalStateException: command '2707 softap set wlan0 .... 错误
  • @AkshayTaru 毫不奇怪!当时我回答的最新版本是 android 5.0 ,希望你能更新答案。
【解决方案2】:

这是一个可能的解决方案:

private WifiManager.LocalOnlyHotspotReservation mReservation;

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

wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            mReservation = reservation;
        }

        @Override
        public void onStopped() {
            super.onStopped();
        }

        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
        }
    }, new Handler());

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
猜你喜欢
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 2011-03-02
  • 1970-01-01
相关资源
最近更新 更多