如何在Oreo中以编程方式创建wifihotspot? [英] How to create wifihotspot in Oreo programmatically?

查看:141
本文介绍了如何在Oreo中以编程方式创建wifihotspot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好给出

Hello Given link question is just showing how to turn on/off wifi hotspot but i want to add create wifi hotspot with SSID and password. I written code for creating wifihotspot(in both NONE and WPA2 PSK) in android and its working fine upto android 7 but in oreo it returning me false value.The summary of my code is-

private WifiManager wifiManager;
private Method method;
private WifiConfiguration config;
config.SSID = ssid;
config.status = WifiConfiguration.Status.ENABLED;
method = wifiManager.getClass().getMethod("setWifiApEnabled",                                           
WifiConfiguration.class, Boolean.TYPE);
boolean status = (Boolean)  method.invoke(wifiManager, config, true);

所以我的问题是如何为Android Oreo创建NONE和WPA2 PSK格式的wifihotspot?有可能吗?

So my question is how to create wifihotspot in both NONE and WPA2 PSK format for android oreo? Is it possible?

推荐答案

Oreo不支持以编程方式创建无密码的热点.它始终使用随机生成的唯一ssid和密钥创建热点.

Oreo doesnot support to create hotspot programmatically with no password. It always creates hotspot with unique ssid and key generated randomly.

 WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
 WifiManager.LocalOnlyHotspotReservation mReservation;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        assert manager != null;
        manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

            @SuppressLint("SetTextI18n")
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);
                Timber.d("Wifi Hotspot is on now , reservation is : %s", reservation.toString());
                mReservation = reservation;
                 key = mReservation.getWifiConfiguration().preSharedKey;
                 ussid = mReservation.getWifiConfiguration().SSID;


            }

            @Override
            public void onStopped() {
                super.onStopped();
                Timber.d("onStopped: ");
            }

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

这篇关于如何在Oreo中以编程方式创建wifihotspot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆