【问题标题】:Android - Wifi Direct / p2p not able to find peers on Android 8Android - Wifi Direct / p2p 无法在 Android 8 上找到对等点
【发布时间】:2018-11-01 16:13:20
【问题描述】:

我开始创建 Android 应用 wifi Wifi 直连功能。 我进行了对等发现,我喜欢在列表视图中查看对等点。

我现在有一个应用程序,它尝试发现对等点并在列表中显示对等点。

但我面临的问题是这在我的 Android 5 设备上运行良好。在这里,我可以看到我的 Android 8 设备,但反之则不行。我在我的 Android 8 设备上看不到我的 Android 5 设备。

我按照以下步骤操作: https://developer.android.com/training/connect-devices-wirelessly/nsd

还发现了这个帖子:Android O issues with WiFi Peer Discovery,上面说我需要在继续之前请求权限。

我的清单:

<uses-permission
    android:required="true"
    android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
    android:required="true"
    android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
    android:required="true"
    android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
    android:required="true"
    android:name="android.permission.INTERNET"/>

可能是什么问题?如果需要解决问题,也许我可以提供更多信息?

【问题讨论】:

  • 1.您能否发布获取同行等的实现,因为它会帮助其他人帮助您; 2. 在请求权限时,您是否确认已授予权限?

标签: android wifi-direct android-8.0-oreo nsd


【解决方案1】:

只需打开位置信息,它对我有用

【讨论】:

    【解决方案2】:

    要在 Android 8(Oreo) 上解决上述问题,有两个步骤:
    1) 授予位置权限。

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
           requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                         PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    
        }else{
           mManager.requestPeers(mChannel, mainActivity.peerListListener);
           //do something, permission was previously granted; or legacy device
        }
    


    2) 启用定位服务/打开设备的 GPS。
    下面的代码打开对话框以启用位置。

    public static void displayLocationSettingsRequest(final Context context, final int REQUEST_CHECK_SETTINGS) {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).build();
        googleApiClient.connect();
    
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(10000 / 2);
    
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);
    
        Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(context).checkLocationSettings(builder.build());
        result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
            @Override
            public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
                LocationSettingsResponse response = null;
                try {
                    response = task.getResult(ApiException.class);
                } catch (ApiException exception) {
                    exception.printStackTrace();
    
                    switch (exception.getStatusCode()) {
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the
                            // user a dialog.
                            try {
                                // Cast to a resolvable exception.
                                ResolvableApiException resolvable = (ResolvableApiException) exception;
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                resolvable.startResolutionForResult((MainActivity)context, REQUEST_CHECK_SETTINGS);
                                break;
                            } catch (IntentSender.SendIntentException e) {
                                // Ignore the error.
                            } catch (ClassCastException e) {
                                // Ignore, should be an impossible error.
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            // Location settings are not satisfied. However, we have no way to fix the
                            // settings so we won't show the dialog.
    
                            break;
                    }
                }
    
            }
        });
    

    【讨论】:

    • 在我的诺基亚 6.1 Android 9(API 级别 28)上,必须在设置中启用定位服务,仅检查权限无济于事。在我的华为 P10 Lite Android 7.0 (API Level 24) 上单独检查权限是可以的,不需要在设置中启用定位服务。要么是从 Android 7.0 到 8.0 的变化,要么取决于制造商。注意:在 Android 6.0 中,对位置等危险权限的处理已更改。
    • @priyanka singhal 请完成这两个步骤
    • 抛出错误 方法抛出 'com.google.android.gms.common.api.ResolvableApiException' 异常。
    【解决方案3】:

    我通过添加运行时权限来修复它...

    Manifest.permission.ACCESS_COARSE_LOCATION

    允许,一切正常

    【讨论】:

    • 您在项目的哪个位置添加了这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多