packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothSettings.java
onCreateOptionsMenu(Menu menu, MenuInflater inflater)
创建菜单,里面有MENU_ID_SCAN,MENU_ID_RENAME_DEVICE,MENU_ID_VISIBILITY_TIMEOUT,MENU_ID_SHOW_RECEIVED
onOptionsItemSelected(MenuItem item)
菜单对应的执行操作
(1)Bluetoothd daemon
external/bluetooth/bluez/src/main.c
main
|——init_defaults 填充main_opts
|——g_option_context_new 下面四步是参数解析相关
|——g_option_context_add_main_entries
|——g_option_context_parse
|——g_option_context_free
|——daemon(0, 0) 参数为0,则将输出都定向到/dev/null
|——__btd_log_init 打印一些log
|——agent_init
|——connection = dbus_bus_get (DBUS_BUS_SYSTEM, NULL)
|——connect_dbus()
|——g_dbus_setup_bus 注册dbus连接,并重命名为"org.bluez"
|——manager_init(conn, "/") 设置base_path
|——g_dbus_register_interface 注册interface,包括medhod,signal
|——set_dbus_connection(conn) 设置connection为conn
|——start_sdp_server 初始化sdp协议
|——init_server
|——创建l2cap_sock套接字
|——bind l2cap_sock
|——listen l2cap_sock
|——listen unix_sock
|——io = g_io_channel_unix_new(l2cap_sock)
|——g_io_add_watch(io,, io_accept_event, &l2cap_sock) 等待对方connect
|——accept l2cap_sock/unix_sock操作
|——plugin_init
|——执行plugin init
|——g_main_loop_new
|——adapter_ops_setup
|——ops->setup()
|——rfkill_init 电源操作
|——g_main_loop_run
/********************** main结束 ************************/
hciops_init
|——btd_register_adapter_ops(&hci_ops, FALSE);
|——g_slist设置,将ops保存
hciops_setup
|——socket(,SOCK_RAW, BTPROTO_HCI)
|——hci_filter_set_ptype HCI_EVENT_PKT/EVT_STACK_INTERNAL设置过滤
|——setsockopt(,,HCI_FILTER, &flt,)
|——bind
|——g_io_add_watch(,,io_stack_event,)
|——g_idle_add(init_known_adapters,)
init_known_adapters
|——ioctl HCIGETDEVLIST
|——init_device
|——hci_open_dev
|——start_hci_dev
|——setsockopt HCI_FILTER设置event事件过滤
|——g_io_add_watch_full io_security_event 检测event事件
|——ioctl HCISETLINKMODE
|——ioctl HCIDEVUP
|——device_event HCI_DEV_UP
device_devup_setup
|——hci_send_cmd(,,READ_STORED_LINK_KEY_CP_SIZE,)
|——init_adapter(index)
|——btd_manager_register_adapter
|——adapter_create
|——g_dbus_register_interface 注册interface
|——adapter_init 读取bdaddr,name,features
|——g_dbus_emit_signal “AdapterAdded” to "org.bluez.Manager"
|——manager_update_adapters
|—— sned signal "PropertyChanged" value "Adapters"
|——manager_set_default_adapter
|——send signal “DefaultAdapterChanged”
|——start_adapter
|——set_event_mask
|——set DEFAULT_LINK_POLICY
|——btd_adapter_start
|——send signal "PropertyChanged" value "Powered"
(2)蓝牙服务启动
SystemServer
|―― bluetooth.initAfterA2dpRegistration();
|―― mEventLoop.getProfileProxy();
|―― mAdapter.getProfileProxy(, ,BluetoothProfile.A2DP);
|―― a2dp = new BluetoothA2dp(context, listener);
|―― mServiceListener.onServiceConnected(BluetoothProfile.A2DP,);
|―― mService = (BluetoothA2dp) proxy;
|―― mAdapter.getProfileProxy(, ,BluetoothProfile. INPUT_DEVICE);
|―― iDev = new BluetoothInputDevice(context, listener);
(3)enable过程
LocalBluetoothAdapter.enable()
|―― mAdapter.enable();
|―― mService.enable();
|―― mBluetoothState.sendMessage(USER_TURN_ON, saveSetting);
|―― broadcastState(BluetoothAdapter.STATE_TURNING_ON);
|―― prepareBluetooth();
|―― mBluetoothService.enableNative();
|―― bt_enable();
(4)scan过程
BluetoothSettings
|―― onPreferenceTreeClick KEY_BT_SCAN 点击scan
|―― mLocalAdapter.startScanning(true);
|―― a2dp.isA2dpPlaying() – return; 如果A2DP正在播放,则退出
|―― mAdapter.startDiscovery()
|―― mService.startDiscovery()
|―― startDiscoveryNative()
|―― dbus接口 -- StartDiscovery
|―― adapter_start_discovery bluez中接收到
|―― start_discovery(adapter);
|―― hciops_start_discovery
|―― hciops_start_inquiry
|―― hci_send_cmd(,OCF_INQUIRY, ,);
事件返回处理
Inquiry指令发出后,host会收到三种event事件回复
Command status Event、Inquiry result Event、Inquiry Complete event
io_security_event
|―― read(fd, buf, sizeof(buf));
|―― switch (eh->evt)事件类型查找
|——cmd_status case EVT_CMD_STATUS Discovering
|―― inquiry_complete_evt -- case EVT_INQUIRY_COMPLETE
|―― set_state(index, DISCOV_HALTED);
|―― inquiry_result -- case EVT_INQUIRY_RESULT
|―― btd_event_device_found
|―― adapter_update_found_devices
|―― adapter_emit_device_found
|―― emit_device_found
|―― dbus_message_new_signal(, , "DeviceFound");
|―― g_dbus_send_message(connection, signal);
JNI处的dbus接收到” DeviceFound” singal后,回调到JNI
BluetoothEventLoop. event_filter
|―― dbus_message_is_signal(,"org.bluez.Adapter", "DeviceFound")??
|―― parse_remote_device_properties(env, &iter)
|―― env->CallVoidMethod(,method_onDeviceFound, ,);
|―― onDeviceFound
|―― addDevice(address, properties);
|―― send intent BluetoothDevice.ACTION_FOUND
BluetoothEventManager.java 接收intent
DeviceFoundHandler
|―― mDeviceManager.addDevice -- cachedDevice
|―― dispatchDeviceAdded(cachedDevice);
回调,在UI上显示扫描到得设备
(5)SDP流程
搜索到某设备后,确定该设备所具有的profile
CachedBluetoothDevice
|―― fillData()
|―― fetchName();
|―― fetchBtClass();
|―― updateProfiles();
|―― mProfileManager.updateProfiles
add A2dpProfile HeadsetProfile
|——dispatchAttributesChanged();
回调,UI显示
(6)pair过程
BluetoothSettings
DeviceListPreferenceFragment
|―― onPreferenceTreeClick 点击除scan外的其它区域
|——onDevicePreferenceClick
|——BluetoothDevicePreference.onClicked()
|——pair() 配对
|——CachedBluetoothDevice. startPairing()
|——BluetoothDevice. createBond()
IBluetooth callback接口
|——BluetoothService. createBond
|——createPairedDeviceNative (address, 60000) 一分钟
|——dbus_func_args_async(, , , ,"CreatePairedDevice",)
|——dbus接口 CreatePairedDevice
|——create_paired_device bluez中处理
|——dbus_message_get_args 读取需要匹配的device信息
|——create_device_internal
|——adapter_create_device
|——dbus signal “DeviceCreated”
|——adapter_update_devices
|——dbus “PropertyChanged” “Devices”
|——device_create_bonding
|——adapter_create_bonding
|——hciops_create_bonding(BT_IO_L2RAW, , ,)
|——l2cap_connect 建立L2CAP连接
|——connect_add
|——bonding_request_new
|——mBondState.setBondState(,BOND_BONDING) 改变状态为bonding
|——new Intent ACTION_BOND_STATE_CHANGED
BluetoothEventManager.java 接收intent
|——BondStateChangedHandler
|——dispatchAttributesChanged();
UI上显示
JNI收到 “DeviceCreated” signal
onDeviceCreated
|——mBluetoothService.getRemoteDeviceProperties
|——addDevice(address, properties)
|——send intent “ACTION_FOUND”
JNI处的dbus接收到”RequestPairingConsent” singal后,回调到JNI
env->CallVoidMethod(, method_onRequestPairingConsent,,)
BluetoothEventLoop.java
|——onRequestPairingConsent
|——send intent ACTION_PAIRING_REQUEST
(7)connect过程
BluetoothDevicePreference.onClicked()
|——CachedBluetoothDevice. connect(true)
|——connectWithoutResettingTimer(connectAllProfiles); 连接所有profile
|——for (LocalBluetoothProfile profile : mProfiles) 遍历每个profile
|——connectInt (profile)
|——profile.connect (mDevice) A2DP HSP connect
(8)A2DP profile
A2dpProfile. connect()
|——BluetoothA2dp. connect()
|——BluetoothA2dpService. connect()
|——BluetoothService. connectSink
msg.arg1 = BluetoothDeviceProfileState.CONNECT_A2DP_OUTGOING;
|——BluetoothProfileState. sendMsg(msg)
|——BluetoothDeviceProfileState. sendMessage(cmd)
|——BluetoothA2dpService. connectSinkInternal
|——handleSinkStateChange STATE_CONNECTING
|——BluetoothService.sendConnectionStateChange
|——updateProfileConnectionState
|——connectSinkNative
|——dbus_func_args_async
("org.bluez.AudioSink", "Connect",)
Bluez org.bluez.AudioSink 收到Connect信号后
bluez/audio/device.c
dev_connect
|——sink_setup_stream
|——avdtp_discover
|——send_request(,,,AVDTP_DISCOVER,)
BluetoothA2dpService.cpp 收到dbus信号
onConnectSinkResult
回调到 BluetoothA2dpService.java
|——onConnectSinkResult