使用MindStorms通过MailBox进行Android NXT通信

时间:2012-09-19 19:21:05

标签: android bluetooth nxt

我可以写入NXT上的邮箱,但不能(还)从中读取。

安装程序将使用GitHub中的MindDroid作为启动程序。

sendBTCmessage(0, BTCommunicator.START_PROGRAM, "bt1.rxe");
            sendBTCmessage(1000, BTCommunicator.SENDMAILBOX, "Kilroy was here");
            sendBTCmessage(3000, BTCommunicator.READMAILBOX,"",1);

SendBTCmessage(int,int,String,int)是新的...

void sendBTCmessage(int delay,int message,String name,int mBox){         捆绑myBundle = new Bundle();         myBundle.putInt(“message”,message);         myBundle.putString(“name”,name);         myBundle.putInt(“MailBox”,mBox);         消息myMessage = myHandler.obtainMessage();         myMessage.setData(myBundle);         Log.d(TAG,String.format(“sendBTCmessageStringMbox%d%d%s mBox =%d”,延迟,消息,名称,mBox));

    if (delay == 0)
        btcHandler.sendMessage(myMessage);
    else
        btcHandler.sendMessageDelayed(myMessage, delay);
}

这会将请求发送到处理程序,其中......

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;

消息进一步准备,“startProgram”是标准的并且工作正常......

    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);
}

最后的准备工作是......

    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;
}

我遇到的问题是接收邮件的NXT程序运行正常,但我无法弄清楚如何阅读邮箱中的内容 http://www.android-tele-health.com/bt1.rbt是在NXT中运行的程序。

任何帮助表示赞赏......谢谢约翰

1 个答案:

答案 0 :(得分:2)

MESSAGE_READ命令请求远程NXT将邮箱中的第一个可用邮件作为回复发送给您 。这就是为什么你必须使用DIRECT_COMMAND_REPLY作为命令类型,而不是DIRECT_COMMAND_NOREPLY

您没有显示您是如何从蓝牙流中读取回复数据的,但看起来您的方法SendMessageAndState()需要能够从远程NXT读取回复数据并将其返回如果您使用NXTDIRECT_COMMAND_REPLY请求SYSTEM_COMMAND_REPLY的回复,则将您视为适当的数据类型。

下面的代码片段显示了我是如何解决这个问题的(请原谅我,如果它不是世界上最优雅的Java代码!)

public synchronized byte[] sendBTMessage(byte[] request) {
    int lsb = request.length;
    int msb = request.length >>> 8;
    try {
        mOut.write((byte) lsb);
        mOut.write((byte) msb);
        mOut.write(request);
    } catch (IOException e) {
        Log.e(TAG, "sendBTMessage: Write failed.");
    }
    if ((request[0] & MASK_NOREPLY) == 0) { // Reply requested
        byte[] reply = readBTMessage();
        if (reply == null) {
            return null;
        }
        if (reply[0] != 0x02) {
            // Not a reply?
            Log.e(TAG, "sendBTMessage read a message that was not a reply.");
            Log.e(TAG, "Hex: " + HexValues(reply));
            return new byte[] {(byte) 0xFF}; 
        }
        if (reply[1] != request[1]) {
            // Reply for incorrect request?
            Log.e(TAG, "sendBTMessage received a reply for the wrong request.");
            Log.e(TAG, "Hex: " + HexValues(reply));
            return new byte[] {(byte) 0xFF}; 
        }
        byte[] replymessage = new byte[reply.length - 2];
        System.arraycopy(reply, 2, replymessage, 0, replymessage.length);               
        return replymessage;
    }
    return new byte[] {0};
}

public byte[] readBTMessage() {
    byte[] buffer = new byte[66];  // All BT messages are a maximum of 66 bytes long
    int numBytes;
    try {
        numBytes = mIn.read(buffer);
    } catch (IOException e) {
        Log.e(TAG, "readBTMessage: Read failed.");
        return null;
    }
    if (numBytes <= 0) return null;
    int msgLength = buffer[0] + (buffer[1] << 8);
    if (numBytes != msgLength + 2) {
        Log.e(TAG, "readBTMessage: BT Message wrong length.");
        Log.e(TAG, "BT Message: " + HexValues(buffer));
        return null;
    }
    byte[] result = new byte[msgLength];
    System.arraycopy(buffer, 2, result, 0, msgLength);
    return result;
}

mInmOut分别是蓝牙套接字的InputStream和OutputStream,通过调用getInputStream()返回的BluetoothSocket上的getOutputStream()createRfcommSocketToServiceRecord(SPP_UUID)返回。 MASK_NOREPLY在我的包中的其他位置定义为:

  public static byte MASK_NOREPLY = (byte) 0x80;

因此,您的消息发送方法(或附近的某个地方)应准备好在您请求时从远程NXT读取回复。然后,您将能够看到您从远程NXT请求的邮箱消息。

你的NXT-G程序看起来很好,BTW。 - 迈克