Android蓝牙协议栈
Android蓝牙协议栈使用的是BlueZ,支持GAP, SDP, and RFCOMM规范,是一个SIG认证的蓝牙协议栈。
Bluez 是GPL许可的,因此Android的框架内与用户空间的bluez代码通过D-BUS进程通讯进行交互,以避免专有代码。
Headset和Handsfree(v1.5)规范就在Android框架中实现的,它是跟Phone App紧密耦合的。这些规范也是SIG认证的。
 
下面的图表提供了一个以库为导向的蓝牙栈视图。
实线框的是Android模块,红虚线部分为合作伙伴指定模块(译者注:芯片商提供)。
 
下面的图表是以进程为导向视图:
 
移植
BlueZ是兼容蓝牙2.1的,可以工作在任何2.1芯片以及向后兼容的旧的蓝牙版本。有要有两个方面:
串口驱动 UART driver
蓝牙电源开/关 Bluetooth Power On/Off
串口驱动
BlueZ核心子系统使用hciattach守护进程添加你的指定硬件串口驱动。
例如,MSM7201A,这个文件是在drivers/serial/msm_serial.c。你还需要通过修改为hciattach来编辑命令行选项。
蓝牙电源开/关
蓝牙芯片的电源开关方法1.0和Post 1.0是不同的,具体如下:
1.0:Android框架写0或1到/sys/modules/board_[PLATFORM]/parameters/bluetooth_power_on
Post 1.0:Android框架使用linux rfkill API,参考 arch/arm/mach-msm/board-trout-rfkill.c例子。
编译
编译Android打开蓝牙支持,添加下面这行内容到BoardConfig.mk。
    BOARD_HAVE_BLUETOOTH :=true
 
解决问题
调试
调试你的蓝牙实现,可以通过读跟蓝牙相关的logs(adb logcat)和查ERROR和警告消息。Android使用Bluez,同时会带来一些有用的调式工具。下面的片段为了提供一个建议的例子: 
hciconfig -a             
# print BT chipset address and features. Useful to check if you can communicate with your BT chipset. 
hcidump -XVt             
# print live HCI UART traffic. 
hcitool scan             
# scan for local devices. Useful to check if RX/TX works. 
l2ping ADDRESS         
# ping another BT device. Useful to check if RX/TX works. 
sdptool records ADDRESS # request the SDP records of another BT device.   
 
守护进程日志
hcid(STDOUT)和hciattach(STDERR)的守护进程日志缺省是被写到/dev/null。编辑和在logwrapper下运行这些守护进程,把它们输出到logcat。
 
hciconfig -a 和 hcitool
如果你编译你自己的system.img,除了hcitool扫描不行,hciconfig -a是可以工作的,尝试安装固件到蓝牙芯片。XXX TBD
 
工具
BlueZ为调试和与蓝牙子系统通信提供很多设置命令行工具,包含下面这些:
hciconfig
hcitool
hcidump
sdptool
dbus-send
dbus-monitor
 
特性支持
这一部分提供在每个android版本中的蓝牙的一些变化,主要是一些性能参数上的东西,就不翻译了。
Android 1.0 release
Platform features
Based on Bluez 3.36 and Linux Kernel 2.6.25
Bluetooth 2.0+EDR host stack
Headset Profile 1.0 in Audio Gateway role
Handsfree Profile 1.5 in Audio Gateway role
o Three-way calling
o Phonebook over AT commands
Qualifications
QDID B014524: Host stack (SDP, L2CAP, GAP, RFCOMM, SPP)
QDID B014624: EPL for HTC Dream (HSP, HFP)
Example products
HTC Dream / T-Mobile G1
 
Android 1.1 release
No Bluetooth changes since 1.0
 
Android 1.5 release (cupcake)
Platform features
Based on Bluez 3.36 with Linux Kernel 2.6.27
Bluetooth 2.0+EDR host stack
o Support for auto-pairing with '0000' devices
Headset Profile 1.1 in Audio Gateway role
Handsfree Profile 1.5 in Audio Gateway role
o Three-way calling
o Phonebook over AT commands
o Volume synchronization
o eSCO
o Extensive bug fixes and compatibility improvements
Stereo Bluetooth (A2DP 1.2) in Source role
o AVDTP 1.2 in Acceptor and Initiator roles
o GAVDTP 1.0 in Acceptor and Initiator roles
o 44.1 khz, stereo, software SBC codec
Remote Control (AVRCP 1.0) in Target role
headseto AVCTP 1.3 in Target role
o play/pause/stop/prev/next
Qualifications
QDID B015261: Host stack (SDP, L2CAP, GAP, RFCOMM, SPP, AVCTP, AVRCP, GAVDP, AVDTP, A2DP)
QDID B015262: EPL for HTC Sapphire (HSP, HFP)
Android 2.0/2.1 release (eclair)
Platform features
Based on Bluez 4.47 with Linux Kernel 2.6.29
Bluetooth 2.1+EDR host stack
o Support for auto-pairing with '0000' devices
o Support for Simple Secure Pairing
Headset Profile 1.1 in Audio Gateway role
Handsfree Profile 1.5 in Audio Gateway role
o Three-way calling
o Phonebook over AT commands
o Volume synchronization
o eSCO
o Extensive bug fixes and compatibility improvements
Stereo Bluetooth (A2DP 1.2) in Source role
o AVDTP 1.2 in Acceptor and Initiator roles