在Android版本牛轧糖上以编程方式创建热点

时间:2018-09-25 07:41:13

标签: android android-wifi android-7.1-nougat

我正在开发一个应用程序,用户在该应用程序上单击按钮即可创建热点。我已经测试过我的代码在Android版本below 7.1(Nougat)上可以完美地工作,但是当我在Android版本7.1(Nougat)上测试代码时,它会创建热点,但是由于{{1 }}。 这是我的代码,用于注册广播接收器以配置wifi

preSharedKey

这是配置wifi状态的代码

 WifiMgr.getInstance(ReceiverActivity.this).disableWifi();
    if(ApMgr.isApOn(ReceiverActivity.this)){
        ApMgr.disableAp(ReceiverActivity.this);
    }

    mWifiAPBroadcastReceiver = new WifiAPBroadcastReceiver() {
        @Override
        public void onWifiApEnabled() {
            if(!mIsInitialized){
                mIsInitialized = true;
                tvHotSpotDetails.setText(getResources().getString(R.string.tip_now_init_is_finish));
                tvHotSpotDetails.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tvHotSpotDetails.setText(getResources().getString(R.string.tip_is_waitting_connect));
                    }
                }, 2*1000);
            }
        }
    };
    IntentFilter filter = new IntentFilter(WifiAPBroadcastReceiver.ACTION_WIFI_AP_STATE_CHANGED);
    registerReceiver(mWifiAPBroadcastReceiver, filter);
    ApMgr.isApOn(ReceiverActivity.this); // check Ap state :boolean
    String ssid = TextUtils.isNullOrBlank(Build.MODEL) ? FileUtils.DEFAULT_SSID : android.os.Build.MODEL;
    ApMgr.configApState(ReceiverActivity.this, ssid); 

广播接收器

public static boolean configApState(Context context, String apName) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {
        wificonfiguration = new WifiConfiguration();
        wificonfiguration.SSID = apName;
        // if WiFi is on, turn it off
        if(isApOn(context)) {
            wifimanager.setWifiEnabled(false);
            // if ap is on and then disable ap
            disableAp(context);
        }
        Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method.invoke(wifimanager, wificonfiguration, !isApOn(context));
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

0 个答案:

没有答案