android-附近的API没有收到任何消息

android-附近的API没有收到任何消息,第1张

概述我尝试连接到附近的消息API,并成功进行了订阅.现在,由于某种原因,我的mMessageListener字段从不获取回调.我已经使用Android信标服务演示应用程序使用邻近信标api配置了我的信标.publicclassMainActivityextendsAppCompatActivityimplementsGoogleApiClient.Connecti

我尝试连接到附近的消息API,并成功进行了订阅.

现在,由于某种原因,我的mMessageListener字段从不获取回调.
我已经使用Android信标服务演示应用程序使用邻近信标API配置了我的信标.

public class MainActivity extends AppCompatActivity implements    Googleapiclient.ConnectionCallbacks, Googleapiclient.OnConnectionFailedListener {// Declaration of member variablesprivate Googleapiclient mGoogleapiclient;private final String TAG = "BrIDge.MainActivity";private boolean mResolvingError = false;private static final int REQUEST_RESolVE_ERROR = 100;private static final int REQUEST_PERMISSION = 42;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    // Initializing the Google Api clIEnt}private MessageListener mMessageListener = new MessageListener() {    @OverrIDe    public voID onFound(Message message) {        // Do something with the message        Log.i(TAG, " Found Message : " + message.toString());    }    @OverrIDe    public voID onLost(Message message) {        super.onLost(message);        Log.i(TAG, " Found Message : " + message.toString());    }};@OverrIDepublic voID onConnected(@Nullable Bundle bundle) {    Log.d(TAG, "GoogleAPI ClIEnt Connected");    foregorundSubscribeBeacons();}@OverrIDepublic voID onConnectionSuspended(int i) {    Log.d(TAG, "Google Api Connection Suspended : " + i);}@OverrIDepublic voID onConnectionFailed(@NonNull ConnectionResult connectionResult) {    Log.d(TAG, "GoogleAPI Connection Failed : " + connectionResult.getErrorMessage());}public voID foregorundSubscribeBeacons() {    // Subscribe to receive messages    Log.i(TAG, "Trying to subscribe");    if (!mGoogleapiclient.isConnected()) {        if (!mGoogleapiclient.isConnecting()) {            mGoogleapiclient.connect();        }    } else {        SubscribeOptions options = new SubscribeOptions.Builder()                .setStrategy(Strategy.BLE_ONLY)                .setCallback(new SubscribeCallback() {                    @OverrIDe                    public voID onExpired() {                        Log.i(TAG, "No longer subscribing.");                    }                }).build();        Nearby.Messages.subscribe(mGoogleapiclient, mMessageListener, options)                .setResultCallback(new ResultCallback<Status>() {                    @OverrIDe                    public voID onResult(Status status) {                        if (status.isSuccess()) {                            Log.i(TAG, "Subscribed successfully.");                        } else {                            Log.i(TAG, "Could not subscribe.");                            // Check whether consent was given;                            // if not, prompt the user for consent.                            handleUnsuccessfulNearbyResult(status);                        }                    }                });    }}private voID handleUnsuccessfulNearbyResult(Status status) {    Log.i(TAG, "Processing error, status = " + status);    if (mResolvingError) {        // Already attempting to resolve an error.        return;    } else if (status.hasResolution()) {        try {            mResolvingError = true;            status.startResolutionForResult(this,                    REQUEST_RESolVE_ERROR);        } catch (IntentSender.SendIntentException e) {            mResolvingError = false;            Log.i(TAG, "Failed to resolve error status.", e);        }    } else {        if (status.getStatusCode() == CommonStatusCodes.NETWORK_ERROR) {            Toast.makeText(this.getApplicationContext(),                    "No connectivity, cannot proceed. Fix in 'Settings' and try again.",                    Toast.LENGTH_LONG).show();        } else {            // To keep things simple, pop a toast for all other error messages.            Toast.makeText(this.getApplicationContext(), "Unsuccessful: " +                    status.getStatusMessage(), Toast.LENGTH_LONG).show();        }    }}@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (requestCode == REQUEST_RESolVE_ERROR) {        // User was presented with the Nearby opt-in dialog and pressed "Allow".        mResolvingError = false;        if (resultCode == RESulT_OK) {            // Execute the pending subscription and publication tasks here.           foregorundSubscribeBeacons();        } else if (resultCode == RESulT_CANCELED) {            // User declined to opt-in. reset application state here.        } else {            Toast.makeText(this, "Failed to resolve error with code " + resultCode,                    Toast.LENGTH_LONG).show();        }    }}@OverrIDeprotected voID onStart() {    super.onStart();    //Initiate connection to Play Services    mGoogleapiclient = new Googleapiclient.Builder(this)            .addAPI(Nearby.MESSAGES_API)            .addConnectionCallbacks(this)            .addOnConnectionFailedListener(this)            .build();    mGoogleapiclient.connect();    //The location permission is required on API 23+ to obtain BLE scan results    int result = ActivityCompat            .checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);    if (result != PackageManager.PERMISSION_GRANTED) {        //Ask for the location permission        ActivityCompat.requestPermissions(this,                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},                REQUEST_PERMISSION);    }}@OverrIDeprotected voID onStop() {    super.onStop();    //Tear down Play Services connection    if (mGoogleapiclient.isConnected()) {        Log.d(TAG, "Un-subscribing…");        mGoogleapiclient.disconnect();    }}

解决方法:

确保Beacon Service Demo应用程序和使用“附近消息”的应用程序都属于同一Google Developers Console项目.您将仅看到您自己的项目附带的消息.

总结

以上是内存溢出为你收集整理的android-附近的API没有收到任何消息全部内容,希望文章能够帮你解决android-附近的API没有收到任何消息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1090326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存