简体   繁体   English

在oreo中无线热点创建失败

[英]Wifi Hotspot creation fails in oreo

I'm working on a simple system app in Oreo AOSP to turn ON wifi Hotspot with predefined SSID and preshared key. 我正在使用Oreo AOSP中的一个简单的系统应用来打开带有预定义SSID和预共享密钥的wifi热点。

As my APP is built as a system app so i don't need reflection. 由于我的APP是作为系统应用程序构建的,所以我不需要反思。

MainActivity.java MainActivity.java

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                boolean result = false;
                WifiManager mwifiManager;
                mwifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

                try {
                        Method method = mwifiManager.getClass().getMethod("getWifiApConfiguration");
                        WifiConfiguration netconfig = (WifiConfiguration) method.invoke(mwifiManager);
                        netconfig.SSID = "DummyApp";
                        netconfig.preSharedKey = "1234567890";
                        netconfig.allowedKeyManagement.set(4);
                        mwifiManager.setWifiEnabled(false);
                        method = mwifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                        result = (boolean) method.invoke(mwifiManager, netconfig, true);
                        if (!result) {
                                Toast.makeText(this, "Hotspot creation failed", Toast.LENGTH_SHORT).show();
                        } else {
                                Toast.makeText(this, "Wifi Enabled", Toast.LENGTH_SHORT).show();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                finish();
        }

        @Override
        protected void onResume() {
                super.onResume();
        }
}

AndroidManifest.xml AndroidManifest.xml中

    android:protectionLevel="signature|privileged"
    android:sharedUserId="android.uid.system">
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

    <!-- for wifi  -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.TETHER_PRIVILEGED" />

Wifi Should turn ON but getting following result: Wifi应该打开但获得以下结果:

Toast Message: Hotspot creation failed Toast消息:热点创建失败

Logcat: WifiManager: PACKAGE_NAME attempted call to setWifiApEnabled: enabled = true Logcat:WifiManager:PACKAGE_NAME尝试调用setWifiApEnabled:enabled = true

update1: How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo) update1: 如何在Android 8.0(Oreo)中以编程方式打开/关闭wifi热点

I have tried above changes. 我已尝试过上述变化。 It will turn on Hotspot locally but no custom SSID and password. 它将在本地打开Hotspot,但没有自定义SSID和密码。

update2: After getting input from @Mr.AF. update2:从@ Mr.AF获得输入后。

Method setConfigMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                                                                                 ......code snip.........

netconfig.allowedKeyManagement.set(4);
Method Method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);

HOTSPOT Creation FAiled HOTSPOT创作失败

To turn ON Portable HotSpot in Android Nougat and below following code works. 要在Android Nougat及以下代码中启用Portable HotSpot,请执行以下代码。

   Method method = mwifiManager.getClass().getMethod("getWifiApConfiguration");
   WifiConfiguration netconfig = (WifiConfiguration) method.invoke(mwifiManager);
   method = mwifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
   result = (boolean) method.invoke(mwifiManager, netconfig, true);

Above API is deprecated in Oreo. 以上API在Oreo中已弃用。

Following is a @system hidden API in oreo, If the android application is built as a system app then only below API code can be accessed. 以下是oreo中的@system隐藏API,如果android应用程序是作为系统应用程序构建的,则只能访问API代码以下。 In my case, I can use the below API. 就我而言,我可以使用以下API。

   ConnectivityManager oncm = (ConnectivityManager)ontext.getSystemService(Context.CONNECTIVITY_SERVICE);

   oncm.startTethering(ConnectivityManager.TETHERING_WIFI, true, new ConnectivityManager.OnStartTetheringCallback() {

   @Override
   public void onTetheringStarted() {
          super.onTetheringStarted();
          Log.i(TAG, "Hotspot is successfully opened");
   }

   @Override
   public void onTetheringFailed() {
          super.onTetheringFailed();
          Log.e(TAG, "Hotspot failed to open");
   }
 });

You don't need to use reflection for versions>=Oreo. 您不需要对版本> = Oreo使用反射。 After android's public exposed API startLocalOnlyHotspot . 在android的公开暴露API startLocalOnlyHotspot之后 I've explained this answer in detail on this question on Stackoveflow. 我已经在这个详细解释了这个答案的问题上Stackoveflow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM