在android中创建Wifi热点配置

时间:2013-06-17 16:56:25

标签: android wifi android-wifi tethering

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

2 个答案:

答案 0 :(得分:16)

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

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

答案 1 :(得分:0)

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

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