http://www.oschina.net/question/158589_55569android
如下是我本身關於這個問題的詳細分析過程:app
問題:bluetooth必須在wifi開啓後才能正常開啓。socket
目標:bluetooth的開啓不受wifi影響。函數
現象:oop
先打開wifi,再打開bluetooth。ui
I/wpa_supplicant( 2748): CTRL-EVENT-STATE-CHANGE id=-1 state=1this
V/WifiMonitor( 2359): Event [CTRL-EVENT-STATE-CHANGE id=-1 state=1]spa
V/WifiStateTracker( 2359): Changing supplicant state: INACTIVE ==> INACTIVE.net
D/BluetoothService( 2359): Bluetooth state 10 -> 11orm
V/BluetoothEventRedirector( 2533): Received android.bluetooth.adapter.action.STATE_CHANGED
init: no such service 'hciattach'
I/bluedroid( 2359): Starting hciattach daemon
I/bluedroid( 2359): Starting bluetoothd deamon
E/BluetoothEventLoop.cpp( 2359): event_filter: Received signal org.freedesktop.DBus:NameAcquired from /org/freedesktop/DBus
D/BluetoothService( 2359): found 1 bonded devices
I/BluetoothDeviceProfileState( 2359): Entering ACL Connected state with: -1
D/BluetoothService( 2359): Bluetooth state 11 -> 12
V/BluetoothEventRedirector( 2533): Received android.bluetooth.adapter.action.STATE_CHANGED
D/BluetoothService( 2359): updateDeviceServiceChannelCache(E4:B0:21:58:18:53)
I/bluetooth_ScoSocket.cpp( 2439): Listening SCO socket...
D/BluetoothService( 2359): uuid(system): 00001105-0000-1000-8000-00805f9b34fb 3
I/BtOppRfcommListener( 2571): Accept thread started on channel 12
D/BluetoothService( 2359): Registering hfag record
打開bluetooth,不打開wifi
D/BluetoothService( 2359): Bluetooth state 10 -> 11
V/BluetoothEventRedirector( 2533): Received android.bluetooth.adapter.action.STATE_CHANGED
init: no such service 'hciattach'
I/bluedroid( 2359): Starting hciattach daemon
D/dalvikvm( 2571): GC_EXPLICIT freed 194K, 49% free 2802K/5447K, external 716K/1038K, paused 104ms
打開bluetooth,而後打開wifi
D/BluetoothService( 2359): Bluetooth state 10 -> 11
V/BluetoothEventRedirector( 2533): Received android.bluetooth.adapter.action.STATE_CHANGED
init: no such service 'hciattach'
I/bluedroid( 2359): Starting hciattach daemon
D/dalvikvm( 2571): GC_EXPLICIT freed 120K, 49% free 2782K/5447K, external 716K/1038K, paused 89ms
WLAN FW already running! Skip FW download
WLAN FW is active
I/bluedroid( 2359): Starting bluetoothd deamon
W/Netd ( 2215): action=1, iface=mlan0
D/Tethering( 2359): mlan0 is not a tetherable iface, ignoring
D/NetdConnector( 2359): RCV <- {600 Iface added mlan0}
W/Netd ( 2215): action=5, iface=mlan0
D/Tethering( 2359): interfaceLinkStatusChanged mlan0, false
D/NetdConnector( 2359): RCV <- {600 Iface linkstate mlan0 down}
W/Netd ( 2215): action=1, iface=uap0
D/Tethering( 2359): uap0 is not a tetherable iface, ignoring
W/Netd ( 2215): action=5, iface=uap0
D/Tethering( 2359): interfaceLinkStatusChanged uap0, false
D/NetdConnector( 2359): RCV <- {600 Iface added uap0}
D/NetdConnector( 2359): RCV <- {600 Iface linkstate uap0 down}
I/BluetoothEventLoop.cpp( 2359): agent_event_filter: Received method org.bluez.Agent:Release
D/BluetoothService( 2359): found 1 bonded devices
D/BluetoothService( 2359): Bluetooth state 11 -> 12
V/BluetoothEventRedirector( 2533): Received android.bluetooth.adapter.action.STATE_CHANGED
I/bluetooth_ScoSocket.cpp( 2439): Listening SCO socket...
分析:
定位關鍵代碼
if (set_bluetooth_power(1) < 0) goto out;
LOGI("Starting hciattach daemon");
if (property_set("ctl.start", "hciattach") < 0) {
LOGE("Failed to start hciattach");
goto out;
}
// Try for 10 seconds, this can only succeed once hciattach has sent the
// firmware and then turned on hci device via HCIUARTSETPROTO ioctl
for (attempt = 1000; attempt > 0; attempt--) {
hci_sock = create_hci_sock();
if (hci_sock < 0) goto out;
if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
break;
}
close(hci_sock);
usleep(10000); // 10 ms retry delay
}
if (attempt == 0) {
LOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
goto out;
}
LOGI("Starting bluetoothd deamon");
if (property_set("ctl.start", "bluetoothd") < 0) {
LOGE("Failed to start bluetoothd");
goto out;
}
在標紅的代碼兩端添加打印,看是不是該段代碼阻塞了藍牙的打開。
I/bluedroid( 2385): Starting hciattach daemon
I/bluedroid( 2385): <<James.C>> --- Turning on HCI device
D/dalvikvm( 2583): GC_EXPLICIT freed 170K, 49% free 2800K/5447K, external 716K/1038K, paused 89ms
D/dalvikvm( 2385): GC_EXPLICIT freed 434K, 38% free 4808K/7751K, external 1867K/3065
並無LOGI("<<James.C>> --- End of Turning on HCI device: attemp = %d", attempt);的打印。
=》多是卡死在for循環了。
=》多是直接走out了。
須要進行確認。
在for循環中添加打印:
LOGI("<<James.C>> --- Turning on HCI device");
for (attempt = 1000; attempt > 0; attempt--) {
hci_sock = create_hci_sock();
LOGI("<<James.C>> --- hci_sock = %d, attempt = %d", hci_sock, attempt);
if (hci_sock < 0) goto out;
if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
break;
}
close(hci_sock);
usleep(10000); // 10 ms retry delay
}
LOGI("<<James.C>> --- End of Turning on HCI device: attemp = %d", attempt);
if (attempt == 0) {
LOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
goto out;
}
一直在循環。
經過for循環前的註釋,初步定位爲hciattach服務的問題。Grep關鍵詞,主要是在如下文件中:
./device/fsl/imx51_3stack/mxc_bt.sh:/system/bin/hciattach -n -s 921600 /dev/ttymxc1 bcsp 921600;
./device/fsl/imx51_3stack/init.rc:service hciattach /system/bin/logwrapper /system/bin/sh /system/etc/bluez/mxc_bt.sh
經確認,不是這個緣由,由於fsl目錄下的東西沒有被執行。
HCI device沒法經過ioctl打開
=》ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)失敗了
=》查看hci socket的ioctl
static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
case HCIDEVUP:
if (!capable(CAP_NET_ADMIN))
return -EACCES;
return hci_dev_open(arg);
}
其實就是權限檢查+設備打開。
增長打印
case HCIDEVUP:
if (!capable(CAP_NET_ADMIN))
return -EACCES;
printk("<<James.C>> --- hci_dev_open(arg) \n");
ret = hci_dev_open(arg);
printk("<<James.C>> --- ret = %d \n", ret);
return ret;
目標:經過打印看是權限不對仍是打開hci設備出錯。
<<James.C>> --- ret = -110
<<James.C>> --- hci_dev_open(arg)
I/bluedroid( 2386): <<James.C>> --- hci_sock = 164, attempt = 997
#define ETIMEDOUT 110 /* Connection timed out */
因而問題就定位爲HCI device作request時產生了timeout的錯誤。
int hci_dev_open(__u16 dev)
{
ret = __hci_request(hdev, hci_init_req, 0,
msecs_to_jiffies(HCI_INIT_TIMEOUT));//10s timeout
}
執行hci_init_req(hdev, 0)產生了超時
=》對hci device進行考察,找出超時緣由。(android hci; Linux hci; hci device;)
=》wifi開啓時,哪些東西會影響hci device?
猜測:有多是固件加載的問題。打開藍牙時沒有對固件進行加載。
已經排除此可能。
猜測:是否是nh387尚未上電?
Nh387已經成功初始化。
猜測:HCI daemon沒有啓動,或者確實文件。
方法:瞭解HCI通訊機制及其須要的文件。
在bt_main.c中有以下函數:
/**
* @brief This function send module cfg cmd to firmware
*
* Command format:
* +--------+--------+--------+--------+--------+--------+--------+
* | OCF OGF | Length | Data |
* +--------+--------+--------+--------+--------+--------+--------+
* | 2-byte | 1-byte | 4-byte |
* +--------+--------+--------+--------+--------+--------+--------+
*
* @param priv A pointer to bt_private structure
* @param subcmd sub command
* @return BT_STATUS_SUCCESS or BT_STATUS_FAILURE
*/
int
bt_send_module_cfg_cmd(bt_private * priv, int subcmd)
它在執行以下語句時失敗了
ret = bt_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
if (ret < 0) {
PRINTM(FATAL, "Module cfg command send failed!\n");
goto err_init_fw;
}
這就頗有多是問題所在。提升PRINTM打印級別,看bt模塊是否正確的初始化其firmware。
Firmware初始化正常了,可是bringup module這個request卻執行失敗。
bt_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);這句話會如何執行?
用舊的驅動和firmware:
加載藍牙驅動時,沒有timeout的錯誤,也就是說module_bringup_req執行成功了。可是,依然是不能打開藍牙,現象和新的藍牙驅動同樣。
另外,使用舊的wifi驅動,系統啓動後,」setting」 -> 「open wifi」會致使down機。
還原SDIO驅動
ok。。。。。。。。我擦!