【问题标题】:Nearby Connections api not working on Android Pie附近的连接 api 不适用于 Android Pie
【发布时间】:2021-01-12 19:03:46
【问题描述】:

我已经为 Google 的 Nearby Connections 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: Nearby.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());
                            }
                        });
    }

【问题讨论】:

    标签: android google-nearby


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 2020-10-17
      • 2018-09-22
      相关资源
      最近更新 更多