android 启动蓝牙设备,Android 蓝牙启动过程

到网上摘抄的,来自

蓝牙设置种常用的Intent,下面是在bluetoothsettings.java 中注册蓝牙Intent的函数:

private boolean initBluetoothAPI() {

mIntentFilter =

//  跟远端蓝牙设备连接上时返回来的intent

new IntentFilter(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION);

//  跟远端蓝牙设备断开时返回来的intent mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION);

//  跟远端的蓝牙设备配对上时收到的intent,不过前提是对方主动发起的配对

//  才能收到这个intent

mIntentFilter.addAction(BluetoothIntent.BONDING_CREATED_ACTION);

//  本地蓝牙设备可用时收到的Intent

mIntentFilter.addAction(BluetoothIntent.ENABLED_ACTION);

//  本地蓝牙设备不可用时收到的Intent

mIntentFilter.addAction(BluetoothIntent.DISABLED_ACTION);

//  扫描到远端设备时收到的intent   mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION);

//   远端蓝牙设备消失时收到的intent        mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION);

//   远端蓝牙设备名称更换时收到的intent,因为刚发现设备的时候还没有获取//    它的名称

mIntentFilter.addAction(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION);

//  当有远端设备发起配对请求时收到的intent

mIntentFilter.addAction(BluetoothIntent.PAIRING_REQUEST_ACTION);

//  蓝牙耳机状态改变时候到的intent

mIntentFilter.addAction(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION);

// 扫描设备结束

mIntentFilter.addAction(BluetoothIntent.DISCOVERY_COMPLETED_ACTION);

//  扫描开始

mIntentFilter.addAction(BluetoothIntent.DISCOVERY_STARTED_ACTION);

//  蓝牙设备模式改变,表示本地蓝牙设备是否可以被查找

mIntentFilter.addAction(BluetoothIntent.MODE_CHANGED_ACTION);

//   有耳机插入

mIntentFilter.addAction(Intent.ACTION_HEADSET_PLUG);

启动蓝牙

在启动蓝牙的时候,要注意的地方是不能正常启动蓝牙的情况,因为正常启动的时候会返回BluetoothIntent.ENABLED_ACTION 这个Intent,当时当启动出现异常的时候是没有Intent返回的,android使用回调函数来解决这个问题。下面是在bluetoothdeviceservice.java 里面enable((IBluetoothDeviceCallback callback) 的过程:

public synchronized boolean enable(IBluetoothDeviceCallback callback) {

checkPermissionBluetoothAdmin();

Log.d(TAG,"start enable! ");

// Airplane mode can prevent Bluetooth radio from being turned on.

if (mIsAirplaneSensitive && isAirplaneModeOn()) {

return false;

}

if (mIsEnabled) {

return false;

}

if (mEnableThread != null && mEnableThread.isAlive()) {

return false;

}

//   主要的启动过程是放在一个新起的线程里面,但是不管能不能启动

//    仍然返回了true

mEnableThread = new EnableThread(callback);

mEnableThread.start();

//

return true;

}

private EnableThread mEnableThread;

private class EnableThread extends Thread {

private final IBluetoothDeviceCallback mEnableCallback;

public EnableThread(IBluetoothDeviceCallback callback) {

mEnableCallback = callback;

}

public void run() {

boolean res = enableNative();

if (res) {

mEventLoop.start();

}

if (mEnableCallback != null) {

try {

// 通过回调函数来表明是否正常启动蓝牙设备

mEnableCallback.onEnableResult(res ?

BluetoothDevice.RESULT_SUCCESS :

BluetoothDevice.RESULT_FAILURE);

} catch (RemoteException e) {}

}

if (res) {

mIsEnabled = true;

mIsDiscovering = false;

Intent intent = new Intent(BluetoothIntent.ENABLED_ACTION);

mContext.sendBroadcast(intent);

}

}else{

mIsEnabled = false;

mIsDiscovering = false;

}

mEnableThread = null;

}

}

//  这个回调函数将被作为参数传进bluetoothservice 里面的enable(IBluetoothDeviceCallback callback)

static class DeviceCallback extends IBluetoothDeviceCallback.Stub {

Handler messageHandler;

public void setHandler(Handler handler) {

synchronized (this) {

messageHandler = handler;

}

public void onEnableResult(int result) {

switch(result) {

//  启动不成功的时候执行

case BluetoothDevice.RESULT_FAILURE:

messageHandler.sendMessage(messageHandler.obtainMessage(EVENT_FAILED_BT_ENABLE,0));

break;

}

}

//   配对完成时执行

public void onCreateBondingResult(String address, int result) {

synchronized (this) {

if (messageHandler != null) {

if (result == BluetoothDevice.RESULT_FAILURE) {

messageHandler.sendMessage(messageHandler.obtainMessage(

HANDLE_PAIRING_FAILED, address));

} else {

messageHandler.sendMessage(messageHandler.obtainMessage(

HANDLE_PAIRING_PASSED, address));

}

}

}

}

};

关闭过程

public synchronized boolean disable() {

checkPermissionBluetoothAdmin();

if (mEnableThread != null && mEnableThread.isAlive()) {

return false;

}

if (!mIsEnabled) {

return true;

}

if(!disableNative()){

Log.d(TAG,"disableNative false ");

return false;

}

mEventLoop.stop();

mIsEnabled = false;

mIsDiscovering = false;

Intent intent = new Intent(BluetoothIntent.DISABLED_ACTION);

mContext.sendBroadcast(intent);

return true;

}

配对过程

private void doPair(Preference pref, String address) {

pref.setEnabled(false);

pref.setSummary(STR_PAIRING);

if (mPinEdit != null){

String strPIN = mPinEdit.getText().toString();

mBluetooth.writePinCode(address, strPIN);

mBluetooth.createBonding(address, sDeviceCallback);

}

}

android启动蓝牙的过程

=========================Kernel Space=========================1. Board power initMACHINE_STARTinit_machine = comet_initbt_power_initbluetooth_power2. HCI device/connection manager, socket initbt_init()bt_sysfs_initsock_registerhci_sock_init3. HCI UART init( UART LINE DISCIPLINE)hci_uart_init(open/close/read/write/ioctl,poll...)h4_inithci_uart_register_proto4. Bluetooth Sleep Module,/proc/bluetoothbluesleep_init()platform_driver_register(&bluesleep_driver)5. BT Power switch, RF killbluetooth_power_init()bt_power_probe6. L2CAP initl2cap_init()proto_register(&l2cap_proto, 0)bt_sock_register(BTPROTO_L2CAP , &l2cap_sock_family_ops)hci_register_proto(&l2cap_hci_proto)class_create_file(bt_class, &class_attr_l2cap)7. SCO initsco_init()proto_register(&sco_proto, 0)bt_sock_register(BTPROTO_SCO , &sco_sock_family_ops)hci_register_proto(&sco_hci_proto)class_create_file(bt_class, &class_attr_sco)8. RFCOMM Initrfcomm_init()hci_register_cb(&rfcomm_cb)kthread_run(rfcomm_run, NULL, "krfcommd")class_create_file(bt_class, &class_attr_rfcomm_dlc)rfcomm_init_socketsrfcomm_init_ttys()9. BNEP Initbnep_init()bnep_sock_init()10. thread runningrfcomm_runrfcomm_l2sock_createl2cap_sock_create: sockl2cap_sock_init: skl2cap_sock_bind: skl2cap_sock_listen: skrfcomm_session_add: session=========================命令行方式启动BT==============================1.echo 1 > /sys/class/rfkill/rfkill0/state*Bluetooth power switch: 1[bluetooth_power(), board_qsc8x50.c]2. hci_qcomm_init -vvv -e*hci_qcomm_init-d /dev/ttyHS0 -s 3200000 -i 115200 -r 19200000 (open HSUART COM port and initializeBTS402x )3. hciattach /dev/ttyHS0 qualcomm 3000000 (HCI LINE DISCIPLINE)hciattach/dev/ttyHS0 any 200 flow (attach serial device via UART HCI to BlueZstack )=============user space===================main()init_uart()ioctl(fd, HCIUARTSETPROTO, u->proto)==============kernel space=================hci_uart_tty_ioctl()hci_uart_set_proto()open()[h4_open()]hci_uart_register_dev()hci_register_dev()hci_register_sysfs()4. hciconfig hci0 up(open and initialize HCI device) [hdev->name == hci0]==========user space====================ctl=socket(AF_BLUETOOTH , SOCK_RAW , BTPROTO_HCI ))ioctl(ctl, HCIGETDEVINFO, (void *) &di)hci_open_dev(di.dev_id)->bind(dd, (struct sockaddr *) &a, sizeof(a))cmd_upioctl(ctl, HCIDEVUP, hdev)==========kernel space================hci_sock_ioctlhci_dev_openhci_dev_gethdev->open [ hci_uart_open(hci_ldisc) ]_hci_request(hdev, hci_init_req, 0,msecs_to_jiffies(HCI_INIT_TIMEOUT ))hci_init_reqhci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES , 0, NULL);......................===========CMD Flow============================hci_cmd_task: hci0 cmd 1hci_send_frame: hci0 type 1 len 3hci_sock_dev_event: hdev hci0 event 7hci_send_to_sock: hdev (null) len 8hci_uart_send_frame: hci0: type 1 len 3h4_enqueue: hu c658f6c0 skb c662d340hci_uart_tx_wakeup:hci_uart_tty_wakeup:hci_uart_tx_wakeup:===========Event Flow===========================h4_recv: hu c658f6c0 count 15 rx_state 0 rx_count 0h4_recv: Event packeth4_recv: Event header: evt 0x0e plen 12h4_check_data_len: len 12 room 1046h4_recv: Complete datahci_rx_task: hci0hci_cc_read_local_features: hci0 status 0x0hci_cc_read_local_features: hci0 features 0xfffe8ffe9bff598========================================BT TOOLS=======================================hciattatchhciconfighciconfig - HCI device configuration utilityUsage:hciconfighciconfig [-a] hciX [command]Commands:up Open and initialize HCI devicedown Close HCI devicereset Reset HCI devicerstat Reset statistic countersauth Enable Authenticationnoauth Disable Authenticationencrypt Enable Encryptionnoencrypt Disable Encryptionpiscan Enable Page and Inquiry scannoscan Disable scaniscan Enable Inquiry scanpscan Enable Page scanptype [type] Get/Set default packet typelm [mode] Get/Set default link modelp [policy] Get/Set default link policyname [name] Get/Set local nameclass [class] Get/Set class of devicevoice [voice] Get/Set voice settingiac [iac] Get/Set inquiry access codeinqtpl [level] Get/Set inquiry transmit power levelinqmode [mode] Get/Set inquiry modeinqdata [data] Get/Set inquiry datainqtype [type] Get/Set inquiry scan typeinqparms [win:int] Get/Set inquiry scan window and intervalpageparms [win:int] Get/Set page scan window and intervalpageto [to] Get/Set page timeoutafhmode [mode] Get/Set AFH modesspmode [mode] Get/Set Simple Pairing Modeaclmtu Set ACL MTU and number of packetsscomtu Set SCO MTU and number of packetsputkey Store link key on the devicedelkey Delete link key from the deviceoobdata Display local OOB datacommands Display supported commandsfeatures Display device featuresversion Display version informationrevision Display revision informationbttesthcitool cmdhcitool scanhcidumphcidump -B -w /data/test.cfa [Log HCI packets as FTS btsnoopformat]hcidump -XVt [Print HCI packets that can be readable with timestamps and payloads]sdptoolsdptool add -channel=10 HFAG (add HFAG service records to be supported)sdptool del [record_handle] (remove service from local SDP)sdptool browse local [Browse all local service records]l2ping [Run L2CAPping-to-peer device]========================================android 脚本执行过程===============================2. init.rc3. init.qcom.rc4. init.qcom.bt.sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值