【问题标题】:Nearby message api error - Nearby.Messages is not enabled for this app: packageName附近消息 api 错误 - 此应用未启用 Nearby.Messages:packageName
【发布时间】:2016-11-03 22:17:17
【问题描述】:

我正在尝试使用附近的消息 api 从 eddystone 信标获取消息。我已遵循以下文档:

[https://developers.google.com/nearby/messages/overview?hl=en][1]

我正在使用默认调试密钥库中的 SHA1。但我不断收到以下错误消息

 Nearby.Messages is not enabled for this app: packageName

设备:Nexus 6(Android 版本 5.1.1)

播放服务版本:8.1.15

【问题讨论】:

  • 你能添加一些关于你的设备的信息吗?您使用的是哪个版本的 Google Play 服务?
  • @ShailenTuli 我已经用设备和播放服务版本更新了帖子
  • 如果没有更多上下文,很难知道您收到此消息的原因。您可以从github.com/googlesamples/android-nearby 构建和运行 NearbyDevices 示例吗?按照 README 中的说明在清单中设置密钥等。将订阅方法 Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, PUB_SUB_STRATEGY) 修改为 Nearby.Messages.subscribe(mGoogleApiClient, messageListener, Strategy.BLE_ONLY)。让我知道这是否有效。
  • 我之前已经尝试过该示例并尝试对其进行调试,但我得到了同样的错误。即使我更换了 api 密钥。是否与密钥库有关。我使用android studio的默认调试密钥库。
  • 找到生成的apk,使用keytool -list -printcert -jarfile {something}.apk | grep SHA1获取SHA。然后按照自述文件中的说明进行操作。

标签: android eddystone google-nearby


【解决方案1】:

您收到此错误是因为您没有明确要求用户授予权限,而您必须这样做才能使用 Nearby API。这是执行此操作的一种方法:

// GoogleApiClient connection callback. Initiate permission check here.
@Override
public void onConnected(Bundle connectionHint) {
    Nearby.Messages.getPermissionStatus(mGoogleApiClient).setResultCallback(
            new ErrorCheckingCallback("getPermissionStatus", new Runnable() {
                @Override
                public void run() {
                    publishAndSubscribe();
                }
            })
    );
}


// This is called in response to a button tap in the Nearby permission dialog.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_RESOLVE_ERROR) {
        mResolvingError = false;
        if (resultCode == RESULT_OK) {
            // Permission granted or error resolved successfully then we proceed
            // with publish or subscribe..
            publishAndSubscribe();
        } else {
            // This may mean that user had rejected to grant nearby permission.
            showToast("Failed to resolve error with code " + resultCode);
        }
    }
}


/**
* A simple ResultCallback that displays a toast when errors occur.
* It also displays the Nearby opt-in dialog when necessary.
*/
private class ErrorCheckingCallback implements ResultCallback<Status> {
   private final String method;
   private final Runnable runOnSuccess;

   private ErrorCheckingCallback(String method) {
       this(method, null);
   }

   private ErrorCheckingCallback(String method, @Nullable Runnable runOnSuccess) {
       this.method = method;
       this.runOnSuccess = runOnSuccess;
   }

   @Override
   public void onResult(@NonNull Status status) {
       if (status.isSuccess()) {
           Log.i(TAG, method + " succeeded.");
           if (runOnSuccess != null) {
               runOnSuccess.run();
           }
       } else {
           // Currently, the only resolvable error is that the device is not opted
           // in to Nearby. Starting the resolution displays an opt-in dialog.
           if (status.hasResolution()) {
               if (!mResolvingError) {
                   try {
                       status.startResolutionForResult(MainActivity.this,
                               REQUEST_RESOLVE_ERROR);
                       mResolvingError = true;
                   } catch (IntentSender.SendIntentException e) {
                       showToastAndLog(Log.ERROR, method + " failed with exception: " + e);
                   }
               } else {
                   // This will be encountered on initial startup because we do
                   // both publish and subscribe together.  So having a toast while
                   // resolving dialog is in progress is confusing, so just log it.
                   Log.i(TAG, method + " failed with status: " + status
                           + " while resolving error.");
               }
           } else {
               showToastAndLog(Log.ERROR, method + " failed with : " + status
                       + " resolving error: " + mResolvingError);
           }
       }
   }
}

你可以找到一个完整的例子in the documentation

另外,请记住guidelines

  • 不要让用户感到惊讶。要求用户执行显式 操作(点击按钮,转到应用程序中的某个部分,一个特殊的 开关等)以激活附近。

  • 在 iOS 和 Android 上,调用 附近第一次会触发权限对话框。等待 在调用 Nearby 之前执行明确的用户操作将帮助用户 上下文化对话框并将其与您的应用程序相关联 基于邻近的功能。

【讨论】:

  • 小改动,我觉得应该是“status.startResolutionForResult(MainActivity.this,ConnectionResult.RESOLUTION_REQUIRED);”我找不到“REQUEST_RESOLVE_ERROR”
  • @tallen 就是所谓的“请求码”,一个你自己定义的随机常数。它可以是任何整数。请参阅developer.android.com/training/basics/intents/result.html 了解更多信息。
  • 这或多或少是正确的。我发现他们执行回调类(带有可运行对象)的方式不必要地令人费解,或者我可能只是没有那么聪明,我还没有决定。在运行时,您最终会被回调两次,因为您隐含地要求两个权限(这是错误的词,选择加入可能会更好)。对 startResolutionForResult 的调用会弹出一个请求权限的活动。不要将这些权限(选择加入)与您在清单中所做的事情混淆,这是 GooglePlayServicesAPI 的事情。
【解决方案2】:

您必须明确要求用户获得使用 Nearby 的权限。 Nearby API 现在为EyesClear 提供的代码提供快捷方式。您只需要在 API 客户端生成器上调用 enableAutoManage 方法即可。

详情请访问:https://developers.google.com/nearby/messages/android/user-consent

不幸的是,这仅适用于前台订阅。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2019-05-07
    • 1970-01-01
    • 2011-07-30
    相关资源
    最近更新 更多