0

我正在开发一个应用程序,我需要通过蓝牙将 3 个搜索栏的值发送到 PCB。我已经根据 bluetoothchat 示例完成了所有蓝牙代码。我首先对其进行了修改以发送具有这 3 个值的字符串。但是现在,我需要做一些更困难的事情,我不知道该怎么做。

首先,在应用程序中我修改搜索栏,然后单击发送按钮。在代码中,我需要为每个 seekbar 的值设置一个字符串,因为我需要访问 MCU 变量并设置每个变量的地址、值、CRC 等...

所以,我需要知道正确的方法来做到这一点。这是我定义发送函数的代码:

/**
 * SEND THREAD
 */
/**[Start Thread + Send command + Nº bytes thread + Nº bytes variable + Address + Variable value + CRC]*/

public void sendValues() {

    /**Set the seekbars values into a string*/
    send_value1 = Integer.toString(savedProgress1);
    send_value2 = Integer.toString(savedProgress2);
    send_value3 = Integer.toString(savedProgress3);



    String message1 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_1+" "+Value+" "+CRC;
    String message2 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_2+" "+Value+" "+CRC;
    String message3 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_3+" "+Value+" "+CRC;
    String message4 = start_thread+" "+send_command+" "+num_byte_trama2+ " "+num_byte_variable+" "+pos_reg_save_request+" "+Value+" "+CRC;
    String message5 = start_thread+" "+send_command+" "+num_byte_trama2+ " "+num_byte_variable+" "+pos_reg_save_status+" "+Value+" "+CRC;



    /**Check that we're actually connected before trying anything*/
    if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }

    /**Get the message bytes and tell the Transmission to write*/
    byte[] send = message.getBytes();
    GlobalVar.mTransmission.write(send);

    /**Reset out string buffer to zero*/
    GlobalVar.mOutStringBuffer.setLength(0);
}

有几件事我请你帮助我:

1- Need to know how to calculate the CRC
2- I need to send these 5 strings together when pressing the send button. 

在我获取要发送的字节的部分,我不知道正确的方法是将这 5 个字符串添加到 1 上并发送这个(如果我这样做可能会很长),或者创建一个函数来分别但同时发送这 5 个。

这是编辑后的代码,用于一一发送每条消息:

    /**Conversion to decimal of the seekbar's % value*/
    send_int1 = ((savedProgress1 * 20480) / 100) * -1;
    send_int2 = ((savedProgress2 * 20480) / 100) * -1;
    send_int3 = ((savedProgress3 * 20480) / 100) * -1;

    /**Conversion to string of the previous values to send in the string message*/
    sendValue1 = Integer.toString(send_int1);
    sendValue2 = Integer.toString(send_int1);
    sendValue3 = Integer.toString(send_int1);


    String message1 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_1+" "+sendValue1+" " ;
    String message2 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_2+" "+sendValue2+" " ;
    String message3 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_3+" "+sendValue3+" " ;
    String message4 = start_thread+" "+send_command+" "+num_byte_trama2+" "+num_byte_variable+" "+pos_reg_save_request+" " ;
    String message5 = start_thread+" "+send_command+" "+num_byte_trama2+" "+num_byte_variable+" "+pos_reg_save_status+" " ;



    /**Check that we're actually connected before trying anything*/
    if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }

    /**Get the message bytes and tell the Transmission to write*/
    byte[] send1 = message1.getBytes();
    GlobalVar.mTransmission.write(send1);

    //Wait untill I receive the confirmation from the MCU

    byte[] send2 = message2.getBytes();
    GlobalVar.mTransmission.write(send2);

    byte[] send3 = message3.getBytes();
    GlobalVar.mTransmission.write(send3);

    byte[] send4 = message4.getBytes();
    GlobalVar.mTransmission.write(send4);

    byte[] send5 = message5.getBytes();
    GlobalVar.mTransmission.write(send5);


    /**Reset out string buffer to zero*/
    GlobalVar.mOutStringBuffer.setLength(0);
}
4

1 回答 1

1

对于您的框架,我建议您使用这种框架:

final byte[] HEADER = AA11 // For example // When you want to send a message : Strign messageToSend = new String(HEADER) + yourStringMessage

收到框架后,您可以更轻松地分析框架。

那么,对于CRC,如果你不告诉CRC的种类,我无法回答。在我的应用程序中,我使用了

private static char createCRC(byte[] frame)
{ 
    int crc = 0;
    for(byte i : frame)
    {
        crc = crc^i;
    }
   return (char)crc;
}

通过“XORing”我的消息的每个字节来创建 CRC,然后检查 CRC 非常容易

更新:嗯,我终于明白了。

在 BluetoothChat 活动中,您会获得消息的字符串版本和字节 [] 版本。如果要获取消息的第一个字节,只需在 Thenbyte myByte = readBuf[0]之前添加,String readMessage = new String(readBuf, 0, msg.arg1);String readMessage = new String(myByte, 0, msg.arg1);

于 2013-08-02T11:01:06.787 回答