Android之Bluetooth详解

对于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都包含在android.bluetooth包中。下面是建立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、BluetoothProfile.ServiceListener
一个接口描述,在与服务连接或者断连接的时候通知BluetoothProfile IPC(这是内部服务运行的一个特定的模式<profile>)。

三、Bluetooth Permissions
要使用Bluetooth功能,至少需要2个Bluetooth权限:BLUETOOTHBLUETOOTH_ADMIN.
BLUETOOTH:用来授权任何Bluetooth通信,如请求连接,接受连接,传输数据等。
BLUETOOTH_ADMIN:用来授权初始化设备搜索或操作Bluetooth设置。大多数应用需要它的唯一场合是用来搜索本地Bluetooth设备。本授权的其他功能不应该被使用,除非是需要修改Bluetooth设置的“power manager(电源管理)”应用。
注意:需要BLUETOOTH_ADMIN权限的场合,BLUETOOTH权限也是必需的。
需要在manifest文件中声明Bluetooth权限,示例如下:
<manifest ... >
  <uses-permission android:name="android.permission.BLUETOOTH" />
  ...
</manifest>

四、Setting Up Bluetooth
在用Bluetooth通讯之前,需要确认设备是否支持Bluetooth,如果支持,还得确保Bluetooth是可用的。
如果设备不支持Bluetooth,需要优雅的将Bluetooth置为不可用。如果支持Bluetooth,但没有开启,可以在应用中请求开启Bluetooth。该设置使用BluetoothAdapter.通过两个步骤完成。
1、获取BluetoothAdapter
BluetoothAdapter是每个Bluetooth的Activity都需要用到的。用静态方法getDefaultAdapter()获取 BluetoothAdapter,返回一个拥有Bluetooth 适配器的BluetoothAdapter对象。如果返回null,说明设备不支持Bluetooth,关于Bluetooth的故事到此就结束了(因为你干不了什么了)。示例:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}
2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值