Android/Arduino蓝牙通讯 [英] Android/Arduino bluetooth communication

查看:30
本文介绍了Android/Arduino蓝牙通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接成功建立并能够发送数据.

Link successfully established and able to send data.

Android 会在我们更改 SeekBar 数据时发送它.

Android is sending SeekBar data when ever we change it.

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(seekBar.getId() == R.id.seekBar)
    {
        speed.setText(String.valueOf(progress));
        String outputData = String.valueOf(progress);
        streams.write(outputData.getBytes());
    }
}

streams.write() 将数据写入SocketOutputStream.

问题在于数据的格式.如果我发送'25',arduino在执行Serial.read()时正在接收'2'、'5'.

Problem is with the format of data.If I Send '25' arduino is receiving '2','5' when I do Serial.read().

outputData转换成字节后,数据的格式是什么?一切都被 \0 终止了吗?

What is the format of data, when outputData is converted into bytes? Is everything terminated by \0?

我需要检索整数而不是个位数.

I need to retrieve the whole number instead of single digits.

推荐答案

arduinoboard 似乎在逐字节读取 RX-Stream.如果您发送25",它会传输字符2"(即 0x32/十进制 50)的 ascii 字节,然后是字符5"(即 0x35/十进制 53)的 ascii 表示.arduino 将这些数字解释为字符.因此,如果您要传输的数字低于 256,您可以执行以下操作:在安卓上:

the arduinoboard seems to read the RX-Stream byte by byte. If you send "25" it transmits the ascii byte for the character'2' (which is 0x32 / decimal 50) and then the ascii representation for the character '5' (which is 0x35 / decimal 53). The arduino interprets these numbers as characters. So if the number you want to transmit is lower than 256 you can do: On Android:

if(seekBar.getId() == R.id.seekBar)
    {
        speed.setText(String.valueOf(progress));
        if(progress<256)
            streams.write((byte)progress);
    }

为确保 Arduino 正确解释,请将接收到的字符用作短字符而不是字符.

To make sure the Arduino interprets it right, use the received character as a short and not as a character.

希望能帮到你

这篇关于Android/Arduino蓝牙通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆