2017-02-15 60 views
1

我一直在尝试使用附近的API来检测我的Android应用中的信标。遵循了https://developers.google.com/nearby/messages/android/get-startedhttps://developers.google.com/nearby/messages/android/get-beacon-messages的说明附近的消息Api成功订阅,但onFound从未要求注册Google Beacon

与GoogleAPI客户端的连接是通过点击应用程序上的按钮(用于显式意图)来启动的,并且在onConnected回调中调用了对Nearby Messages的订阅。

//Inside onCreate 
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) {     
      startNearby(); 
     } 
    }); 


private void startNearby() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Nearby.MESSAGES_API) 
      .addConnectionCallbacks(this) 
      .enableAutoManage(this, this) 
      .build(); 
} 
@Override 
public void onConnected(Bundle bundle) { 
    if(mGoogleApiClient.isConnected()){ 
     System.out.println("Google_Api_Client: It was connected on (onConnected) function, working as it should."); 
    } 
    else{ 
     System.out.println("Google_Api_Client: It was NOT connected on (onConnected) function, It is definetly bugged."); 
    } 
    subscribe(); 
} 
private void subscribe() { 
    mMessageListener = new MessageListener() { 
     @Override 
     public void onFound(Message message) { 
      String messageAsString = new String(message.getContent()); 
      System.out.println("On Found: "+messageAsString); 
      adapter.add("FOUND: "+messageAsString); 
     } 

     @Override 
     public void onLost(Message message) { 
      String messageAsString = new String(message.getContent()); 
      System.out.println("On Lost: "+messageAsString); 
      adapter.add("LOST: "+messageAsString); 
     } 
    }; 
    SubscribeOptions options = new SubscribeOptions.Builder() 
      .setStrategy(Strategy.BLE_ONLY) 
      .build(); 
    Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, options).setResultCallback(new ResultCallback<Status>() { 
     @Override 
     public void onResult(@NonNull Status status) { 
      if (status.isSuccess()) { 
      //  Log.i(TAG, "Subscribed successfully."); 
       System.out.println("DONE. Subscribed successfully." +status); 
       adapter.add("Subscribing done. "+status); 
      } else { 
       System.out.println("ERROR. Could not subscribe, status = " + status); 
     //  logAndShowSnackbar("Could not subscribe, status = " + status); 
     //  mSubscribeSwitch.setChecked(false); 
      } 
     } 
    }); 
} 

在Nearby.messages.subscribe通话ResultCallback,我得到成功响应状态对象 -

I/System.out: Google_Api_Client: It was connected on (onConnected) function, working as it should. 
I/System.out: DONE. Subscribed successfully.Status{statusCode=SUCCESS, resolution=null} 

对于信标,我使用在Android Play商店灯塔模拟器应用模拟Eddystone UID信标。我使用Beacon Tools应用程序注册了两个信标,并可以在Google Beacon仪表板上看到活动的信标。

问题是onFound/onLost回调从未在我的应用程序中调用过。请注意,信标可通过信标模拟器应用程序(在Android上)以及Google Beacon工具应用程序(在注册信标附近部分下)检测到。

对于我在这里可能做错的事情,我真的很感激任何帮助。谢谢!!

回答

0

试试这个。

btnScan = (Button)findViewById(R.id.btnScan); 

btnScan.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v){ 
     startNearby(); 
    } 
}); 

mMessageListener = new MessageListener() { 
    @Override 
    public void onFound(Message message) { 
     String messageAsString = new String(message.getContent()); 
     Log.d(null, "Found message: " + messageAsString); 
     Toast.makeText(getApplication(), "Found message: " + messageAsString, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onLost(Message message){ 
     String messageAsString = new String(message.getContent()); 
     Log.d(null, "Lost sight of message: " + messageAsString); 
     Toast.makeText(getApplication(), "Lost sight of message: " + messageAsString, Toast.LENGTH_LONG).show(); 
    } 
}; 

void startNearby() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Nearby.MESSAGES_API) 
      .addConnectionCallbacks(this) 
      .enableAutoManage(this, this) 
      .build(); 
} 

private void subscribe() { 
    Log.i("", "Subscribing..."); 
    Toast.makeText(getApplication(), "Subscribing...", Toast.LENGTH_LONG).show(); 

    try { 
     SubscribeOptions options = new SubscribeOptions.Builder() 
       .setStrategy(Strategy.BLE_ONLY) 
       .build(); 
     // Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, options); 
     Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, options);/* */ 
    } 
    catch(Exception ex) { 
     Toast.makeText(getApplication(), ex.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    subscribe(); 
}