Android-Wifi Direct/P2p无法在Android 8上找到对等方 [英] Android - Wifi Direct / p2p not able to find peers on Android 8

查看:210
本文介绍了Android-Wifi Direct/P2p无法在Android 8上找到对等方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始创建Android应用程序wifi Wifi直接功能. 我做了一个同伴发现,并且我喜欢在列表视图中看到同伴.

I started creating an Android app wifi Wifi direct functionality. I do a peer discover and I like to see the peers in a listview.

我现在有一个应用程序,该应用程序尝试发现对等体并在列表中显示对等体.

I now have a app which tries to discover peers and shows the peers in a list.

但是我遇到了这样的问题,它可以在我的Android 5设备上正常工作.在这里,我可以看到我的Android 8设备,但反过来却看不到.我无法在Android 8设备上看到我的Android 5设备.

But I'm facing the problem that this works fine on my Android 5 device. Here I can see my Android 8 device, but not the other way around. I cannot see my Android 5 device on my Android 8 device.

我从这里开始执行以下步骤: https://developer.android.com/training/connect-devices-wirelessly/nsd

I followed steps from here: https://developer.android.com/training/connect-devices-wirelessly/nsd

并且还发现了这篇文章: WiFi Peer Discovery中的Android O问题表示我需要先获得许可才能继续.

And also discovered this post: Android O issues with WiFi Peer Discovery which says that I need to ask for permissions before continuing.

我的清单:

<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"/>

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

What could be the problem? Maybe I can provide more information if needed to solve the problem?

推荐答案

要解决Android 8(Oreo)的上述问题,需要执行两个步骤:
1)授予位置权限.

To solve above problem with Android 8(Oreo), There are two steps to do:
1) Grant location permission.

    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.
下面的代码打开启用位置对话框.


2) Enable Location service/ Turn on GPS of device.
Below code open dialog for enable location.

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;
                }
            }

        }
    });

这篇关于Android-Wifi Direct/P2p无法在Android 8上找到对等方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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