Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在阵列中存储arduino的串行打印数据_Java_Android_Bluetooth_Arduino - Fatal编程技术网

Java 在阵列中存储arduino的串行打印数据

Java 在阵列中存储arduino的串行打印数据,java,android,bluetooth,arduino,Java,Android,Bluetooth,Arduino,我正在使用蓝牙连接将串行数据从Arduino发送到我的Android应用程序。我用来连接Android设备的代码是BluetoothChat.java,这是谷歌搜索到的示例代码 示例代码在列表视图中显示数据,我想将其存储在数组中 这是从Arduino读取序列打印的代码 case MESSAGE_READ: byte[] readBuf = (byte[]) msg.obj; String readMessage = new String(read

我正在使用蓝牙连接将串行数据从Arduino发送到我的Android应用程序。我用来连接Android设备的代码是
BluetoothChat.java
,这是谷歌搜索到的示例代码

示例代码在
列表视图中显示数据,我想将其存储在数组中

这是从Arduino读取序列打印的代码

case MESSAGE_READ:

            byte[] readBuf = (byte[]) msg.obj;
            String readMessage = new String(readBuf);
            mConversationArrayAdapter.add(readMessage);
            int a = readBuf.length;

            //coordinates[] is the exact length necessary as given by double[a], and contains no more and
            //no fewer positions than required.

            double coordinates[] = new double[a];

            //Check the values of the data in readBuf[] and make the necessary conversions to store in the
            //variables.
            for(int i=0; i<a; i++){

                int val=readBuf[i];

                /*
                *(int) val/8 will divide val and round down to the nearest integer.
                *val%8 divides by 8 but takes the remainder which is the value of
                *all variables above x1, though the math works with x1.
                */

                coordinates[(int) val/8] = val%8;

            }
回归作为一个独立的Java应用程序独立工作

coordinates[(int) val/8] = val%8;
这句话让我觉得很奇怪。创建一个长度为readBuf的数组。那么为什么要使用
(int)val/8
之类的东西来存储数组中的值呢

大概

coordinates[i] = val%8;
这就是你想要的。或者至少
坐标[i]=something
是使用
for
循环在数组中存储值的正确方法

coordinates[i] = val%8;