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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 入手评测:i9 10900k和i9 11900k区别 i9109
  2. rtx3080ti功耗 rtx3080ti价格
  3. 解决go get下载包失败问题
  4. i7 10700k和10700kf哪个好 i7 10700k和10
  5. Cookie和Session
  6. 10700kf和10700f差距有多大 10700kf和107
  7. Go语言基础之流程控制
  8. Vue(7):vue项目部署到服务器通过公网IP访
  9. LeetCode go
  10. 轮播图、懒加载