Android Studio通过蓝牙读取和写入数据

时间:2016-05-09 14:32:29

标签: android

我目前正在编写一个连接2个配对设备的应用程序,允许它们相互发送数据。我对Android开发很新,我遇到了一些问题,我似乎无法让数据传输工作。

我一直在关注本教程:http://developer.android.com/guide/topics/connectivity/bluetooth.html

以下是我的一些代码:

- “发送”按钮,用于发送字符串

btnSendData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String message = editTxtMsg.getText().toString();

            if (connectedThread != null) {
                connectedThread.write(message.getBytes());
            }
        }
    });

-The 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 = new byte[1024];  // 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
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

                /*Bundle data =new Bundle();
                //data.p
                Message m =mHandler.obtainMessage();

                m.setData(data);
                mHandler.sendMessage(m);*/

            } 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);

            mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
        } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

- 处理程序

private final Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case MESSAGE_READ:
                byte[] readBuffer = (byte[]) msg.obj;
                String readMessage = new String(readBuffer, 0, msg.arg1);
                //arrayAdapterMsg.add(btDevice.getName() + ": " + readMessage);
                aryLstMsg.add(btDevice.getName() + ": " + readMessage);

                arrayAdapterMsg.notifyDataSetChanged();

                break;

            case MESSAGE_WRITE:
                byte[] writeBuffer = (byte[]) msg.obj;
                String writeMessage = new String(writeBuffer);
                //arrayAdapterMsg.add("Me: " + "test");
                aryLstMsg.add("Me: " + writeMessage);

                arrayAdapterMsg.notifyDataSetChanged();

                break;
        }
    }
};

我不知道为什么它不起作用,其他人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

检查蓝牙权限 它应该是

<uses-permission android:name="android.permission.BLUETOOTH" />


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

答案 1 :(得分:0)

您正在尝试从write()函数访问缓冲区,但它是在run()中定义的。尝试全局定义它或通过传递合适的参数从run中调用write()。