I can write to a mailbox on the NXT but cannot (yet) read from it.
the Setup is to use MindDroid from GitHub as a starter.
sendBTCmessage(0, BTCommunicator.START_PROGRAM, "bt1.rxe");
sendBTCmessage(1000, BTCommunicator.SENDMAILBOX, "Kilroy was here");
sendBTCmessage(3000, BTCommunicator.READMAILBOX,"",1);
the SendBTCmessage (int,int,String,int) is new...
void sendBTCmessage(int delay, int message, String name, int mBox) { Bundle myBundle = new Bundle(); myBundle.putInt("message", message); myBundle.putString("name", name); myBundle.putInt ("MailBox",mBox); Message myMessage = myHandler.obtainMessage(); myMessage.setData(myBundle); Log.d(TAG,String.format("sendBTCmessageStringMbox %d %d %s mBox=%d",delay, message,name,mBox));
if (delay == 0)
btcHandler.sendMessage(myMessage);
else
btcHandler.sendMessageDelayed(myMessage, delay);
}
This sends the request to a handler where...
case SENDMAILBOX:
Log.d(TAG,String.format("case SENDMAILBOX %d %s",myMessage.getData().getInt("value1"), myMessage.getData().getString("name")));
sendMailbox(myMessage.getData().getInt("MailBox"),myMessage.getData().getString("name"));
break;
case READMAILBOX:
Log.d(TAG,String.format("case ReadMAILBOX %d %s",myMessage.getData().getInt("MailBox"), myMessage.getData().getString("name")));
readMailbox(myMessage.getData().getInt("MailBox"));
break;
case START_PROGRAM:
startProgram(myMessage.getData().getString("name"));
break;
the message is further prepared, the "startProgram" is standard and works fine...
private void startProgram(String programName) {
byte[] message = LCPMessage.getStartProgramMessage(programName);
Log.d(TAG,String.format("startProgram %s %s",programName,LCPMessage.getStartProgramMessage(programName)));
sendMessageAndState(message);
}
private void readMailbox(int mBox){
byte[] message = LCPMessage.getReadMailboxMessage(mBox);
Log.d(TAG,String.format("readMailbox %d",mBox));
logByteArray(TAG,"sendMailBox",message);
sendMessageAndState(message);
}
private void sendMailbox(int mBox,String text){
byte[] message = LCPMessage.getMailBoxMessage(mBox,text);
Log.d(TAG,String.format("mail box message %s %d",text,message.length));
logByteArray(TAG,"sendMailBox",message);
sendMessageAndState(message);
}
The final preparation is ...
public static byte[] getMailBoxMessage(int mBox, String text){
byte[] message = new byte[text.length()+5];
message[0] = DIRECT_COMMAND_NOREPLY;
message[1] = MESSAGE_WRITE;
message[2] = (byte)mBox;
message[3] = (byte)(text.length()+1);
for (int pos=0; pos<text.length(); pos++)
message[4+pos] = (byte) text.charAt(pos);
message[text.length()+4] = 0;
return message;
}
public static byte[] getReadMailboxMessage(int mBox){
byte[] message = new byte[5];
message[0] = DIRECT_COMMAND_REPLY;
message[1] = MESSAGE_READ;
message[2] = (byte)(mBox+10);
message[3] = (byte)mBox;
message[4] = (byte)0x01; //true
return message;
}
public static byte[] getStartProgramMessage(String programName) {
byte[] message = new byte[22];
message[0] = DIRECT_COMMAND_NOREPLY;
message[1] = START_PROGRAM;
// copy programName and end with 0 delimiter
for (int pos=0; pos<programName.length(); pos++)
message[2+pos] = (byte) programName.charAt(pos);
message[programName.length()+2] = 0;
return message;
}
The problem I have is that the NXT program to receive the message works fine, but I cannot figure out how to read what is in the mailbox http://www.android-tele-health.com/bt1.rbt is the program running in the NXT.
Any help appreciated...Thanks John