admin 管理员组

文章数量: 1086019


2024年3月26日发(作者:itoa函数touwenj)

对于Android的英文帮助文档,总是看了记不住,远不如对中文资料那么印象深,所以下面

的叙述都是对Android帮助文档Bluetooth的翻译。

一、Bluetooth

Android平台包含了对Bluetooth协议栈的支持,允许机器通过Bluetooth设备进行无线数

据交换。应用框架通过Android Bluetooth API访问Bluetooth功能模块。 这些API能让应

用无线连接其他Bluetooth设备,实现点对点和多点之间的通信。

运用蓝牙API,Android应用程序可以完成如下操作:

1、扫描其他Bluetooth设备。

2、查询配对Bluetooth设备的本地Bluetooth适配器。

3、建立RFCOMM通道。

4、通过服务探索连接到其他设备。

5、与其他设备进行数据传输。

6、管理多个连接

二、The Basics

本文描述如何使用Android Bluetooth APIs完成Bluetooth通讯的4个必要任务:设置

Bluetooth,搜寻本地配对或者可用的Bluetooth设备,连接Bluetooth设备,与Bluetooth

设备进行数据传输。

所有可用的Bluetooth APIs都包含在oth包中。下面是建立Bluetooth连接

需要用到的类和接口的总结:

1、BluetoothAdapter

描述本地Bluetooth适配器(Bluetooth接收器)。BluetoothAdapter是所有Bluetooth相关

活动的入口。运用BluetoothAdapter可以发现其他Bluetooth设备,查询连接(或配对)的

设备列表,用已知MAC地址实例化一个BluetoothDevice对象,创建一个

BluetoothServerSocket对象侦听其他设备的通信。

2、BluetoothDevice

描述一个远程Bluetooth设备。可以用它通过一个BluetoothSocket请求一个远程设备的连

接,或者查询远程设备的名称、地址、类、连接状态等信息。

3、BluetoothSocket

描述一个Bluetooth Socket接口(类似于TCP Socket)。应用通过InputStream和

OutputStream.与另外一个Bluetooth设备交换数据,即它是应用与另外一个设备交换数据

的连接点。

4、BluetoothServerSocket

描述一个开放的socket服务器,用来侦听连接进来的请求(类似于RCP ServerSocket)。

为了连接两个Android设备,一个设备必须使用该类来开启一个socket做服务器,当另外

一个设备对它发起连接请求时并且请求被接受时,BluetoothServerSocket会返回一个连接

的BluetoothSocket对象。

5、BluetoothClass

描述一个Bluetooth设备的一般规格和功能。这个是用来定义设备类和它的服务的只读属性

集。然而,它并不是可靠的描述设备支持的所有Bluetooth配置和服务,而只是一些设备类

型的有用特征。

6、BluetoothProfile

描述Bluetooth Profile的接口。Bluetooth Profile是两个设备基于蓝牙通讯的无线接口描述。

(对Bluetooth Profile的详细解释,来自百度:为了更容易的保持Bluetooth设备之间的兼

容,Bluetooth规范中定义了 Profile。Profile定义了设备如何实现一种连接或者应用,你可

以把Profile理解为连接层或者应用层协议。 比如,如果一家公司希望它们的Bluetooth芯

片支援所有的Bluetooth耳机,那么它只要支持HeadSet Profile即可,而无须考虑该芯片

与其它Bluetooth设备的通讯与兼容性问题。如果你想购买Bluetooth产品,你应该了解你

的应用需要哪些Profile来完成,并且确保你购买的Bluetooth产品支持这些Profile。)

7、BluetoothHeadset

提供移动电话的Bluetooth耳机支持。包括Bluetooth耳机和Hands-Free (v1.5) profiles。

8、BluetoothA2dp

定义两个设备间如何通过Bluetooth连接进行高质量的音频传输。

A2DP(Advanced Audio Distribution Profile):高级音频传输模式。

9、eListener

一个接口描述,在与服务连接或者断连接的时候通知BluetoothProfile IPC(这是内部服务

运行的一个特定的模式)。

三、Bluetooth Permissions

要使用Bluetooth功能,至少需要2个Bluetooth权限:BLUETOOTH和

BLUETOOTH_ADMIN.

BLUETOOTH:用来授权任何Bluetooth通信,如请求连接,接受连接,传输数据等。

BLUETOOTH_ADMIN:用来授权初始化设备搜索或操作Bluetooth设置。大多数应用需要


本文标签: 设备 连接 支持 请求 描述