0

我可以写信到 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) { Bundle myBundle = new Bundle(); myBundle.putInt("消息", 消息); myBundle.putString("name", name); myBundle.putInt("邮箱",mBox); 消息 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);
}

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

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。

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

4

1 回答 1

2

MESSAGE_READ命令请求遥控器将邮箱中的第一条可用消息作为回复NXT发送回您。这就是为什么你必须使用命令类型,而不是.DIRECT_COMMAND_REPLYDIRECT_COMMAND_NOREPLY

您没有显示如何从蓝牙流中读取回复数据,但看起来您的方法SendMessageAndState()需要能够从远程读取回复数据NXT并将其作为适当的数据类型返回给您,如果您请求回复从NXT使用DIRECT_COMMAND_REPLYSYSTEM_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()和返回的蓝牙套接字getOutputStream()返回createRfcommSocketToServiceRecord(SPP_UUID)MASK_NOREPLY在我的包中的其他地方定义为:

  public static byte MASK_NOREPLY = (byte) 0x80;

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

顺便说一句,您的 NXT-G 程序看起来不错。——迈克

于 2012-11-07T08:08:26.440 回答