Android bluetooth introduction part I

本文详细介绍了Android设备中蓝牙的工作原理及架构。从底层的BlueZ内核模块到上层的应用程序,包括蓝牙服务的初始化过程、如何通过dbus-daemon进行进程间通信、以及如何扫描附近的蓝牙设备等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here is a very detailed image from Android website and it tells us the whole architecture about bluetooth in Android. I tried to understand how bluetooth works in Android Dev phone and what BT services we can use.

I. related source code

* bluez 3.36 (both userspace & kernel)
- /mydroid/externel/bluez
- /mydroid/kernel/drivers/bluetooth
- /mydroid/kernel/net/bluetooth

* framework (java & c++)
- /mydroid/frameworks/base/core/jni/android_bluetooth_*.cpp (connect with hcid via dbus)
- /mydroid/frameworks/base/core/java/android/bluetooth/*.java
- /mydroid/frameworks/base/services/java/com/android/server/ (SystemServer)

* UI application (java)
- /mydroid/packages/apps/Phone/src/com/android/phone/ (Phone App)
- /mydroid/packages/apps/Settings/src/com/android/settings/bluetooth/ (Settings App)


II. Init bluetooth

* init script from /root/init.rc

  1. create this directory '/data/misc/hcid' to store paired BT device information
  2. launch dbus-daemon (deal connections between hcid and system server)
  3. create hcid (Bluetooth Host Controller Interface Daemon) service, but disabled at first
  4. create hfag service, it's for bluetooth Hands-Free Profile, but disabled at first, and only 'one shot' (it means not restart this service when it exits). Also it would register a SDP service and channel is 10.
  5. create hsag service, it's for bluetooth Headset Profile, but disabled at first, and only 'one shot' (it means not restart this service when it exits). Also it would register a SDP service and channel is 11.
  6. create dun service, it's for bluetooth Dial-up Networking Profile, but disabled at first, and only 'one shot' (it means not restart this service when it exits). Also it would register a SDP service and channel is 2.

 


# create basic filesystem structure
mkdir /data/misc/hcid 0770 bluetooth bluetooth

service dbus /system/bin/dbus-daemon --system --nofork
socket dbus stream 660 bluetooth bluetooth
t;user bluetooth
group bluetooth net_bt_admin

service hcid /system/bin/hcid -s -n -f /etc/bluez/hcid.conf
socket bluetooth stream 660 bluetooth bluetooth
socket dbus_bluetooth stream 660 bluetooth bluetooth
# init.rc does not yet support applying capabilities, so run as root and
# let hcid drop uid to bluetooth with the right linux capabilities
group bluetooth net_bt_admin misc
disabled

service hfag /system/bin/sdptool add --channel=10 HFAG
user bluetooth
group bluetooth net_bt_admin
disabled
oneshot

service dun /system/bin/sdptool add --channel=2 DUN
user bluetooth
group bluetooth net_bt_admin
disabled
oneshot

service hsag /system/bin/sdptool add --channel=11 HSAG
user bluetooth
group bluetooth net_bt_admin
disabled
oneshot


* init script from /root/init.trout.rc

  1. this script file is from /vendor/htc/dream folder and it's particular for this device.
  2. create hciattach (attach serial devices via UART HCI to BlueZ stack) service, but disabled at first.
  3. specify UART speed is 115200, serial device is /dev/ttyHS0, id/type is texas.

 


service hciattach /system/bin/hciattach /
-n -s 115200 /dev/ttyHS0 texas 4000000 flow
user bluetooth
group bluetooth net_bt_admin
disabled


* system server: it would decide whether it should turn bluetooth ON or OFF from settings value. If the value is ON, then it would start hciattach and hcid service. That is why the init scripts set them disabled at first. We can decide BT power from UI application.


III. Connect with other BT devices

  • bluez: it provides 'hcid' daemon and it's responsible for all related bluetooth services
  • dbus-daemon: the bridge between hcid and system server
  • D-Bus is a simple inter-process communication (IPC) system for software applications to communicate with one another.
  • debug utility: d-feet (a dbus UI debugger ), dbus-monitor (debug probe to print message bus messages), dbus-send (Send a message to a message bus)
  • bluez dbus API document: /mydroid/external/bluez/utils/hcid/dbus-api.txt
  • one example as below



* Scan nearby BT devices in Android

In system server, it would send a method 'DiscoverDevices' to bluez via dbus-daemon. This method starts the device discovery procedure. This includes an inquiry procedure and remote device name resolving. On start up this process will generate a DiscoveryStarted signal and then return DeviceFound singals. If the procedure has been finished an DiscoveryCompleted signal will be sent.



/mydroid/frameworks/base/core/jni/android_server_BluetoothDeviceService.cpp

 
/* Compose the command */ 
msg
= dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC, nat->adapter, DBUS_CLASS_NAME, "DiscoverDevices"); 
/* Send the command. */ 
reply
= dbus_connection_send_with_reply_and_block(nat->conn, msg, -1, &err); 


log from ddms

 
06-18 14:42:14.567: INFO/BluetoothEventLoop.cpp(67): DiscoveryStarted signal received 
06-18 14:42:14.584: VERBOSE/BluetoothEventRedirector(361): Received android.bluetooth.intent.action.DISCOVERY_STARTED 
06-18 14:42:14.834: VERBOSE/BluetoothEventRedirector(361): Received android.bluetooth.intent.action.REMOTE_DEVICE_FOUND 
06-18 14:42:35.034: INFO/BluetoothEventLoop.cpp(67): DiscoveryCompleted signal received 

http://i-miss-erin.blogspot.com/2009/06/android-bluetooth-introduction-part-i.html

http://source.android.com/porting/bluetooth.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值