Java Can';无法理解Android蓝牙示例中的mHandler.ActainMessage()

Java Can';无法理解Android蓝牙示例中的mHandler.ActainMessage(),java,android,bluetooth,handler,inputstream,Java,Android,Bluetooth,Handler,Inputstream,我正在进行蓝牙rfcomm连接。Android示例中有一行我不理解,不幸的是,我在其他问题和参考资料中找不到好的答案 以下是全部代码: public void run() { byte[] buffer = new byte[1024]; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputSt

我正在进行蓝牙rfcomm连接。Android示例中有一行我不理解,不幸的是,我在其他问题和参考资料中找不到好的答案

以下是全部代码:

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();
            } 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) { }
    }
我听不懂这句话:

 // Read from the InputStream
                    bytes = mmInStream.read(buffer);    
// Send the obtained bytes to the UI activity
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                                .sendToTarget();
mHandler
未在此代码中定义,也未定义
MESSAGE\u READ

我不明白
字节的作用是什么


我想,正如注释中提到的,它将接收到的字节发送到我设置为主要活动的活动。我是否可以在我的主活动中创建一个
静态文本视图,而不是sendToTarget(),以显示收到的消息?

处理程序的主要目标是提供生产者线程和消费者线程之间的接口,这里是UI线程和工作线程之间的接口。
处理程序的实现进入使用者线程

在您的情况下,您希望在线程之间进行
MESSAGE\u READ
通信

没有处理程序,您就无法从主活动线程中执行任何操作

因此,在主要活动中寻找
mHandler
启动

默认处理程序init应类似于:

Handler mHandler = new Handler(){
 @Override
    public void handleMessage(Message msg) {
 /**/
  }
};
如果使用Eclipse,请单击项目->Ctrl+H->文件搜索->处理程序

或者在记事本+->Serch->Find in files中

[编辑]

final int MESSAGE_READ = 9999; // its only identifier to tell to handler what to do with data you passed through.  

// Handler in DataTransferActivity
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
  switch (msg.what) {
    case SOCKET_CONNECTED: {
      mBluetoothConnection = (ConnectionThread) msg.obj;
      if (!mServerMode)
        mBluetoothConnection.write("this is a message".getBytes());
      break;
    }
    case DATA_RECEIVED: {
      data = (String) msg.obj;
      tv.setText(data);
      if (mServerMode)
       mBluetoothConnection.write(data.getBytes());
     }
     case MESSAGE_READ:
      // your code goes here
我相信您必须实现以下功能:

new ConnectionThread(mBluetoothSocket, mHandler);

我找到的源代码

mHandler可能是一个实例变量,在类中的某个地方声明。MESSAGE_READ可能是一个常量,在类中或通过静态导入定义。谢谢,字节如何?它做什么?嗯,我想我应该在这里写完整的代码。我将编辑我的问题。好吧,没有人会完整阅读您的代码,首先阅读Android中的处理程序,@MaximShoustin在查询中,如果蓝牙设备连接成功,因为您可以执行读取从蓝牙设备发送到应用程序的数据的功能?我已经准备好了,但是我无法得到他们发送给我的蓝牙device@sioesi建议您在这里获取完整的教程/示例,这样您可以看到所有零件是如何连接的,并且可以从一个工作示例开始(我今天也在做同样的事情,昨天我开始在这条路上尝试为我的DIY电动滑板制作一个蓝牙遥控器……我把最后一个遥控器拆开并接线错误)