简体   繁体   English

在Android Oreo 8.x中更改WiFi热点的SSID和密码

[英]Change WiFi hotspot's SSID and password in Android Oreo 8.x

In my Android application I'm using the following code snippet: 在我的Android应用程序中,我使用以下代码片段:

@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot(){
    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback(){

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            Log.d(TAG, "Wifi Hotspot is on now");
        }

        @Override
        public void onStopped() {
            super.onStopped();
            Log.d(TAG, "onStopped: ");
        }

        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
            Log.d(TAG, "onFailed: ");
        }
    },new Handler());
}

This piece of code creates a hotspot named something like "AndroidShare_1234". 这段代码创建了一个名为“AndroidShare_1234”的热点。 For a project of mine I need to be able to set a password and SSID to this hotspot, however I can't find a way to do this. 对于我的项目,我需要能够为此热点设置密码和SSID,但是我找不到这样做的方法。 I would like to create a hotspot with an SSID like MyHotspot and a custom password. 我想创建一个像MyHotspot这样的SSID和自定义密码的热点。

Note that the setWifiApEnabled is not supported anymore in Android O, this is how it's done in older versions of Android. 请注意,Android O中不再支持setWifiApEnabled ,这是在旧版Android中完成的。 However, I still need to programmatically make a wifi hotspot with a SSID and a password. 但是,我仍然需要以编程方式使用SSID和密码创建一个wifi热点。 I can't figure out how to do this. 我无法弄清楚如何做到这一点。 Thanks in advance! 提前致谢!

For who cares...: 谁关心...:

For a school project I made a locker that unlocks whenever it can connect to a wireless network with certain cridentials, hence the need of setting a hotspot programmatically. 对于一个学校项目,我制作了一个储物柜,只要它可以连接到具有某些警告的无线网络,就可以解锁,因此需要以编程方式设置热点。

I have only a partial solution to the problem. 我只能部分解决这个问题。 Hopefully, it will be sufficient for the application you are designing. 希望它对您正在设计的应用程序足够了。

The SSID and the password are hard-coded by the android system when you start the Hotspot . 启动Hotspot时,android系统会对SSID和密码进行硬编码 By looking over at the AOSP code, I see that the same hotspot can be shared by multiple applications. 通过查看AOSP代码,我发现多个应用程序可以共享相同的热点。 The configuration for this hotspot(class name is WifiConfiguration ) is also shared with all the requesting applications. 此热点的配置(类名称为WifiConfiguration )也与所有请求的应用程序共享。 This configuration is passed back to the application in the callback onStarted(LocalOnlyHotspotReservation reservation) . 此配置将在回调onStarted(LocalOnlyHotspotReservation reservation)传递回应用程序。 You can get the WifiConfiguration by calling reservation.getWifiConfiguration() . 您可以通过调用reservation.getWifiConfiguration()来获取WifiConfiguration You will get all the information you need from the WifiConfiguration object. 您将从WifiConfiguration对象获得所需的所有信息。 So you can read the Pre-Shared Key and the access point name. 因此,您可以阅读预共享密钥和访问点名称。 But I don't think you can change them 但我认为你不能改变它们


FYI, The relevant code that sets up the wifi configuration (including the hard-coded SSID and WPA2-PSK key) is done by the following piece of code 仅供参考,设置wifi配置的相关代码(包括硬编码的SSID和WPA2-PSK密钥)由以下代码完成

  /**
   * Generate a temporary WPA2 based configuration for use by the local only hotspot.
   * This config is not persisted and will not be stored by the WifiApConfigStore.
   */
   public static WifiConfiguration generateLocalOnlyHotspotConfig(Context context) {
       WifiConfiguration config = new WifiConfiguration();
       config.SSID = context.getResources().getString(
              R.string.wifi_localhotspot_configure_ssid_default) + "_"
                      + getRandomIntForDefaultSsid();
       config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
       config.networkId = WifiConfiguration.LOCAL_ONLY_NETWORK_ID;
       String randomUUID = UUID.randomUUID().toString();
       // first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
       config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
       return config;
   }

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

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