繁体   English   中英

Android与Arduino的蓝牙通信

[英]Android bluetooth communication with Arduino

我目前正在看Google的蓝牙聊天示例。 目标是基于此示例在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个字节长的字节数组。 不管传入的字节长度是多少。 如果我能说明收到了多少个字节,那将是非常好的。
  • 字节似乎无法一次全部读取。 例如上面的代码被多次调用,但是缓冲区从不包含我从Arduino发送的所有字节。 有时只有第一个字节,然后只有最后一个字节。
  • 尽管此代码被多次调用,但我的主要活动仅收到一次通知。 怎么可能?

什么是正确的方法来做到这一点。 是否应该实现一种收集和连接字节的机制? 还是我以错误的方式使用此代码?

我总是很难一次读取大于一个的字节缓冲区。 这是因为无法保证您正确接收了所有字节。 我的解决方法是一次调用一次重复读取一个字节并填充我的缓冲区。 这样,如果我的任何字节都没有被读取,请在我的connectThread的I / O捕获部分中捕获该异常,并可以选择处理它,但是我想这样做。

样本connectedThread

private class ConnectedThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final InputStream mmInStream;
            private final OutputStream mmOutStream;

            public ConnectedThread(BluetoothSocket socket) {
                mmSocket = socket;
                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the input and output streams, using temp objects because
                // member streams are final
                try {
                    tmpIn = socket.getInputStream();
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) { }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
            }


            public void run() {
                byte[] buffer; // buffer store for the stream
                int bytes; // bytes returned from read()
                // Keep listening to the InputStream until an exception occurs
                while (true) {
                    try {
                        // Read from the InputStream
                       // You can define this buffer to be 1024 or anything you like
                        buffer = new byte[3];
                            mmOutStream.write(253);
                            bytes = mmInStream.read(buffer,0,1);
                            mmOutStream.write(254);
                            bytes = mmInStream.read(buffer,1,1);
                            mmOutStream.write(255);
                            bytes = mmInStream.read(buffer,2,1);
                            mHandler.obtainMessage(MESSAGE_READ, buffer).sendToTarget();
                        }
                        catch (IOException e) {
                            break;
                     }
                }
             }
            /* Call this from the main activity to send data to the remote device */
            public void write(byte[] bytes) {
                try {
                    mmOutStream.write(bytes);
                } catch (IOException e) { }
            }
        }

在这种情况下,我使用无符号字节数组表示0-255之间的整数。 此外,我使用值255-253作为命令来告诉Arduino向我发送某些类型的信息。 您不必设置任何值来表示arduino的命令,而是只需告诉arduino遍历每次收到信息请求时需要发送的值即可。 我发现这是确认收到的字节数(即byte[] buffer的大小)的唯一方法之一。尽管在这种情况下,我没有在catch语句中为您可以放置​​的connectedThread放入任何内容那里的读命令,以确认您收到一个字节。

讯息处理常式

这是我处理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");

            }
        }       
    };

在这种情况下,我正在手机上显示实时传感器数据。 但是你真的可以做任何事情。

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM