我正在开发一个应用程序,我需要通过蓝牙将3个搜索条的值发送到PCB。我已经完成了基于bluetoothchat示例的所有蓝牙代码。我首先修改它以发送包含这3个值的字符串。但现在,我需要做一些更难的事情,我不知道该怎么做。
首先,在应用程序中我修改了搜索栏,然后我点击发送按钮。在代码中,我需要为每个seekbar的值设置一个字符串,因为我需要访问MCU变量并设置每个变量地址,值,CRC等...
所以,我需要知道正确的方法。这是我定义send函数的代码:
/**
* 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.
在我得到要发送的字节的部分中,我不知道如果正确的方法是在1上添加这5个字符串并发送这个(如果我这样做可能会很长),或创建一个函数来分别发送这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);
}
答案 0 :(得分: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活动中,您将获得字符串版本的消息,以及字节[] 1。
如果您想获取邮件的第一个字节,只需在byte myByte = readBuf[0]
之前添加String readMessage = new String(readBuf, 0, msg.arg1);
然后,String readMessage = new String(myByte, 0, msg.arg1);