Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9192870
  • 博文数量: 1728
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19870
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1728)

文章存档

2024年(4)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: LINUX

2012-07-19 18:48:57

到网上摘抄的,来自 

 蓝牙设置种常用的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 init
MACHINE_START 
init_machine = comet_init
bt_power_init
bluetooth_power
2. HCI device/connection manager, socket init
bt_init()
bt_sysfs_init
sock_register
hci_sock_init
3. HCI UART init( UART LINE DISCIPLINE)
hci_uart_init(open/close/read/write/ioctl,poll...)
h4_init
hci_uart_register_proto
4. Bluetooth Sleep Module,/proc/bluetooth
bluesleep_init()
platform_driver_register(&bluesleep_driver)
5. BT Power switch, RF kill
bluetooth_power_init()
bt_power_probe
6. L2CAP init
l2cap_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 init
sco_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 Init
rfcomm_init()
hci_register_cb(&rfcomm_cb)
kthread_run(rfcomm_run, NULL, "krfcommd")
class_create_file(bt_class, &class_attr_rfcomm_dlc)
rfcomm_init_sockets
rfcomm_init_ttys()
9. BNEP Init
bnep_init()
bnep_sock_init()
10. thread running
rfcomm_run
rfcomm_l2sock_create
l2cap_sock_create: sock 
l2cap_sock_init: sk 
l2cap_sock_bind: sk 
l2cap_sock_listen: sk 
rfcomm_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_up
ioctl(ctl, HCIDEVUP, hdev)
==========kernel space================
hci_sock_ioctl
hci_dev_open
hci_dev_get
hdev->open [ hci_uart_open(hci_ldisc) ]
_hci_request(hdev, hci_init_req, 0,msecs_to_jiffies(HCI_INIT_TIMEOUT ))
hci_init_req
hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES , 0, NULL);
......................
===========CMD Flow============================
hci_cmd_task: hci0 cmd 1
hci_send_frame: hci0 type 1 len 3
hci_sock_dev_event: hdev hci0 event 7
hci_send_to_sock: hdev (null) len 8
hci_uart_send_frame: hci0: type 1 len 3
h4_enqueue: hu c658f6c0 skb c662d340
hci_uart_tx_wakeup:
hci_uart_tty_wakeup:
hci_uart_tx_wakeup:
===========Event Flow===========================
h4_recv: hu c658f6c0 count 15 rx_state 0 rx_count 0
h4_recv: Event packet
h4_recv: Event header: evt 0x0e plen 12
h4_check_data_len: len 12 room 1046
h4_recv: Complete data
hci_rx_task: hci0
hci_cc_read_local_features: hci0 status 0x0
hci_cc_read_local_features: hci0 features 0xfffe8ffe9bff598
========================================BT TOOLS=======================================
hciattatch
hciconfig
hciconfig - HCI device configuration utility
Usage:
hciconfig
hciconfig [-a] hciX [command]
Commands:
up Open and initialize HCI device
down Close HCI device
reset Reset HCI device
rstat Reset statistic counters
auth Enable Authentication
noauth Disable Authentication
encrypt Enable Encryption
noencrypt Disable Encryption
piscan Enable Page and Inquiry scan
noscan Disable scan
iscan Enable Inquiry scan
pscan Enable Page scan
ptype [type] Get/Set default packet type
lm [mode] Get/Set default link mode
lp [policy] Get/Set default link policy
name [name] Get/Set local name
class [class] Get/Set class of device
voice [voice] Get/Set voice setting
iac [iac] Get/Set inquiry access code
inqtpl [level] Get/Set inquiry transmit power level
inqmode [mode] Get/Set inquiry mode
inqdata [data] Get/Set inquiry data
inqtype [type] Get/Set inquiry scan type
inqparms [win:int] Get/Set inquiry scan window and interval
pageparms [win:int] Get/Set page scan window and interval
pageto [to] Get/Set page timeout
afhmode [mode] Get/Set AFH mode
sspmode [mode] Get/Set Simple Pairing Mode
aclmtu Set ACL MTU and number of packets
scomtu Set SCO MTU and number of packets
putkey Store link key on the device
delkey Delete link key from the device
oobdata Display local OOB data
commands Display supported commands
features Display device features
version Display version information
revision Display revision information
bttest
hcitool cmd
hcitool scan
hcidump
hcidump -B -w /data/test.cfa [Log HCI packets as FTS btsnoopformat]
hcidump -XVt [Print HCI packets that can be readable with timestamps and payloads]
sdptool
sdptool 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.rc
3. init.qcom.rc
4. init.qcom.bt.sh

阅读(5154) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~