0

アプリはAndroid Jelly Beanでうまく動作しますが、Android Mでは動作しません。時にはInvocationTargetExceptionと表示されます。Wi-Fiホットスポットを作成するアプリでエラーを見つける方法を教えてください。

OnOfHotspot.java

// toggle wifi hotspot on or off 
public static boolean configApState(Context context, boolean apState) { 

    WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
    wifimanager.setWifiEnabled(false); 
    try { 
     if (apState) { 
      WifiConfiguration netConfig = new WifiConfiguration(); 

      netConfig.SSID = hotspotName; 
      netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 

      Method method = WifiManager.class.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
      method.invoke(wifimanager, netConfig, apState); 
      Toast.makeText(context, "WiFi Hotspot \'" + config.SSID + "\' is Created!", Toast.LENGTH_SHORT).show(); 
      return true; 
     } else { 

      Method method = WifiManager.class.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
      method.invoke(wifimanager, config, false); 
      Toast.makeText(context, "WiFi Hotspot \'" + config.SSID + "\' is Disabled!", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

public static WifiConfiguration getApConfiguration(Context context) { 

    WifiConfiguration config = null; 
    WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
    Method[] methods = WifiManager.class.getDeclaredMethods(); 
    for (Method m : methods) { 
     if (m.getName().equals("getWifiApConfiguration")) { 
      try { 
       config = (WifiConfiguration) m.invoke(wifimanager); 
      } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 
       Log.d("Testing", e.getCause().toString()); 
      } 
     } 
    } 
    OnOfHotspot.config = config; 
    return config; 
} 

MainActivity.java

 @Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.enableWifiHotSpotId: 
      try { 
       if (!((editText.getText().toString()).equals(""))) { 
        OnOfHotspot.hotspotName = editText.getText().toString(); 
        OnOfHotspot.getApConfiguration(this); 
        OnOfHotspot.configApState(this, true); 
       }else { 
        Toast.makeText(getApplicationContext(),"Please Specify Hotspot name!",Toast.LENGTH_LONG).show(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      break; 

     case R.id.disableWifiHotspotId: 
      try { 
       OnOfHotspot.getApConfiguration(this); 
       OnOfHotspot.configApState(this, false); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      break; 
     } 
} 

権限

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

Logcat

07-09 14:04:23.869 29724-29724/com.juggernaut.hotspot W/System.err:java.lang.reflect.InvocationTargetException 
07-09 14:04:23.874 432-3097/? D/APM-AudioPolicyManager: startOutput()-- 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at com.juggernaut.hotspot.OnOfHotspot.configApState(OnOfHotspot.java:47) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at com.juggernaut.hotspot.MainActivity.onClick(MainActivity.java:48) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.view.View.performClick(View.java:5233) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.view.View$PerformClick.run(View.java:21209) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.os.Handler.handleCallback(Handler.java:739) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.os.Looper.loop(Looper.java:152) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5497) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-09 14:04:23.879 29724-29724/com.juggernaut.hotspot W/System.err: Caused by: java.lang.SecurityException: com.juggernaut.hotspot was not granted this 
permission: android.permission.WRITE_SETTINGS. 
07-9 14:04:23.883 29724-29724/com.juggernaut.hotspot W/System.err:  at android.os.Parcel.readException(Parcel.java:1620) 
07-09 14:04:23.883 29724-29724/com.juggernaut.hotspot W/System.err:  at android.os.Parcel.readException(Parcel.java:1573) 
07-09 14:04:23.883 29724-29724/com.juggernaut.hotspot W/System.err:  at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1511) 
07-09 14:04:23.883 29724-29724/com.juggernaut.hotspot W/System.err:  at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1590) 
07-09 14:04:23.883 29724-29724/com.juggernaut.hotspot W/System.err:  ... 12 more 

Githubのlink

ソリューション:私はちょうどWRITE_SETTING 許可を取得するため、このメソッドを追加する必要があります。

public void writePermission() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (!Settings.System.canWrite(getApplicationContext())) { 
      Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); 
      startActivityForResult(intent, 200); 

     } 
    } 
} 

そして

アンドロイドバージョン23では、あなたは、実行時に要求権限を持っている上のonClick

+0

他の人の変更を元に戻しないでください。 –

+0

さて、私はしません。おかげで –

答えて

0

内writePermission()を呼び出します。すなわち、マニフェストでパーミッションを宣言したとしても、実行時にバージョン23以降のパーミッションを確認する必要があります。それがアプリがJellybeanで働いていて、Marshmallowでクラッシュした理由です。行番号Caused by: java.lang.SecurityException: com.juggernaut.hotspot was not granted this permission: android.permission.WRITE_SETTINGS.を参照して、許可が与えられていないことを明確に示しています。ホットスポットを作成する前に許可を求めて許可を得てください。実行時にアクセス許可を管理する方法の詳細については、This linkを参照してください。

+0

ありがとう、私のアプリケーションもAndroid Mデバイスで動作します。 –

+0

WPA2-PSKでホットスポットを保護したい。あなたは私を助けてくれますか?私はWPA-PSKとOPENのアプリにセキュリティを追加しました。しかし、私はWPA2-PSKをコードする方法を知らない。助けてください。 –

関連する問題