微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Startlocalonlyhotspot 在 Android 11 中不起作用

如何解决Startlocalonlyhotspot 在 Android 11 中不起作用

我附上了我在 Android 11、Samsung galaxy A71 中遇到的本地热点问题。

当我们第一次启动它时,它进入内部,日志显示生成了本地热点以及名称和密码,但实际上并没有启动。在随后的尝试中,它甚至不会进入 onStarted() 函数

任何帮助将不胜感激

@RequiresApi(api = Build.VERSION_CODES.O)
public void createLocalHotspot() {
    WifiManager manager = (WifiManager) mApplicationContext.getSystemService(Context.WIFI_SERVICE);

    /*//If Wifi is ON then switch it OFF
    if (manager.isWifiEnabled()) {
        manager.isWifiEnabled(false);
    }*/

    try {
        Log.d("DynamicRingOfFire",": " + manager);
        //prevIoUsNetConfig = getHotSpotConfig();

        if (ActivityCompat.checkSelfPermission(mApplicationContext,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        &&  ActivityCompat.checkSelfPermission(mApplicationContext,Manifest.permission.CHANGE_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
            // Todo: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions,and then overriding
            //   public void onRequestPermissionsResult(int requestCode,String[] permissions,//                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

            if(manager.isWifiEnabled())
                manager.setWifiEnabled(false);

            manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

                @Override
                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                    super.onStarted(reservation);
                    Log.d("DynamicRingOfFire","Wifi Hotspot is on Now");
                    mReservation = reservation;
                    WifiConfiguration mWifiConfig = mReservation.getWifiConfiguration();

                    Log.d("DynamicRingOfFire","BSSID:" + mWifiConfig.BSSID);
                    Log.d("DynamicRingOfFire","presharedkey:" + mWifiConfig.preSharedKey);
                    Log.d("DynamicRingOfFire","SSID:" + mWifiConfig.SSID);

                    String data = "ConnectWIFI" + Constants.SPLIT_BY_COMMA + mWifiConfig.SSID + Constants.SPLIT_BY_COMMA + mWifiConfig.preSharedKey + Constants.SPLIT_BY_COMMA + "Hotspot";
                    Log.d("DynamicRingOfFire:",data);
                    MessageSender.getInstance().sendCriticalData(data,false,4);
                }

                @Override
                public void onStopped() {
                    super.onStopped();
                    if (mReservation != null) {
                        mReservation.close();
                        mReservation = null;
                    }

                    Log.d("DynamicRingOfFire","onStopped: ");
                }

                @Override
                public void onFailed(int reason) {
                    super.onFailed(reason);
                    if (mReservation != null) {
                        mReservation.close();
                        mReservation = null;
                    }
                    Log.d("DynamicRingOfFire","onFailed: " + reason);
                }
            },new Handler());
            return;
        }
        else
            Log.d("DynamicRingOfFire","required permissions not there");

    } catch (Exception e) {
        Log.d("Exception","" + e);
    }

}

谢谢, WachMe 团队 Picture of the logcat is also enclosed/attached

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。