Java Question Passing a Byte[]

barx

Well-Known Member
Licensed User
Longtime User
Yet another question to help me on my DataLayer library (not doing so well am I, lol).

When sending a message over the DataLayer the sendMessage method looks like this
public abstract PendingResult<MessageApi.SendMessageResult> sendMessage (GoogleApiClient client, String nodeId, String path, byte[] data)

And the onMessageReceived callback passes an object containing the same info.

As you can see the last parameter is of type byte[]. I have tried to implement this as follows.

The sendMessage in lib is simple enough:
B4X:
        public void Send(final String NodeID, final long Timeout, final String Path, final byte[] Data) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    BA.Log("Sending message to " + NodeID);
                    mGoogleApiClient.blockingConnect(Timeout, TimeUnit.MILLISECONDS);
                    Wearable.MessageApi.sendMessage(mGoogleApiClient, NodeID, Path, Data);
                }
            }).start();
        }

The onMessageReceived in lib:
B4X:
        @Hide
        @Override
        public void onMessageReceived(MessageEvent msg) {
            lastMessageReceivedNodeId = msg.getSourceNodeId();
            BA.Log("Dynamic Message Received");
            if (mBA.subExists(mEventname + "_messagereceived")) {
                BA.Log("MessageReceived Sub present");
                mBA.raiseEvent(mGoogleApiClient, mEventname + "_messagereceived", new Object[] {msg.getSourceNodeId(), msg.getPath(), msg.getRequestId(), msg.getData()});
            }
  
        }


and the signature defined in @events
B4X:
MessageReceived(SourceNodeID As String, RequestID As Int, Path As String, Data() As Byte)

lastly the actual even in B4A:

B4X:
Sub DataLayer_MessageReceived(SourceNodeID As String, RequestID As Int, Path As String, Data() As Byte)
    ToastMessageShow(Path, False)
    Log(SourceNodeID)
    Log(Path)

End Sub

When I send the message the phone logs the message has sent
The phone logs that a message has been received but then I get an Error

java.lang.Exception: Sub datalayer_messagereceived signature does not match expected signature.

The only thing I can think it is causing this is the handling of a byte[]. I have tried many tweaks to solve but I'm now stuck.

Any ideas?
 
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
path as string

To be honest I already had it set to String but for some reason when I posted this I thought it was wrong so edited the post. This in it's turn lead me to getting closer to a fix. I had in fact got the RequestId and Path the wrong way round in the RaiseEvent. I have edited the original post to correct it as Now I get another error.

** Activity (main) Pause, UserClosed = true **


Dynamic Message Listener Stopped


** Activity (main) Create, isFirst = false **


Dynamic Message Listener Started


** Activity (main) Resume **
Google Play Services Client Connected


Dynamic Message Received


MessageReceived Sub present


java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:327)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:241)
at anywheresoftware.b4a.keywords.Common.ToastMessageShow(Common.java:389)
at barxdroid.wearable_app.main._datalayer_messagereceived(main.java:332)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at BarxDroid.WearableDataLayer.WearableDataLayer$Message.onMessageReceived(WearableDataLayer.java:314)
at com.google.android.gms.wearable.internal.av.a(Unknown Source)
at com.google.android.gms.wearable.internal.ac$a.onTransact(Unknown Source)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Never see anything like it before :confused:

It looks like it's complaining about the ToastMessageShow(), but I cannot think why?
 

barx

Well-Known Member
Licensed User
Longtime User
See beginning of first post
:confused:
 

barx

Well-Known Member
Licensed User
Longtime User
Try to rem the line with toast. Use only log.
I kinda figured that commenting out the line would stop the error, it was understanding why it is happening I need so I can sort it properly. Thanks
 

barx

Well-Known Member
Licensed User
Longtime User
raiseEventFromThread or something like that.
raise what you? The toast is done in b4a. I thought the idea of callbacks was to raise the event back in the main thread.
 

thedesolatesoul

Expert
Licensed User
Longtime User

barx

Well-Known Member
Licensed User
Longtime User
Oh so because I created a new thread to send the message it is received out of the main thread. Will give it a go tomorrow, thanks. Hown do you know when you are out of the new thread if that makes sense. Cheers mucka
 
Top