このエラーが発生するのは、Nearby API を使用するために必要なアクセス許可をユーザーに明示的に要求していないためです。これを行う 1 つの方法を次に示します。
// 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);
}
}
}
}
ドキュメントで完全な例を見つけることができます。
また、ガイドラインについて覚えておいてください。
ユーザーを驚かせないでください。Nearby を有効にするには、明示的なアクション (ボタンのタップ、アプリ内のセクションへの移動、特別なスイッチなど) を実行することをユーザーに要求します。
iOS と Android の両方で、Nearby を初めて呼び出すと、許可ダイアログがトリガーされます。Nearby を呼び出す前に明示的なユーザー アクションを待つことで、ユーザーはダイアログを文脈化し、それをアプリの近接ベースの機能に関連付けることができます。