安卓蓝牙示例 [英] Android Bluetooth Example

查看:16
本文介绍了安卓蓝牙示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anybody give me Android Bluetooth communication tutorial links or hints? Please don't tell me to refer to the BluetoothChat example, I can only understand how to discover and connect to devices but don't know how to send and receive the data over Bluetooth.

我实际上正在开发一个 Android 和嵌入式 Bluetooth 设备项目.请帮帮我.

I am actually working on an Android and embedded Bluetooth device project. Please help me out.

推荐答案

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

I have also used following link as others have suggested you for bluetooth communication.

http://developer.android.com/guide/topics/connectivity/蓝牙.html

你只需要一个类 BluetoothChatService.java

这个类有以下线程:

  1. 接受
  2. 连接
  3. 已连接

现在当你调用BluetoothChatService的启动函数时:

Now when you call start function of the BluetoothChatService like:

mChatService.start();

它开始接受线程,这意味着它将开始寻找连接.

It starts accept thread which means it will start looking for connection.

现在当你打电话

mChatService.connect(<deviceObject>,false/true);

这里的第一个参数是设备对象,您可以从配对设备列表中获取,或者当您扫描设备时,您将获得范围内的所有设备,您可以将该对象传递给此函数,第二个参数是一个布尔值,以确保安全或不安全连接.

Here first argument is device object that you can get from paired devices list or when you scan for devices you will get all the devices in range you can pass that object to this function and 2nd argument is a boolean to make secure or insecure connection.

connect 函数将开始连接线程,该线程将查找任何正在运行接受线程的设备.

connect function will start connecting thread which will look for any device which is running accept thread.

当发现这样的设备时,accept线程和connection线程都会调用BluetoothChatService中的connected函数:

When such a device is found both accept thread and connecting thread will call connected function in BluetoothChatService:

connected(mmSocket, mmDevice, mSocketType);

此方法在两个设备中启动连接线程:使用此套接字对象连接的线程获取到其他设备的输入和输出流.并在 while 循环中调用 inputstream 上的 read 函数,以便它始终尝试从其他设备读取,以便每当其他设备发送消息时,此读取函数都会返回该消息.

this method starts connected thread in both the devices: Using this socket object connected thread obtains the input and output stream to the other device. And calls read function on inputstream in a while loop so that it's always trying read from other device so that whenever other device send a message this read function returns that message.

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

BluetoothChatService also has a write method which takes byte[] as input and calls write method on connected thread.

mChatService.write("your message".getByte());

连接线程中的write方法只是将这个字节数据写入其他设备的outputsream.

write method in connected thread just write this byte data to outputsream of the other device.

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 函数并处理您将在另一台设备上收到的消息.

Now to communicate between two devices just call write function on mChatService and handle the message that you will receive on the other device.

这篇关于安卓蓝牙示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆