Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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
Android蓝牙示例_Android_Bluetooth - Fatal编程技术网

Android蓝牙示例

Android蓝牙示例,android,bluetooth,Android,Bluetooth,谁能给我安卓蓝牙通信教程链接或提示?请不要告诉我参考BluetoothChat示例,我只知道如何发现和连接设备,但不知道如何通过Bluetooth发送和接收数据 我实际上在做一个安卓和嵌入式蓝牙设备项目。 请帮助我。我也使用了以下链接,因为其他人建议您使用蓝牙通信 您只需要一个类BluetoothChatService.java 此类具有以下线程: 接受 连接 连接的 现在,当您调用BluetoothChat服务的start函数时,如: mChatService.start(); 它启动ac

谁能给我安卓
蓝牙
通信教程链接或提示?请不要告诉我参考BluetoothChat示例,我只知道如何发现和连接设备,但不知道如何通过Bluetooth发送和接收数据

我实际上在做一个安卓和嵌入式
蓝牙
设备项目。

请帮助我。

我也使用了以下链接,因为其他人建议您使用蓝牙通信

您只需要一个类
BluetoothChatService.java

此类具有以下线程:

  • 接受
  • 连接
  • 连接的
  • 现在,当您调用BluetoothChat服务的start函数时,如:

    mChatService.start();
    
    它启动accept线程,这意味着它将开始寻找连接

    现在当你打电话的时候

    mChatService.connect(<deviceObject>,false/true);
    
    此方法在两个设备中启动连接的线程: 使用此套接字对象连接的线程获取到其他设备的输入和输出流。 并在while循环中调用inputstream上的
    read
    函数,以便它总是尝试从其他设备读取,以便每当其他设备发送消息时,该read函数返回该消息

    BluetoothChatService还有一个
    write
    方法,它将
    byte[]
    作为输入,并在连接的线程上调用write方法

    mChatService.write("your message".getByte());
    
    连接线程中的write方法只需将此字节数据写入另一个设备的outputsream

    public void write(byte[] buffer) {
       try {
           mmOutStream.write(buffer);
        // Share the sent message back to the UI Activity
        // mHandler.obtainMessage(
        // BluetoothGameSetupActivity.MESSAGE_WRITE, -1, -1,
        // buffer).sendToTarget();
        } catch (IOException e) {
        Log.e(TAG, "Exception during write", e);
         }
    }
    

    现在,要在两台设备之间进行通信,只需调用mChatService上的write功能,并处理您将在另一台设备上收到的消息。

    有人遇到过此聊天服务无法与设备连接的问题吗,甚至它连接并立即断开连接?Android开发者页面上有并解释了如何使用蓝牙功能:我希望这会有所帮助。我想最好使用以下工具了解蓝牙连接:)我已经经历了这个过程,我可以发现设备,连接它们,但是如何在BT上发送和接收数据?你可以以BluetoothChat为例发送数据。如果您查看代码,文本消息字符串在通过蓝牙发送之前将转换为字节[]。因此,将数据转换为字节[],然后发送过来。IMHO聊天示例仅适用于聊天或更快的通信。。当你需要每秒发送100多条信息时,我不确定这是否有用。
    public void write(byte[] buffer) {
       try {
           mmOutStream.write(buffer);
        // Share the sent message back to the UI Activity
        // mHandler.obtainMessage(
        // BluetoothGameSetupActivity.MESSAGE_WRITE, -1, -1,
        // buffer).sendToTarget();
        } catch (IOException e) {
        Log.e(TAG, "Exception during write", e);
         }
    }