0

I am trying to use the nearby sample and follow this link: https://developers.google.com/nearby/messages/android/pub-sub

and i want to subscribe and publish a small text. so i added:

private Message mActiveMessage;

private void publish(String message) {
    Log.i(TAG, "Publishing message: " + message);
    mActiveMessage = new Message(message.getBytes());
    Nearby.Messages.publish(mGoogleApiClient, mActiveMessage);
}

and on a button click:

btnPublish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            publish("Hello World");
        }
    });

and try to receive it on other phone onFound:

public void onFound(final Message message) {
            // Called when a new message is found.
            mNearbyDevicesArrayAdapter.add(
                    DeviceMessage.fromNearbyMessage(message).getMessageBody());

            String messageAsString = new String(message.getContent());
            Log.d(TAG, "Found message: " + messageAsString);
            Toast.makeText(getBaseContext(), messageAsString, Toast.LENGTH_LONG).show();

but i get error:

    E/AndroidRuntime: FATAL EXCEPTION: main 
gms.nearby.messages.samples.nearbydevices, PID: 733
com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: 
Expected BEGIN_OBJECT but was STRING at line 1
       

i did something wrong? because i don't think i need to go and parser it like suggested here: GSON: Expected BEGIN_OBJECT but was STRING

another question about Nearby: it's possible to use only the Ultrasonic to public text?

thank you for your time.

Community
  • 1
  • 1
Yanay Hollander
  • 327
  • 1
  • 5
  • 19

1 Answers1

4

Remove this line:

mNearbyDevicesArrayAdapter.add(
        DeviceMessage.fromNearbyMessage(message).getMessageBody());

The DeviceMessage.fromNearbyMessage(message) call assumes the message bytes are JSON and is trying to parse it, but the message you published is not JSON.

Joe Farfel
  • 136
  • 2