- 我
Google Messages Nearby Api
用来在两个 android 设备之间简单地交换小消息。 - 我想
republish
在单击按钮时收到更新的消息,但通常会失败。我所做的是首先取消发布消息对象,然后再次使用新数据发布它,如下所示,但它经常失败。
发送消息的设备 1 代码:
class Testing extends Activity{
MessageListener mMessageListener;
Message mMessage;
MessagesClient messagesClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
mMessage = new Message('data in bytes');
messagesClient = Nearby.getMessagesClient(this);
mMessageListener = new MessageListener() {
@Override
public void onFound(Message message) {
Log.d(TAG, "onFound :--- " + message);
}
@Override
public void onLost(Message message) {
Log.d(TAG, "onLost :--- " + message);
}
}
startNearbyMessage();
}
private void startNearbyMessage() {
if (messagesClient != null && mMessageListener != null) {
messagesClient.publish(mMessage);
SubscribeOptions options = new SubscribeOptions.Builder()
.setStrategy(Strategy.BLE_ONLY)
.build();
messagesClient.subscribe(mMessageListener, options);
}
}
@Override
protected void onDestroy() {
try {
AppLog.e(TAG, "Stop NearBy Message : ");
isNetworkingAlreadyStarted = false;
if (messagesClient != null) {
if (mMessage != null)
messagesClient.unpublish(mMessage);
if (mMessageListener != null)
messagesClient.unsubscribe(mMessageListener);
}
} catch (Exception ex) {
AppLog.e(TAG, "stopNearByMessgae Ex : " + ex.toString());
}
}
}
我的问题是我想在单击按钮时重新发布一条新消息,所以我将此代码放在单击侦听器上:
//updated mMessage object with new data:
messagesClient.unpublish(mMessage)
messagesClient.publish(mMessage)
但通常消息不会发布,并且 onFound 仍然保留旧数据。