2015-12-09 19 views
7

Tôi đã cố gắng tạo điểm phát sóng chia sẻ kết nối wifi trong Android Marshmallow bằng cách sử dụng mã sau.Làm thế nào để tạo điểm truy cập Internet tethering trong Android Marshmallow?

public class WifiAccessManager { 

private static final String SSID = "1234567890abcdef"; 
public static boolean setWifiApState(Context context, boolean enabled) { 
    //config = Preconditions.checkNotNull(config); 
    try { 
     WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
     if (enabled) { 
      mWifiManager.setWifiEnabled(false); 
     } 
     WifiConfiguration conf = getWifiApConfiguration(); 
     mWifiManager.addNetwork(conf); 

     return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

public static WifiConfiguration getWifiApConfiguration() { 
    WifiConfiguration conf = new WifiConfiguration(); 
    conf.SSID = SSID; 
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
    return conf; 
} 

}

Nhưng nó được thể hiện vấn đề cho phép sau. .

java.lang.SecurityException: googleplus.tarun.info.hotspotcreation was not granted either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS. 

Nhưng tôi đã thêm chúng vào tệp kê khai. .

Làm cách nào để giải quyết sự cố?

Trả lời

-1

Tôi nghĩ Android M không hỗ trợ tạo điểm phát sóng theo lập trình. Bạn có thể đưa người dùng Marshmallow của bạn đến trang cài đặt để tạo điểm phát sóng của chính mình. bên dưới mã sẽ giúp yo để đi thiết lập trang.

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); 
1

Tôi đã làm việc trong Android Marshmallow và đã tìm ra cách để tạo chia sẻ kết nối WiFi như mô tả bên dưới. Lưu ý rằng theo Android 6.0 Changes Các ứng dụng của bạn hiện có thể thay đổi trạng thái của các đối tượng WifiConfiguration chỉ khi bạn đã tạo các đối tượng này. Và bắt đầu từ Android 6.0 (API cấp 23), người dùng cấp quyền cho ứng dụng trong khi ứng dụng đang chạy, không phải khi họ cài đặt ứng dụng. Read this article to know more about this. Tôi có thể thấy bạn đang tạo điểm phát sóng của riêng bạn. Vì vậy, không có vấn đề. Việc cho phép trong Manifest được đưa ra dưới đây:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 

Tôi đang sử dụng các chức năng sau đây để tạo WiFi tethering Hotspot trong android marshmallow:

public void setWifiTetheringEnabled(boolean enable) { 
    //Log.d(TAG,"setWifiTetheringEnabled: "+enable); 
    String SSID=getHotspotName(); // my function is to get a predefined SSID 
    String PASS=getHotspotPassword(); // my function is to get a predefined a Password 

    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 

    if(enable){ 
     wifiManager.setWifiEnabled(!enable); // Disable all existing WiFi Network 
    }else { 
     if(!wifiManager.isWifiEnabled()) 
      wifiManager.setWifiEnabled(!enable); 
    } 
    Method[] methods = wifiManager.getClass().getDeclaredMethods(); 
    for (Method method : methods) { 
     if (method.getName().equals("setWifiApEnabled")) { 
      WifiConfiguration netConfig = new WifiConfiguration(); 
      if(!SSID.isEmpty() || !PASS.isEmpty()){ 
       netConfig.SSID=SSID; 
       netConfig.preSharedKey = PASS; 
       netConfig.hiddenSSID = false; 
       netConfig.status = WifiConfiguration.Status.ENABLED; 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
       netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
      } 
      try { 
       method.invoke(wifiManager, netConfig, enable); 
       Log.e(TAG,"set hotspot enable method"); 
      } catch (Exception ex) { 
      } 
      break; 
     } 
    } 
} 

Bật Hotspot cuộc gọi chức năng là: setWifiTetheringEnabled(true) và vô hiệu hóa setWifiTetheringEnabled(false) .

Vậy đó.

N.B. Lưu ý rằng các thiết bị nhỏ hơn của SIM không được hỗ trợ để sử dụng Hotspot. Bạn sẽ không thể tạo Điểm phát sóng trên các thiết bị đó mà không có gốc.

Hy vọng điều này sẽ hữu ích cho khách truy cập sắp tới.

Các vấn đề liên quan