【问题标题】:Send and receive string values via bluetooth android通过蓝牙 android 发送和接收字符串值
【发布时间】:2013-08-02 10:20:06
【问题描述】:

我正在开发一个应用程序,我需要通过蓝牙将 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);
}

【问题讨论】:

  • 是什么样的CRC?如果你想在同一帧中发送所有数据,你可以做String stringToSend = message1+message2+message3+message4+message5,但我认为这不是最好的主意。当我必须发送蓝牙帧时,我更喜欢使用标题,如11AA,然后发送帧长度,然后是帧内容。有了它,你会更容易捕捉到一个新的框架。
  • 我正在考虑将每条消息一一发送。我发送第一个,我等到我收到确认,然后发送下一个。我需要计算的 CRC 是针对以下变量:{send_comand, num_byte_trama, num_byte_variable, pos_reg, value}。所以,我需要创建一个函数,为每条消息计算这些变量的 CRC。请帮忙。
  • 是的,但它是哪种 CRC? 16 ? 32 ?

标签: android string bluetooth send


【解决方案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;
}

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

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

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

【讨论】:

  • 我真的不太了解如何计算CRC或者我需要什么样的CRC。我刚刚开始在 android 上使用这个项目进行编程。我做单片机软件的伙伴不知道他在计算什么样的CRC。他只是说我必须对这些变量的字节求和,然后转换为十六进制。关于字符串消息发送,我很欣赏你的建议,但我不知道怎么做,所以我会先尝试我的方法。我想问你的是:
  • 例如,当我发送message1时,MCU会向我发送响应。我需要知道如何获取并阅读此响应,它将类似于 {2b 00 00},而中间的 00 是我需要确认它已正确发送并继续发送消息 2。我使用为此目的修改的 bluetoohchat 示例的代码,但我没有找到在哪里读取以获取响应变量
  • 在aceptThread中有这样的代码://从InputStream中读取 bytes = mmInStream.read(buffer); //将获取的字节发送到UI Activity @ 987654327@但我不知道去哪儿了。
  • 在connectedThread中,如果你的代码是基于BluetoothChat的。 mHandler 将消息发送到 UI
  • 真的,谢谢。我看到这是消息 mConversationArrayAdapter.add(readMessage); 所以,在我的代码中,如何引入这个值并将其拆分以从结果字符串 {2b 00 00} 中获取 00 值??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 2018-01-12
  • 1970-01-01
  • 2015-04-21
  • 2015-11-24
  • 1970-01-01
相关资源
最近更新 更多