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.


Never see anything like it before

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

Picena Informatica

Active Member
Licensed User
Longtime User
Try to rem the line with toast. Use only log.
 

barx

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

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
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…