Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 Android与Arduino的蓝牙通信_Java_Android_Bluetooth_Serial Port_Arduino - Fatal编程技术网

Java Android与Arduino的蓝牙通信

Java Android与Arduino的蓝牙通信,java,android,bluetooth,serial-port,arduino,Java,Android,Bluetooth,Serial Port,Arduino,我目前正在看谷歌的蓝牙聊天示例。目标是在这个示例的基础上实现android和Arduino之间的通信 虽然从智能手机到Arduino的通信效果很好,但另一个方向却不行: 从Arduino向智能手机发送字节时,以下代码用于接收: // Read from the InputStream bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI Activity mHandler.obtainMessage(Main

我目前正在看谷歌的蓝牙聊天示例。目标是在这个示例的基础上实现android和Arduino之间的通信

虽然从智能手机到Arduino的通信效果很好,但另一个方向却不行: 从Arduino向智能手机发送字节时,以下代码用于接收:

// Read from the InputStream
bytes = mmInStream.read(buffer);

// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
这有以下问题:

  • 在我的主要活动中,我得到一个字节数组,它的长度总是1024字节。不管传入的字节长度是多少。这将是非常好的,如果我有一个ID有多少字节收到
  • 这些字节似乎不能一次全部读取。例如,上面的代码被多次调用,但缓冲区从未包含我从Arduino发送的所有字节。有时只有第一个字节,之后只有最后一个字节
  • 尽管此代码被多次调用,但我的主要活动只收到一次通知。这怎么可能

正确的方法是什么。是否应该实现收集和连接字节的机制?还是我用错了这段代码?

我每次读取大于一个字节缓冲区时总是遇到问题。这是因为无法保证正确接收所有字节。我的工作是一次重复调用read一个字节,然后填充缓冲区。这样,如果我的任何字节没有被读取,我将在connectedThread的I/O catch部分捕获它,并可以选择以我想要的方式处理它

示例connectedThread 在本例中,我使用了一个无符号字节数组来表示0-255之间的整数。此外,我使用值255-253作为命令,告诉我的Arduino向我发送某些类型的信息。您不必设置任何值来表示arduino的命令,相反,您可以告诉arduino在每次收到信息请求时循环它需要发送的值。我发现这是确认收到的字节数(即
byte[]buffer
的大小)的唯一方法之一。虽然在本例中,我没有在connectedThread的catch语句中放入任何内容,但您可以在其中放入read命令以确认收到字节

消息处理程序 下面是我如何处理readBuffer

/*
     * Bluetooth Handler Method
     */
    ConnectedThread connectedThread;
    Handler mHandler = new Handler(){           
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch(msg.what){
                case SUCCESS_CONNECT:
                    // Do Something;
                    Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_SHORT).show();
                    
                    connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                    listView.setVisibility(View.GONE);
                    connectedThread.start();
                    break;
                    
                case MESSAGE_READ:
                    byte[] readBuf = (byte[])msg.obj;
                    int tempInt = byteToInt(readBuf[0]);
                    int speedInt = byteToInt(readBuf[1]);
                    int cadenceInt = byteToInt(readBuf[2]);
                    
                    EditText temperatureData = (EditText)getActivity().findViewById(R.id.temperatureData);
                    temperatureData.setText(Integer.toString(tempInt) + " C" );
                    EditText cadenceData = (EditText)getActivity().findViewById(R.id.cadence);
                    cadenceData.setText(Integer.toString(cadenceInt) + " rpm");
                    EditText speedData = (EditText)getActivity().findViewById(R.id.speed_data);
                    speedData.setText(Integer.toString(speedInt) + " kph");
                    
            }
        }       
    };
在本例中,我在手机上显示实时传感器数据。但你真的可以做任何事


希望这会有所帮助。

首先,你不应该期望所有的字节都同时显示出来——当你异步获取一个字节一次的接口(如Arduino的串行输出)并将其通过一个打包接口(如蓝牙(或以太网或USB等)时,情况并非如此。接下来,在read()调用之后,立即记录read()调用返回的
bytes
值,看看是否合理。如果你走得那么远,你的问题是通知UI或代码有什么。如果你的ARDUINO的数据是文本和新行被终止,你可以考虑将输入流打包在一个输入流读取器中,然后是一个提供Read Load()的BuffErdRe读器。方法,该方法可能适合您的需要。此外,使用Arduino时,请确保您的波特率设置正确,因为Ardunio无法以最快的速度运行,因为您的蓝牙可能无法以最快的速度运行。我应该补充一点,java没有无符号字节数组。所以我用BytePoint(字节b)做了自己的函数来进行转换。你能在这里发布你是如何从Arduino发送数据的吗?作为每个循环的一个字符串?我的示例发送“T:30.39H:33.98PPM:2943”,我需要在Arduino上解析它,并在单独的文本元素中显示
/*
     * Bluetooth Handler Method
     */
    ConnectedThread connectedThread;
    Handler mHandler = new Handler(){           
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch(msg.what){
                case SUCCESS_CONNECT:
                    // Do Something;
                    Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_SHORT).show();
                    
                    connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                    listView.setVisibility(View.GONE);
                    connectedThread.start();
                    break;
                    
                case MESSAGE_READ:
                    byte[] readBuf = (byte[])msg.obj;
                    int tempInt = byteToInt(readBuf[0]);
                    int speedInt = byteToInt(readBuf[1]);
                    int cadenceInt = byteToInt(readBuf[2]);
                    
                    EditText temperatureData = (EditText)getActivity().findViewById(R.id.temperatureData);
                    temperatureData.setText(Integer.toString(tempInt) + " C" );
                    EditText cadenceData = (EditText)getActivity().findViewById(R.id.cadence);
                    cadenceData.setText(Integer.toString(cadenceInt) + " rpm");
                    EditText speedData = (EditText)getActivity().findViewById(R.id.speed_data);
                    speedData.setText(Integer.toString(speedInt) + " kph");
                    
            }
        }       
    };