附近的连接A​​PI在Android Pie上不起作用

时间:2019-07-08 19:42:48

标签: android google-nearby

我已经为Google的“附近的连接” API安装了测试应用。它可以在运行Android 4.4.4的Nexus 7(2012)和运行Android 8的Sony Xperia XZ上完美运行。

但是,当我在运行Android 9的Pixel 3A上运行该应用程序时,尝试在该应用程序中投放广告时会收到以下错误消息:

“ com.google.android.gms.common.api.ApiException:17:API:附近。CONNECTIONS_API在此设备上不可用。”

在应用尝试开始投放广告之前,还会出现此错误消息

“无法连接到套接字'localabstract:com.jameschamberlain.nearbyconnectionstest':连接被拒绝”

任何帮助将不胜感激

public void advertise(View v) {
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                showLocationExplanation(this, LOCATION_PERMISSION_REQUEST_ADVERTISE);
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_ADVERTISE);
            }
        } else {
            // Permission has already been granted
            startAdvertising();
        }
    }


    /**
     * Begin the actual advertising using the Nearby Connections API
     */
    private void startAdvertising() {
        AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder().setStrategy(Strategy.P2P_STAR).build();
        Nearby.getConnectionsClient(getApplicationContext()).startAdvertising(
                "James",
                SERVICE_ID,
                connectionLifecycleCallback,
                advertisingOptions)
                .addOnSuccessListener(
                        new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void unusedResult) {
                                // We're advertising!
                                loggingTextView.append("\nAdvertising");
                            }
                        })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // We were unable to start advertising.
                                loggingTextView.append("\nNot advertising");
                                loggingTextView.append("\n" + e.toString());
                                Log.e("NearbyError", e.toString());
                            }
                        });
    }

1 个答案:

答案 0 :(得分:0)

可能是您的 Pixel 3A 模拟器设备尚未使用 google API 创建。当您在 AVD 中创建新设备时,请尝试使用 google API 创建图像。