当前位置: 首页>移动开发>正文

Android 高版本BluetoothDevice bluetooth apk

l

 Android Bluetooth

Android 4.4上蓝牙协议栈采用的是BRCM和Google共同开发的bluedroid,代替了之前的Bluez.

一、 Bluetooth 源码分布 (基于Android 4.4 )

1.  packages/apps/Settings/src/com/android/settings/bluetooth

     bluetooth Settings 代码

2.  packages/apps/Bluetooth

     BT 应用层代码,及BT profile(如:A2dp,gatt,hdp,hfp,hid,map,opp,pan,pbap ...) 上层代码

     packages/apps/Bluetooth/jni

3.  frameworks/base/core/java/android/bluetooth

     framework 层相关 java 代码与aidl

4.  external/bluetooth/bluedroid      

     BRCM和Google共同开发的官方蓝牙协议栈

5.  linux/kernel/drivers/bluetooth

6.  linux/kernel/net/bluetooth

7. 以下是近期项目intel 平台

hardware/broadcom/libbt

hardware/libhardware

vendor/intel/fw/PRIVATE/bt    厂商bt固件

 二、Bluetooth 常用类及相关profile

A2dp: Advanced Audio Distribution Profile 蓝牙音频传输模型协定

 蓝牙立体声,和蓝牙耳机听歌有关那些,另还有个AVRCP--(Audio/Video Remote Control Profile)音频/视频远程控制配置文件,是用来听歌时暂停,上下歌曲选择的

GATT: Generic Attribute Profile   通用属性配置文件

       GATT是基于ATT Protocol的,ATT针对BLE设备做了专门的优化,具体就是在传输过程中使用尽量少的数据。每个属性都有一个唯一的UUID,属性将以characteristics and services的形式传输

       https://developer.bluetooth.org/TechnologyOverview/Pages/GATT.aspx

HDP:Bluetooth Health Device Profile 蓝牙关于医疗方面的应用 

HFP : Hands-free Profile  和电话相关,蓝牙接听、挂断电话 

HID : Human Interface Device  

         定义了蓝牙在人机接口设备中的协议、特征和使用规程。典型的应用包括蓝牙鼠标、蓝牙键盘、蓝牙游戏手柄等。该协议改编自USB HID Protocol

MAP : Message Access Profile

OPP : Object Push Profile

PAN : Personal Area Network Profile

描述了两个或更多个 Bluetooth 设备如何构成一个即时网络,和网络有关的还有串行端口功能(SPP),拨号网络功能(DUN)

PBAP: Phonebook Access Profile 电话号码簿访问协议

三、Enable Bluetooth

1. 服务启动:

frameworks/base/services/java/com/android/server/SystemServer.java

系统启动时在SystemServer中注册蓝牙服务管理BluetoothManagerService服务: 


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1             // Skip Bluetooth if we have an emulator kernel



02. 2             // TODO: Use a more reliable check to see if this product should



03. 3             // support Bluetooth - see bug 988521



04. 4             if (SystemProperties.get('ro.kernel.qemu').equals('1')) {



05. 5                 Slog.i(TAG, 'No Bluetooh Service (emulator)');



06. 6             else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {



07. 7                 Slog.i(TAG, 'No Bluetooth Service (factory test)');



08. 8             else if (!context.getPackageManager().hasSystemFeature



09. 9                        (PackageManager.FEATURE_BLUETOOTH)) {



10. 10                 Slog.i(TAG, 'No Bluetooth Service (Bluetooth Hardware Not Present)');



11. 11             else if (disableBluetooth) {



12. 12                 Slog.i(TAG, 'Bluetooth Service disabled by config');



13. 13             else {



14. 14                 Slog.i(TAG, 'Bluetooth Manager Service');



15. 15                 bluetooth = new BluetoothManagerService(context);



16. 16                 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);



17. 17             }


Bt 服务

 其它进程通过binder机制调用该服务,该服务属于综合服务管理类,包括AdapterService的启动、蓝牙适配器Adapter的管理等。

 2.  BluetoothAdapter 

Android的蓝牙Enable是由BluetoothAdapter提供的。只需要调用BluetoothAdapter.enable()即可启动蓝牙。下面我就分析这一个过程

 frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     /**



02. 2      * Turn on the local Bluetooth adapter—do not use without explicit



03. 3      * user action to turn on Bluetooth.



04. 4      * <p>This powers on the underlying Bluetooth hardware, and starts all



05. 5      * Bluetooth system services.



06. 6      * <p class='caution'><strong>Bluetooth should never be enabled without



07. 7      * direct user consent</strong>. If you want to turn on Bluetooth in order



08. 8      * to create a wireless connection, you should use the {@link



09. 9      * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests



10. 10      * user permission to turn on Bluetooth. The {@link #enable()} method is



11. 11      * provided only for applications that include a user interface for changing



12. 12      * system settings, such as a 'power manager' app.</p>



13. 13      * <p>This is an asynchronous call: it will return immediately, and



14. 14      * clients should listen for {@link #ACTION_STATE_CHANGED}



15. 15      * to be notified of subsequent adapter state changes. If this call returns



16. 16      * true, then the adapter state will immediately transition from {@link



17. 17      * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time



18. 18      * later transition to either {@link #STATE_OFF} or {@link



19. 19      * #STATE_ON}. If this call returns false then there was an



20. 20      * immediate problem that will prevent the adapter from being turned on -



21. 21      * such as Airplane mode, or the adapter is already turned on.



22. 22      * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}



23. 23      * permission



24. 24      *



25. 25      * @return true to indicate adapter startup has begun, or false on



26. 26      *         immediate error



27. 27      */



28. 28     public boolean enable() {



29. 29         if (isEnabled() == true){



30. 30             if (DBG) Log.d(TAG, 'enable(): BT is already enabled..!');



31. 31             return true;



32. 32         }



33. 33         try {



34. 34             return mManagerService.enable(); //



35. 35         catch (RemoteException e) {Log.e(TAG, '', e);}



36. 36         return false;



37. 37     }


mManagerService.enable()

mManagerService其实就是bluetoothAdapter的一个proxy, 


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     /**



02. 2      * Get a handle to the default local Bluetooth adapter.



03. 3      * <p>Currently Android only supports one Bluetooth adapter, but the API



04. 4      * could be extended to support more. This will always return the default



05. 5      * adapter.



06. 6      * @return the default local adapter, or null if Bluetooth is not supported



07. 7      *         on this hardware platform



08. 8      */



09. 9     public static synchronized BluetoothAdapter getDefaultAdapter() {



10. 10         if (sAdapter == null) {



11. 11             IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);



12. 12             if (b != null) {



13. 13                 IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);



14. 14                 sAdapter = new BluetoothAdapter(managerService);



15. 15             else {



16. 16                 Log.e(TAG, 'Bluetooth binder is null');



17. 17             }



18. 18         }



19. 19         return sAdapter;



20. 20     }


getDefaultAdapter 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     /**



02. 2      * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.



03. 3      */



04. 4     BluetoothAdapter(IBluetoothManager managerService) {



05. 5



06. 6         if (managerService == null) {



07. 7             throw new IllegalArgumentException('bluetooth manager service is null');



08. 8         }



09. 9         try {



10. 10             mService = managerService.registerAdapter(mManagerCallback);



11. 11         catch (RemoteException e) {Log.e(TAG, '', e);}



12. 12         mManagerService = managerService;



13. 13         mLeScanClients = new HashMap<LeScanCallback, GattCallbackWrapper>();



14. 14     }


BluetoothAdapter

 3. BluetoothManagerService

frameworks/base/services/java/com/android/server/BluetoothManagerService.java


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     public boolean enable() {



02. 2         if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&



03. 3             (!checkIfCallerIsForegroundUser())) {



04. 4             Log.w(TAG,'enable(): not allowed for non-active and non system user');



05. 5             return false;



06. 6         }



07. 7



08. 8         mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,



09. 9                                                 'Need BLUETOOTH ADMIN permission');



10. 10         if (DBG) {



11. 11             Log.d(TAG,'enable():  mBluetooth =' + mBluetooth +



12. 12                     ' mBinding = ' + mBinding);



13. 13         }



14. 14



15. 15         synchronized(mReceiver) {



16. 16             mQuietEnableExternal = false;



17. 17             mEnableExternal = true;



18. 18             // waive WRITE_SECURE_SETTINGS permission check



19. 19             long callingIdentity = Binder.clearCallingIdentity();



20. 20             persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);



21. 21             Binder.restoreCallingIdentity(callingIdentity);



22. 22             sendEnableMsg(false);



23. 23         }



24. 24         return true;



25. 25     }


BluetoothManagerService:enable() 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



1. 1     private void sendEnableMsg(boolean quietMode) {



2. 2         mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,



3. 3                              quietMode ? 1 00));



4. 4     }


sendEnableMsg

sendEnableMsg 交给handleMessage 处理,可以看到case MESSAGE_ENABLE: 里调用了handleEnable


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     private void handleEnable(boolean quietMode) {



02. 2         mQuietEnable = quietMode;



03. 3



04. 4         synchronized(mConnection) {



05. 5             if ((mBluetooth == null) && (!mBinding)) {



06. 6                 //Start bind timeout and bind



07. 7                 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);



08. 8                 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);



09. 9                 mConnection.setGetNameAddressOnly(false);



10. 10                 Intent i = new Intent(IBluetooth.class.getName());



11. 11                 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {



12. 12                     mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);



13. 13                 else {



14. 14                     mBinding = true;



15. 15                 }



16. 16             else if (mBluetooth != null) {



17. 17                 if (mConnection.isGetNameAddressOnly()) {



18. 18                     // if GetNameAddressOnly is set, we can clear this flag,



19. 19                     // so the service won't be unbind



20. 20                     // after name and address are saved



21. 21                     mConnection.setGetNameAddressOnly(false);



22. 22                     //Register callback object



23. 23                     try {



24. 24                         mBluetooth.registerCallback(mBluetoothCallback);



25. 25                     catch (RemoteException re) {



26. 26                         Log.e(TAG, 'Unable to register BluetoothCallback',re);



27. 27                     }



28. 28                     //Inform BluetoothAdapter instances that service is up



29. 29                     sendBluetoothServiceUpCallback();



30. 30                 }



31. 31



32. 32                 //Enable bluetooth



33. 33                 try {



34. 34                     if (!mQuietEnable) {



35. 35                         if(!mBluetooth.enable()) {



36. 36                             Log.e(TAG,'IBluetooth.enable() returned false');



37. 37                         }



38. 38                     }



39. 39                     else {



40. 40                         if(!mBluetooth.enableNoAutoConnect()) {



41. 41                             Log.e(TAG,'IBluetooth.enableNoAutoConnect() returned false');



42. 42                         }



43. 43                     }



44. 44                 catch (RemoteException e) {



45. 45                     Log.e(TAG,'Unable to call enable()',e);



46. 46                 }



47. 47             }



48. 48



49. 49             // Inform AudioRouteManager that bluetooth is enabled



50. 50             mAudioManager.setParameters(AUDIO_PARAMETER_KEY_BLUETOOTH_STATE + '=true');



51. 51         }



52. 52     }


handleEnable

可以看到是调用了mBluetooth.enable()

 4. AdapterService,AdapterState

packages/app/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1      public synchronized boolean enable(boolean quietMode) {



02. 2          enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,



03. 3                                         'Need BLUETOOTH ADMIN permission');



04. 4          if (DBG)debugLog('Enable called with quiet mode status =  ' + mQuietmode);



05. 5          mQuietmode  = quietMode;



06. 6          Message m =



07. 7                  mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);



08. 8          mAdapterStateMachine.sendMessage(m);



09. 9          return true;



10. 10      }


AdapterService:enable()

此处用了用了StateMachine,它会在AdapterState 里processMessage 处理(StateMachine就是状态机,在不同的状态下,收到相同的Event,做不同的事情),直接搜索UER_TURN_ON 可以看到下面:


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     private class OffState extends State {



02. 2         @Override



03. 3         public void enter() {



04. 4             infoLog('Entering OffState');



05. 5         }



06. 6



07. 7         @Override



08. 8         public boolean processMessage(Message msg) {



09. 9             AdapterService adapterService = mAdapterService;



10. 10             if (adapterService == null) {



11. 11                 Log.e(TAG,'receive message at OffState after cleanup:' +



12. 12                           msg.what);



13. 13                 return false;



14. 14             }



15. 15             switch(msg.what) {



16. 16                case USER_TURN_ON:



17. 17                    if (DBG) Log.d(TAG,'CURRENT_STATE=OFF, MESSAGE = USER_TURN_ON');



18. 18                    sendCrashToolInfo('ON');



19. 19                    notifyAdapterStateChange(BluetoothAdapter.STATE_TURNING_ON);



20. 20                    mPendingCommandState.setTurningOn(true);



21. 21                    transitionTo(mPendingCommandState);



22. 22                    sendMessageDelayed(START_TIMEOUT, START_TIMEOUT_DELAY);



23. 23                    adapterService.processStart();



24. 24                    break;



25. 25                case USER_TURN_OFF:



26. 26                    if (DBG) Log.d(TAG,'CURRENT_STATE=OFF, MESSAGE = USER_TURN_OFF');



27. 27                    sendCrashToolInfo('OFF');



28. 28                    //TODO: Handle case of service started and stopped without enable



29. 29                    break;



30. 30                default:



31. 31                    if (DBG) Log.d(TAG,'ERROR: UNEXPECTED MESSAGE: CURRENT_STATE=OFF, MESSAGE = ' + msg.what );



32. 32                    return false;



33. 33             }



34. 34             return true;



35. 35         }



36. 36     }


USER_TURN_ON

接下来是调用了adapterService.processStart()


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1     void processStart() {



02. 2         if (DBG) debugLog('processStart()');



03. 3         Class[] supportedProfileServices = Config.getSupportedProfiles();



04. 4         //Initialize data objects



05. 5         for (int i=0; i < supportedProfileServices.length;i++) {



06. 6             mProfileServicesState.put(supportedProfileServices[i].getName(),BluetoothAdapter.STATE_OFF);



07. 7         }



08. 8         mRemoteDevices = new RemoteDevices(this);



09. 9         mAdapterProperties.init(mRemoteDevices);



10. 10



11. 11         if(mBondStateMachine != null//Avoid resource leakage



12. 12         {



13. 13             mBondStateMachine.doQuit();



14. 14             mBondStateMachine.cleanup();



15. 15         }



16. 16



17. 17         if (DBG) {debugLog('processStart(): Make Bond State Machine');}



18. 18         mBondStateMachine = BondStateMachine.make(this, mAdapterProperties, mRemoteDevices);



19. 19



20. 20         mJniCallbacks.init(mBondStateMachine,mRemoteDevices);



21. 21



22. 22         //FIXME: Set static instance here???



23. 23         setAdapterService(this);



24. 24



25. 25         //Start profile services



26. 26         if (!mProfilesStarted && supportedProfileServices.length >0) {



27. 27             //Startup all profile services



28. 28             setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON);



29. 29         }else {



30. 30             if (DBG) {debugLog('processStart(): Profile Services alreay started');}



31. 31             mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED));



32. 32         }



33. 33     }


processStart

setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON); 是用来开启Bluetooth Profile 的,log 中可以看到:        


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.hfp.HeadsetService



02. 2         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.a2dp.A2dpService



03. 3         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.hid.HidService



04. 4         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.hdp.HealthService



05. 5         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.pan.PanService



06. 6         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.gatt.GattService



07. 7         BluetoothAdapterService( 1789): Starting service com.android.bluetooth.map.BluetoothMapService



08. 8     bluedroid( 1789): get_profile_interface handsfree



09. 9     bluedroid( 1789): get_profile_interface a2dp



10. 10     bluedroid( 1789): get_profile_interface avrcp



11. 11     bluedroid( 1789): get_profile_interface hidhost



12. 12     bluedroid( 1789): get_profile_interface health



13. 13     bluedroid( 1789): get_profile_interface pan



14. 14     bluedroid( 1789): get_profile_interface gatt?


BT Profile log

然后可以看到:mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED)); 


交给了PendingCommandState下的processMessage处理 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1                 case STARTED:   {



02. 2                     if (DBG) Log.d(TAG,'CURRENT_STATE=PENDING, MESSAGE = STARTED, isTurningOn=' + isTurningOn + ', isTurningOff=' + isTurningOff);



03. 3                     //Remove start timeout



04. 4                     removeMessages(START_TIMEOUT);



05. 5



06. 6                     //Enable



07. 7                     boolean ret = adapterService.enableNative();



08. 8                     if (!ret) {



09. 9                         errorLog('Error while turning Bluetooth On');



10. 10                         notifyAdapterStateChange(BluetoothAdapter.STATE_OFF);



11. 11                         transitionTo(mOffState);



12. 12                     else {



13. 13                         sendMessageDelayed(ENABLE_TIMEOUT, ENABLE_TIMEOUT_DELAY);



14. 14                     }



15. 15                 }


case STARTED

由mAdapterService.enableNative(); 可以看到 /*package*/ native boolean enableNative();

 此时就进入了JNI了

 5. JNI 调用

enableNative() 是在 packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 static jboolean enableNative(JNIEnv* env, jobject obj) {



02. 2     ALOGV('%s:',__FUNCTION__);



03. 3



04. 4     jboolean result = JNI_FALSE;



05. 5     if (!sBluetoothInterface) return result;



06. 6



07. 7     int ret = sBluetoothInterface->enable();



08. 8     result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;



09. 9     return result;



10. 10 }


enableNative static const bt_interface_t *sBluetoothInterface = NULL; 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



001. 1 /** NOTE: By default, no profiles are initialized at the time of init/enable.



002. 2  *  Whenever the application invokes the 'init' API of a profile, then one of



003. 3  *  the following shall occur:



004. 4  *



005. 5  *    1.) If Bluetooth is not enabled, then the Bluetooth core shall mark the



006. 6  *        profile as enabled. Subsequently, when the application invokes the



007. 7  *        Bluetooth 'enable', as part of the enable sequence the profile that were



008. 8  *        marked shall be enabled by calling appropriate stack APIs. The



009. 9  *        'adapter_properties_cb' shall return the list of UUIDs of the



010. 10  *        enabled profiles.



011. 11  *



012. 12  *    2.) If Bluetooth is enabled, then the Bluetooth core shall invoke the stack



013. 13  *        profile API to initialize the profile and trigger a



014. 14  *        'adapter_properties_cb' with the current list of UUIDs including the



015. 15  *        newly added profile's UUID.



016. 16  *



017. 17  *   The reverse shall occur whenever the profile 'cleanup' APIs are invoked



018. 18  */



019. 19



020. 20 /** Represents the standard Bluetooth DM interface. */



021. 21 typedef struct {



022. 22     /** set to sizeof(bt_interface_t) */



023. 23     size_t size;



024. 24     /**



025. 25      * Opens the interface and provides the callback routines



026. 26      * to the implemenation of this interface.



027. 27      */



028. 28     int (*init)(bt_callbacks_t* callbacks );



029. 29



030. 30     /** Enable Bluetooth. */



031. 31     int (*enable)(void);



032. 32



033. 33     /** Disable Bluetooth. */



034. 34     int (*disable)(void);



035. 35



036. 36     /** This ensures the chip is Powered ON  to support other radios in the combo chip.



037. 37      * If the chip is OFF it set the chip to ON, if it is already ON it just increases the radio ref count



038. 38      * to keep track when to Power OFF */



039. 39     int (*enableRadio)(void);



040. 40



041. 41     /** This decreases radio ref count  and ensures that chip is Powered OFF



042. 42      * when the radio ref count becomes zero. */



043. 43     int (*disableRadio)(void);



044. 44



045. 45     /** Closes the interface. */



046. 46     void (*cleanup)(void);



047. 47



048. 48     /** Get all Bluetooth Adapter properties at init */



049. 49     int (*get_adapter_properties)(void);



050. 50



051. 51     /** Get Bluetooth Adapter property of 'type' */



052. 52     int (*get_adapter_property)(bt_property_type_t type);



053. 53



054. 54     /** Set Bluetooth Adapter property of 'type' */



055. 55     /* Based on the type, val shall be one of



056. 56      * bt_bdaddr_t or bt_bdname_t or bt_scanmode_t etc



057. 57      */



058. 58     int (*set_adapter_property)(const bt_property_t *property);



059. 59



060. 60     /** Get all Remote Device properties */



061. 61     int (*get_remote_device_properties)(bt_bdaddr_t *remote_addr);



062. 62



063. 63     /** Get Remote Device property of 'type' */



064. 64     int (*get_remote_device_property)(bt_bdaddr_t *remote_addr,



065. 65                                       bt_property_type_t type);



066. 66



067. 67     /** Set Remote Device property of 'type' */



068. 68     int (*set_remote_device_property)(bt_bdaddr_t *remote_addr,



069. 69                                       const bt_property_t *property);



070. 70



071. 71     /** Get Remote Device's service record  for the given UUID */



072. 72     int (*get_remote_service_record)(bt_bdaddr_t *remote_addr,



073. 73                                      bt_uuid_t *uuid);



074. 74



075. 75     /** Start SDP to get remote services */



076. 76     int (*get_remote_services)(bt_bdaddr_t *remote_addr);



077. 77



078. 78     /** Start Discovery */



079. 79     int (*start_discovery)(void);



080. 80



081. 81     /** Cancel Discovery */



082. 82     int (*cancel_discovery)(void);



083. 83



084. 84     /** Create Bluetooth Bonding */



085. 85     int (*create_bond)(const bt_bdaddr_t *bd_addr);



086. 86



087. 87     /** Remove Bond */



088. 88     int (*remove_bond)(const bt_bdaddr_t *bd_addr);



089. 89



090. 90     /** Cancel Bond */



091. 91     int (*cancel_bond)(const bt_bdaddr_t *bd_addr);



092. 92



093. 93     /** BT Legacy PinKey Reply */



094. 94     /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */



095. 95     int (*pin_reply)(const bt_bdaddr_t *bd_addr, uint8_t accept,



096. 96                      uint8_t pin_len, bt_pin_code_t *pin_code);



097. 97



098. 98     /** BT SSP Reply - Just Works, Numeric Comparison and Passkey



099. 99      * passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &



100. 100      * BT_SSP_VARIANT_CONSENT



101. 101      * For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey



102. 102      * shall be zero */



103. 103     int (*ssp_reply)(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,



104. 104                      uint8_t accept, uint32_t passkey);



105. 105



106. 106     /** Get Bluetooth profile interface */



107. 107     const void* (*get_profile_interface) (const char *profile_id);



108. 108



109. 109     /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */



110. 110     /* Configure DUT Mode - Use this mode to enter/exit DUT mode */



111. 111     int (*dut_mode_configure)(uint8_t enable);



112. 112



113. 113     /* Send any test HCI (vendor-specific) command to the controller. Must be in DUT Mode */



114. 114     int (*dut_mode_send)(uint16_t opcode, uint8_t *buf, uint8_t len);



115. 115     /** BLE Test Mode APIs */



116. 116     /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End */



117. 117     int (*le_test_mode)(uint16_t opcode, uint8_t *buf, uint8_t len);



118. 118



119. 119     /* enable or disable bluetooth HCI snoop log */



120. 120     int (*config_hci_snoop_log)(uint8_t enable);



121. 121



122. 122     /** Get FM module interface */



123. 123     const void* (*get_fm_interface) ();



124. 124 } bt_interface_t;


bt_interface_t 定义

 bt_interface_t 定义 在hardware/libhardware/include/hardware/bluetooth.h


sBluetoothInterface的初始化在classInitNative(),这个函数大概做了以下的事情: 1)、注册java的回调函数(就是当下层已经打开蓝牙了,然后要通知上层,蓝牙已经打开了,java层就可以发送蓝牙打开的Broadcast了。) 2)、初始化蓝牙模块的HAL接口。 3)、得到sBluetoothInterface  

6. Bluedroid ->bluetooth.c

external/bluetooth/bluedroid/btif/src/bluetooth.c

sBluetoothInterface->enable(); 会调到下方


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 static int enable( void )



02. 2 {



03. 3     ALOGI('enable');



04. 4



05. 5     /* sanity check */



06. 6     if (interface_ready() == FALSE)



07. 7         return BT_STATUS_NOT_READY;



08. 8



09. 9     return btif_enable_bluetooth();



10. 10 }


bluetooth:enable()

接下来调用:external/bluetooth/bluedroid/btif/src/Btif_core.c


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /*******************************************************************************



02. 2 **



03. 3 ** Function         btif_enable_bluetooth



04. 4 **



05. 5 ** Description      Performs chip power on and kickstarts OS scheduler



06. 6 **



07. 7 ** Returns          bt_status_t



08. 8 **



09. 9 *******************************************************************************/



10. 10



11. 11 bt_status_t btif_enable_bluetooth(void)



12. 12 {



13. 13     BTIF_TRACE_DEBUG0('BTIF ENABLE BLUETOOTH');



14. 14



15. 15     if (0 == btif_core_radio_ref_count){



16. 16



17. 17         if (btif_core_state != BTIF_CORE_STATE_DISABLED)



18. 18         {



19. 19             ALOGD('not disabled



20. ');



21. 20             return BT_STATUS_DONE;



22. 21         }



23. 22



24. 23         btif_core_state = BTIF_CORE_STATE_ENABLING;



25. 24



26. 25         /* Create the GKI tasks and run them */



27. 26         bte_main_enable();



28. 27         btif_core_radio_ref_count++;



29. 28     }



30. 29     else



31. 30     {



32. 31         btif_core_radio_ref_count++;



33. 32         /*btif core/chip is already enabled so just do other initialisation according to event*/



34. 33         btif_transfer_context(btif_in_generic_evt, BTIF_CORE_BT_STATE_ON, NULL, 0, NULL);



35. 34     }



36. 35



37. 36     return BT_STATUS_SUCCESS;



38. 37 }


btif_enable_bluetooth

 external/bluetooth/bluedroid/main/Bte_main.c


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /******************************************************************************



02. 2 **



03. 3 ** Function         bte_main_enable



04. 4 **



05. 5 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called



06. 6 **                  part of the Bluetooth stack enable sequence



07. 7 **



08. 8 ** Returns          None



09. 9 **



10. 10 ******************************************************************************/



11. 11 void bte_main_enable()



12. 12 {



13. 13     APPL_TRACE_DEBUG1('%s', __FUNCTION__);



14. 14



15. 15     /* Initialize BTE control block */



16. 16     BTE_Init();



17. 17



18. 18     lpm_enabled = FALSE;



19. 19



20. 20     bte_hci_enable();



21. 21



22. 22     GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,



23. 23                     (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),



24. 24                     sizeof(bte_btu_stack));



25. 25



26. 26     GKI_run(0);



27. 27 }


bte_main_enable 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /******************************************************************************



02. 2 **



03. 3 ** Function         bte_hci_enable



04. 4 **



05. 5 ** Description      Enable HCI & Vendor modules



06. 6 **



07. 7 ** Returns          None



08. 8 **



09. 9 ******************************************************************************/



10. 10 static void bte_hci_enable(void)



11. 11 {



12. 12     APPL_TRACE_DEBUG1('%s', __FUNCTION__);



13. 13



14. 14     preload_start_wait_timer();



15. 15



16. 16     if (bt_hc_if)



17. 17     {



18. 18         int result = bt_hc_if->init(&hc_callbacks, btif_local_bd_addr.address);



19. 19         APPL_TRACE_EVENT1('libbt-hci init returns %d', result);



20. 20



21. 21         assert(result == BT_HC_STATUS_SUCCESS);



22. 22



23. 23         if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)



24. 24             bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile);



25. 25



26. 26 #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)



27. 27         APPL_TRACE_DEBUG1('%s  Not Turninig Off the BT before Turninig ON', __FUNCTION__);



28. 28



29. 29         /* Do not power off the chip before powering on  if BT_CLEAN_TURN_ON_DISABLED flag



30. 30          is defined and set to TRUE to avoid below mentioned issue.



31. 31



32. 32          Wingray kernel driver maintains a combined  counter to keep track of



33. 33          BT-Wifi state. Invoking  set_power(BT_HC_CHIP_PWR_OFF) when the BT is already



34. 34          in OFF state causes this counter to be incorrectly decremented and results in undesired



35. 35          behavior of the chip.



36. 36



37. 37          This is only a workaround and when the issue is fixed in the kernel this work around



38. 38          should be removed. */



39. 39 #else



40. 40         /* toggle chip power to ensure we will reset chip in case



41. 41            a previous stack shutdown wasn't completed gracefully */



42. 42         bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);



43. 43 #endif



44. 44         bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);



45. 45



46. 46         bt_hc_if->preload(NULL);



47. 47     }



48. 48 }


bte_hci_enable

我们先看下bt_hc_if->set_power,前面有做一些初始化Bluedroid的动作

由 bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);可以看出:

static bt_hc_interface_t *bt_hc_if=NULL;

下面是bt_hc_if 的初始化


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /******************************************************************************



02. 2 **



03. 3 ** Function         bte_main_in_hw_init



04. 4 **



05. 5 ** Description      Internal helper function for chip hardware init



06. 6 **



07. 7 ** Returns          None



08. 8 **



09. 9 ******************************************************************************/



10. 10 static void bte_main_in_hw_init(void)



11. 11 {



12. 12     if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface())



13. 13          == NULL)



14. 14     {



15. 15         APPL_TRACE_ERROR0('!!! Failed to get BtHostControllerInterface !!!');



16. 16     }



17. 17



18. 18     memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));



19. 19 }


bte_main_in_hw_init

 external/bluetooth/bluedroid/hci/src/Bt_hci_bdroid.c


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /*******************************************************************************



02. 2 **



03. 3 ** Function        bt_hc_get_interface



04. 4 **



05. 5 ** Description     Caller calls this function to get API instance



06. 6 **



07. 7 ** Returns         API table



08. 8 **



09. 9 *******************************************************************************/



10. 10 const bt_hc_interface_t *bt_hc_get_interface(void)



11. 11 {



12. 12     return &bluetoothHCLibInterface;



13. 13 }


bt_hc_get_interface 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 static const bt_hc_interface_t bluetoothHCLibInterface = {



02. 2     sizeof(bt_hc_interface_t),



03. 3     init,



04. 4     set_power,



05. 5     lpm,



06. 6     preload,



07. 7     postload,



08. 8     transmit_buf,



09. 9     set_rxflow,



10. 10     logging,



11. 11     cleanup



12. 12 };


bluetoothHCLibInterface

由以上可以看到set_power


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /** Chip power control */



02. 2 static void set_power(bt_hc_chip_power_state_t state)



03. 3 {



04. 4     int pwr_state;



05. 5



06. 6     BTHCDBG('set_power %d', state);



07. 7



08. 8     /* Calling vendor-specific part */



09. 9     pwr_state = (state == BT_HC_CHIP_PWR_ON) ? BT_VND_PWR_ON : BT_VND_PWR_OFF;



10. 10



11. 11     if (bt_vnd_if)



12. 12         bt_vnd_if->op(BT_VND_OP_POWER_CTRL, &pwr_state);



13. 13     else



14. 14         ALOGE('vendor lib is missing!');



15. 15 }


set_power

可以看到,bt_vnd_if->op(BT_VND_OP_POWER_CTRL, &pwr_state);

 external/bluetooth/bluedroid/hci/src/Bt_hw.c

bt_vendor_interface_t *bt_vnd_if=NULL;

 bt_vnd_if 初始化:


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /******************************************************************************



02. 2 **



03. 3 ** Function         init_vnd_if



04. 4 **



05. 5 ** Description      Initialize vendor lib interface



06. 6 **



07. 7 ** Returns          None



08. 8 **



09. 9 ******************************************************************************/



10. 10 void init_vnd_if(unsigned char *local_bdaddr)



11. 11 {



12. 12     void *dlhandle;



13. 13



14. 14     dlhandle = dlopen('libbt-vendor.so', RTLD_NOW);



15. 15     if (!dlhandle)



16. 16     {



17. 17         ALOGE('!!! Failed to load libbt-vendor.so !!!');



18. 18         return;



19. 19     }



20. 20



21. 21     bt_vnd_if = (bt_vendor_interface_t *) dlsym(dlhandle, 'BLUETOOTH_VENDOR_LIB_INTERFACE');



22. 22     if (!bt_vnd_if)



23. 23     {



24. 24         ALOGE('!!! Failed to get bt vendor interface !!!');



25. 25         return;



26. 26     }



27. 27



28. 28     bt_vnd_if->init(&vnd_callbacks, local_bdaddr);



29. 29 }


init_vnd_if

在init_vnd_if()函数可以看到其实是一个libbt-vendor.so的interface。这个是Vendor(芯片厂商)的library

Vendor就是芯片供应商的意思,在他们做好一块蓝牙芯片后,需要提供一些硬件相关的动作,比如上下电,设置波特率之类的。但是这些操作一般不会对没有许可的开放。Bluedroid提供了一个统一的接口bt_vendor_interface_t,供应商只需要实现这个接口定义的蓝牙相关的操作就可以交给bluedroid去做剩下的事情了

下面主要是broadcom 为例,我们进入/hardware/里面:

$ find . -name Android.mk |xargs grep libbt
./broadcom/libbt/Android.mk:LOCAL_MODULE := libbt-vendor
./broadcom/libbt/Android.mk:LOCAL_MODULE := libbt-vendor
./broadcom/libbt/Android.mk: LOCAL_SRC_FILES := $(TI_BT_VENDOR_PATH)/libbt-vendor-ti.c
./qcom/bt/Android.mk:include $(call all-named-subdir-makefiles,libbt-vendor)
./qcom/bt/libbt-vendor/Android.mk:LOCAL_MODULE := libbt-vendor

或者

$ grep -nr BT_VND_OP_POWER_CTRL
broadcom/libbt/src/bt_vendor_brcm.c:147: case BT_VND_OP_POWER_CTRL:
broadcom/libbt/src/bt_vendor_brcm.c:149: BTVNDDBG('op: BT_VND_OP_POWER_CTRL');
qcom/bt/libbt-vendor/src/bt_vendor_qcom.c:105: case BT_VND_OP_POWER_CTRL:

在broadcom/libbt/src/bt_vendor_brcm.c


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



001. 1 /** Requested operations */



002. 2 static int op(bt_vendor_opcode_t opcode, void *param)



003. 3 {



004. 4     int retval = 0;



005. 5



006. 6     switch(opcode)



007. 7     {



008. 8         case BT_VND_OP_POWER_CTRL:



009. 9             {



010. 10                 BTVNDDBG('op: BT_VND_OP_POWER_CTRL');



011. 11                 int *state = (int *) param;



012. 12                 if (*state == BT_VND_PWR_OFF)



013. 13                     upio_set_bluetooth_power(UPIO_BT_POWER_OFF);



014. 14                 else if (*state == BT_VND_PWR_ON)



015. 15                     upio_set_bluetooth_power(UPIO_BT_POWER_ON);



016. 16                     BTVNDDBG('Delay for a while after BT power on');



017. 17                     usleep(200000);



018. 18             }



019. 19             break;



020. 20



021. 21         case BT_VND_OP_FW_CFG:



022. 22             {



023. 23                 BTVNDDBG('op: BT_VND_OP_FW_CFG');



024. 24                 hw_config_start();



025. 25             }



026. 26             break;



027. 27



028. 28         case BT_VND_OP_SCO_CFG:



029. 29             {



030. 30                 BTVNDDBG('op: BT_VND_OP_SCO_CFG');



031. 31 #if (SCO_CFG_INCLUDED == TRUE)



032. 32                 if (is2076())



033. 33                 {



034. 34                     ALOGD('PCM2 Settings for AP6476(BCM2076)');



035. 35                     hw_pcm2_config();



036. 36                 }



037. 37                 else



038. 38                 {



039. 39                     ALOGD('SCO config');



040. 40                     hw_sco_config();



041. 41                 }



042. 42 #else



043. 43                 retval = -1;



044. 44 #endif



045. 45             }



046. 46             break;



047. 47



048. 48         case BT_VND_OP_USERIAL_OPEN:



049. 49             {



050. 50                 BTVNDDBG('op: BT_VND_OP_USERIAL_OPEN');



051. 51                 int (*fd_array)[] = (int (*)[]) param;



052. 52                 int fd, idx;



053. 53                 fd = userial_vendor_open((tUSERIAL_CFG *) &userial_init_cfg);



054. 54                 if (fd != -1)



055. 55                 {



056. 56                     for (idx=0; idx < CH_MAX; idx++)



057. 57                         (*fd_array)[idx] = fd;



058. 58



059. 59                     retval = 1;



060. 60                 }



061. 61                 /* retval contains numbers of open fd of HCI channels */



062. 62             }



063. 63             break;



064. 64



065. 65         case BT_VND_OP_USERIAL_CLOSE:



066. 66             {



067. 67                 BTVNDDBG('op: BT_VND_OP_USERIAL_CLOSE');



068. 68                 userial_vendor_close();



069. 69             }



070. 70             break;



071. 71



072. 72         case BT_VND_OP_GET_LPM_IDLE_TIMEOUT:



073. 73             {



074. 74                 BTVNDDBG('op: BT_VND_OP_GET_LPM_IDLE_TIMEOUT');



075. 75                 uint32_t *timeout_ms = (uint32_t *) param;



076. 76                 *timeout_ms = hw_lpm_get_idle_timeout();



077. 77             }



078. 78             break;



079. 79



080. 80         case BT_VND_OP_LPM_SET_MODE:



081. 81             {



082. 82                 BTVNDDBG('op: BT_VND_OP_LPM_SET_MODE');



083. 83                 uint8_t *mode = (uint8_t *) param;



084. 84                 retval = hw_lpm_enable(*mode);



085. 85             }



086. 86             break;



087. 87



088. 88         case BT_VND_OP_LPM_WAKE_SET_STATE:



089. 89             {



090. 90                 uint8_t *state = (uint8_t *) param;



091. 91                 uint8_t wake_assert = (*state == BT_VND_LPM_WAKE_ASSERT) ?



092. 92                                         TRUE : FALSE;



093. 93



094. 94                 hw_lpm_set_wake_state(wake_assert);



095. 95             }



096. 96             break;



097. 97



098. 98         case BT_VND_OP_WBS_CFG:



099. 99             {



100. 100 #if (SCO_USE_I2S_INTERFACE == TRUE)



101. 101                 uint8_t *state = (uint8_t *) param;



102. 102                 hw_wbs_enable(*state);



103. 103 #else



104. 104                 ALOGE('WBS configuration not supported without I2S');



105. 105 #endif // (SCO_USE_I2S_INTERFACE == TRUE)



106. 106             }



107. 107             break;



108. 108



109. 109         case BT_VND_OP_EPILOG:



110. 110             {



111. 111 #if (HW_END_WITH_HCI_RESET == FALSE)



112. 112                 if (bt_vendor_cbacks)



113. 113                 {



114. 114                     bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);



115. 115                 }



116. 116 #else



117. 117                 hw_epilog_process();



118. 118 #endif



119. 119             }



120. 120             break;



121. 121     }



122. 122



123. 123     return retval;



124. 124 }


op 

Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 /*******************************************************************************



02. 2 **



03. 3 ** Function        upio_set_bluetooth_power



04. 4 **



05. 5 ** Description     Interact with low layer driver to set Bluetooth power



06. 6 **                 on/off.



07. 7 **



08. 8 ** Returns         0  : SUCCESS or Not-Applicable



09. 9 **                 <0 : ERROR



10. 10 **



11. 11 *******************************************************************************/



12. 12 int upio_set_bluetooth_power(int on)



13. 13 {



14. 14     int sz;



15. 15     int fd = -1;



16. 16     int ret = -1;



17. 17     char buffer = '0';



18. 18



19. 19     switch(on)



20. 20     {



21. 21         case UPIO_BT_POWER_OFF:



22. 22             buffer = '0';



23. 23             break;



24. 24



25. 25         case UPIO_BT_POWER_ON:



26. 26             buffer = '1';



27. 27             break;



28. 28     }



29. 29



30. 30     if (is_emulator_context())



31. 31     {



32. 32         /* if new value is same as current, return -1 */



33. 33         if (bt_emul_enable == on)



34. 34             return ret;



35. 35



36. 36         UPIODBG('set_bluetooth_power [emul] %d', on);



37. 37



38. 38         bt_emul_enable = on;



39. 39         return 0;



40. 40     }



41. 41



42. 42     /* check if we have rfkill interface */



43. 43     if (is_rfkill_disabled())



44. 44         return 0;



45. 45



46. 46     if (rfkill_id == -1)



47. 47     {



48. 48         if (init_rfkill())



49. 49             return ret;



50. 50     }



51. 51



52. 52     fd = open(rfkill_state_path, O_WRONLY);



53. 53



54. 54     if (fd < 0)



55. 55     {



56. 56         ALOGE('set_bluetooth_power : open(%s) for write failed: %s (%d)',



57. 57             rfkill_state_path, strerror(errno), errno);



58. 58         return ret;



59. 59     }



60. 60



61. 61     sz = write(fd, &buffer, 1);



62. 62



63. 63     if (sz < 0) {



64. 64         ALOGE('set_bluetooth_power : write(%s) failed: %s (%d)',



65. 65             rfkill_state_path, strerror(errno),errno);



66. 66     }



67. 67     else



68. 68         ret = 0;



69. 69



70. 70     if (fd >= 0)



71. 71         close(fd);



72. 72



73. 73     return ret;



74. 74 }


upio_set_bluetooth_power

static char *rfkill_state_path = NULL;

 rfkill_state_path 是在下面初始化的。


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 static int init_rfkill()



02. 2 {



03. 3     char path[64];



04. 4     char buf[16];



05. 5     int fd, sz, id;



06. 6



07. 7     if (is_rfkill_disabled())



08. 8         return -1;



09. 9



10. 10     for (id = 0; ; id++)



11. 11     {



12. 12         snprintf(path, sizeof(path), '/sys/class/rfkill/rfkill%d/type', id);



13. 13         fd = open(path, O_RDONLY);



14. 14         if (fd < 0)



15. 15         {



16. 16             ALOGE('init_rfkill : open(%s) failed: %s (%d)



17. ',



18. 17                  path, strerror(errno), errno);



19. 18             return -1;



20. 19         }



21. 20



22. 21         sz = read(fd, &buf, sizeof(buf));



23. 22         close(fd);



24. 23



25. 24         if (sz >= 9 && memcmp(buf, 'bluetooth'9) == 0)



26. 25         {



27. 26             rfkill_id = id;



28. 27             break;



29. 28         }



30. 29     }



31. 30



32. 31     asprintf(&rfkill_state_path, '/sys/class/rfkill/rfkill%d/state', rfkill_id);



33. 32     return 0;



34. 33 }


init_rfkill

原来就是在rfkill_state_path(/sys/class/rfkill/rfkill[x]/state)虚拟设备里写入了1

shell@android:/sys/class/rfkill/rfkill0 $ cat state
0  // 表示蓝牙是关闭状态

shell@android:/sys/class/rfkill/rfkill0 $ cat state 

1   // 开启蓝牙后可以看到

rfkill是Linux下的一个标准的无线控制的虚拟设备,Linux也提供了rfkill的命令去查看以及控制所有的注册的无线设备。它们会在/dev/(PC的Linux)或者/sys/class(一般是Android)下生成相应的虚拟设备。

结合set_power 下面的log 和 bluetoothHCLibInterface  定义,可以看到接下来是调用的 bluetoothHCLibInterface 里的 proload->bthc_signal_event(HC_EVENT_PRELOAD)->bt_hc_worker_thread -》userial_open(USERIAL_PORT_1)->bt_vnd_if->op(BT_VND_OP_USERIAL_OPEN, &fd_array);->userial_vendor_open((tUSERIAL_CFG *) &userial_init_cfg);

接下来是Hardware.c里

hw_config_start-》hw_config_cback

部分log 如下:


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



01. 1 01-01 00:21:02.240 I/bt_userial_vendor( 1821): userial vendor open: opening /dev/ttyMFD0



02. 2 01-01 00:21:02.240 I/GKI_LINUX( 1821): gki_task_entry: gki_task_entry task_id=0 [BTU] starting



03. 3 01-01 00:21:02.240 I/bt-btu ( 1821): btu_task pending for preload complete event



04. 4 01-01 00:21:02.260 I/bt_userial_vendor( 1821): device fd = 71 open



05. 5 01-01 00:21:02.260 E/bt_hwcfg( 1821): Hardware.c--hw_config_start



06. 6 01-01 00:21:02.290 D/bt_hwcfg( 1821): Chipset BCM2076B1



07. 7 01-01 00:21:02.290 D/bt_hwcfg( 1821): Target name = [BCM2076B1]



08. 8 01-01 00:21:02.290 I/bt_hwcfg( 1821): FW patchfile: /etc/firmware/bt/bcm2076b1.hcd



09. 9 01-01 00:21:02.300 I/bt_hwcfg( 1821): bt vendor lib: set UART baud 3000000



10. 10 01-01 00:21:02.590 I/bt_hwcfg( 1821): bt vendor lib: set UART baud 115200



11. 11 01-01 00:21:02.590 D/bt_hwcfg( 1821): Settlement delay -- 100 ms



12. 12 01-01 00:21:02.690 I/bt_hwcfg( 1821): bt vendor lib: set UART baud 3000000



13. 13 01-01 00:21:02.690 I/bt_hwcfg( 1821): Setting local bd addr to 22:22:C7:74:9F:05



14. 14 01-01 00:21:02.720 I/bt_hwcfg( 1821): vendor lib fwcfg completed


log

下面为minicom 里开启蓝牙的logcat 的log


Android 高版本BluetoothDevice bluetooth apk,Android 高版本BluetoothDevice bluetooth apk_Android,第1张


view source print ?



001. 1 [  619.545795] iTCO_wdt: iTCO_wdt_keepalive



002. 2 [  629.551341] iTCO_wdt: iTCO_wdt_keepalive



003. 3 [  639.556687] iTCO_wdt: iTCO_wdt_keepalive



004. 4



005. 5 shell@byt_t_crv2:/ $



006. 6 shell@byt_t_crv2:/ $



007. 7 shell@byt_t_crv2:/ $



008. 8 shell@byt_t_crv2:/ $



009. 9 shell@byt_t_crv2:/ $



010. 10 shell@byt_t_crv2:/ $



011. 11 shell@byt_t_crv2:/ $



012. 12 shell@byt_t_crv2:/ $ logc[  649.561872] iTCO_wdt: iTCO_wdt_keepalive



013. 13 at -vtime



014. 14 --------- beginning of /dev/log/main



015. 15 01-01 00:20:19.810 I/CRASHLOG(  165): [IPTRAK] check_iptrak_file: Initial value of 'need_update' 0.



016. 16 01-01 00:20:19.810 I/CRASHLOG(  165): [IPTRAK] check_iptrak_file: Force update value 0.



017. 17 01-01 00:20:19.810 I/CRASHLOG(  165): [IPTRAK] check_iptrak_file: 'need_update' value 0.



018. 18 [  654.053041] kionix_accel 3-000e: kionix_accel_enable: waiting for resume



019. 19 [  654.054183] request_suspend_state: wakeup (3->0) at 653345302368 (2001-01-01 00:20:52.374454198 UTC)



020. 20 01-01 00:20:52.370 I/WindowManager(  621): Screen turning on...



021. 21 --------- beginning of /dev/log/system



022. 22 01-01 00:20:52.370 I/PowerManagerServic[  654.085087] i915 0000:00:02.0: setting latency timer to 64



023. 23 e(  621): Waking up from sleep...



024. 24 01-01 00:20:52.380 I/InputDis[  654.097219] [drm:clock_off_bend_spread] *ERROR* INFO: PUNIT clocks already OFF



025. 25 patcher(  621): [  654.106967] [drm:clock_off_bend_spread] *ERROR* INFO: PUNIT clocks already OFF



026. 26 Dropped event because input dispatch is disabled[  654.119349] [drm:intel_modeset_pipe_config] *ERROR* plane bpp: 18, pipe bpp: 18, dithering: 0



027. 27 .



028. 28 01-01 00:20:52.380 E/Sensors (  621): writeToFile: line: 209: Cannot open the driver interface for activate: /sys/bus/i2c/devices/3-000e/enable



029. 29 01-01 00:20:52.380 V/KeyguardServiceDelegate(  621): onScreenTurnedOn(showListener = com.android.internal.policy.impl.PhoneWindowManager$18@225247d8)



030. 30 01-01 00:20:52.380 V/ActivityManager(  621): Broadcast: Intent { act=android.intent.action.SCREEN_ON flg=0x50000010 } ordered=true userid=-1



031. 31 01-01 00:20:52.380 V/ActivityManager(  621): Enqueing broadcast: android.intent.action.SCREEN_ON replacePending=false



032. 32 01-01 00:20:52.380 I/ActivityManager(  621): Broadcast intent Intent { act=android.intent.action.SCREEN_ON flg=0x50000010 } on foreground queue



033. 33 01-01 00:20:52.380 V/ActivityManager(  621): Enqueueing ordered broadcast BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON}: prev had 0



034. 34 01-01 00:20:52.380 I/ActivityManager(  621): Enqueueing broadcast android.intent.action.SCREEN_ON seq=-1



035. 35 01-01 00:20:52.380 V/BroadcastQueue(  621): Schedule broadcasts [foreground]: current=false



036. 36 01-01 00:20:52.380 V/BroadcastQueue(  621): Received BROADCAST_INTENT_MSG



037. 37 01-01 00:20:52.380 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



038. 38 01-01 00:20:52.380 V/BroadcastQueue(  621): Processing ordered broadcast [foreground] BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON}



039. 39 01-01 00:20:52.380 V/BroadcastQueue(  621): Submitting BROADCAST_TIMEOUT_MSG [foreground] for BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON} at 663366



040. 40 01-01 00:20:52.380 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{223b23c0 u0 ReceiverList{22376b68 621 system/1000/u0 local:21f9aab8}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



041. 41 01-01 00:20:52.380 I/BroadcastQueue(  621): Delivering to BroadcastFilter{223b23c0 u0 ReceiverList{22376b68 621 system/1000/u0 local:21f9aab8}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



042. 42 01-01 00:20:52.410 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@21f9aab8



043. 43 01-01 00:20:52.410 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



044. 44 01-01 00:20:52.410 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{222cdf18 u0 ReceiverList{22250af8 621 system/1000/u0 local:22064c68}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



045. 45 01-01 00:20:52.410 I/BroadcastQueue(  621): Delivering to BroadcastFilter{222cdf18 u0 ReceiverList{22250af8 621 system/1000/u0 local:22064c68}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



046. 46 01-01 00:20:52.410 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



047. 47 01-01 00:20:52.410 D/ActivityManager(  621): Did OOM ADJ in 0ms



048. 48 01-01 00:20:52.410 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@22064c68



049. 49 01-01 00:20:52.410 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



050. 50 01-01 00:20:52.410 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{222c5838 u0 ReceiverList{2224b440 621 system/1000/u0 local:2203e808}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



051. 51 01-01 00:20:52.410 I/BroadcastQueue(  621): Delivering to BroadcastFilter{222c5838 u0 ReceiverList{2224b440 621 system/1000/u0 local:2203e808}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



052. 52 01-01 00:20:52.410 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



053. 53 01-01 00:20:52.410 D/ActivityManager(  621): Did OOM ADJ in 0ms



054. 54 01-01 00:20:52.410 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@2203e808



055. 55 01-01 00:20:52.410 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



056. 56 01-01 00:20:52.420 D/AudioHardwareALSA(  158): setParameters



057. 57 01-01 00:20:52.420 D/RouteManager(  158): doSetParameters: key value pair screen_state=on, {+++ RECONSIDER ROUTING +++} due to External parameter change



058. 58 01-01 00:20:52.420 D/RouteManager(  158): reconsiderRouting



059. 59 01-01 00:20:52.420 D/EVENT_THREAD(  158): void CEventThread::trig(uint16_t): in



060. 60 01-01 00:20:52.420 D/EVENT_THREAD(  158): void CEventThread::trig(uint16_t): out



061. 61 01-01 00:20:52.420 D/RouteManager(  158): doReconsiderRouting:       Platform Changes:



062. 62 01-01 00:20:52.420 D/RouteManager(  158): doReconsiderRouting:          -Platform Screen State = On [has changed]



063. 63 01-01 00:20:52.420 D/EVENT_THREAD(  158): void CEventThread::run() Do poll with timeout: -1



064. 64 01-01 00:20:52.420 D/RouteManager(  158): reconsiderRouting: DONE



065. 65 01-01 00:20:52.420 D/EVENT_THREAD(  158): void CEventThread::run() POLLIN event on Fd (1)



066. 66 01-01 00:20:52.420 D/EVENT_THREAD(  158): void CEventThread::run() Do poll with timeout: -1



067. 67 01-01 00:20:52.420 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{222bc8b0 u0 ReceiverList{22246620 621 system/1000/u0 local:224d3230}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



068. 68 01-01 00:20:52.420 I/BroadcastQueue(  621): Delivering to BroadcastFilter{222bc8b0 u0 ReceiverList{22246620 621 system/1000/u0 local:224d3230}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



069. 69 01-01 00:20:52.420 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



070. 70 01-01 00:20:52.420 D/ActivityManager(  621): Did OOM ADJ in 0ms



071. 71 01-01 00:20:52.420 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@224d3230



072. 72 01-01 00:20:52.420 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



073. 73 01-01 00:20:52.420 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{22298f10 u0 ReceiverList{221e9848 621 system/1000/u0 local:2236f1b8}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



074. 74 01-01 00:20:52.420 I/BroadcastQueue(  621): Delivering to BroadcastFilter{22298f10 u0 ReceiverList{221e9848 621 system/1000/u0 local:2236f1b8}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



075. 75 01-01 00:20:52.420 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



076. 76 01-01 00:20:52.420 D/ActivityManager(  621): Did OOM ADJ in 0ms



077. 77 01-01 00:20:52.420 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@2236f1b8



078. 78 01-01 00:20:52.420 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



079. 79 01-01 00:20:52.420 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{2225d228 u0 ReceiverList{221de790 621 system/1000/u0 local:22200ba0}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



080. 80 01-01 00:20:52.420 I/BroadcastQueue(  621): Delivering to BroadcastFilter{2225d228 u0 ReceiverList{221de790 621 system/1000/u0 local:22200ba0}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



081. 81 01-01 00:20:52.420 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



082. 82 01-01 00:20:52.420 D/ActivityManager(  621): Did OOM ADJ in 0ms



083. 83 01-01 00:20:52.420 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@22200ba0



084. 84 01-01 00:20:52.420 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



085. 85 01-01 00:20:52.420 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{221fc378 u0 ReceiverList{220fb6f8 621 system/1000/u0 local:22240368}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



086. 86 01-01 00:20:52.420 I/BroadcastQueue(  621): Delivering to BroadcastFilter{221fc378 u0 ReceiverList{220fb6f8 621 system/1000/u0 local:22240368}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



087. 87 01-01 00:20:52.420 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



088. 88 01-01 00:20:52.420 D/ActivityManager(  621): Did OOM ADJ in 0ms



089. 89 01-01 00:20:52.420 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@22240368



090. 90 01-01 00:20:52.420 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



091. 91 01-01 00:20:52.420 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{2219dd90 u-1 ReceiverList{22015a60 621 system/1000/u-1 local:22508ef0}}: BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON}



092. 92 01-01 00:20:52.420 I/BroadcastQueue(  621): Delivering to BroadcastFilter{2219dd90 u-1 ReceiverList{22015a60 621 system/1000/u-1 local:22508ef0}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



093. 93 01-01 00:20:52.420 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



094. 94 01-01 00:20:52.420 D/ActivityManager(  621): Did OOM ADJ in 0ms



095. 95 01-01 00:20:52.430 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@22508ef0



096. 96 01-01 00:20:52.430 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



097. 97 01-01 00:20:52.430 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{222af680 u0 ReceiverList{22506f08 814 com.android.systemui/10011/u0 remote:22571b78}}: BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON}



098. 98 01-01 00:20:52.430 I/BroadcastQueue(  621): Delivering to BroadcastFilter{222af680 u0 ReceiverList{22506f08 814 com.android.systemui/10011/u0 remote:22571b78}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



099. 99 01-01 00:20:52.430 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



100. 100 01-01 00:20:52.430 D/ActivityManager(  621): Did OOM ADJ in 0ms



101. 101 01-01 00:20:52.450 D/InputMethodManagerService(  621): --- calledFromForegroundUserOrSystemProcess ? calling uid = 10011 system uid = 1000 calling userId = 0, foreground user id = 0, calling pid = 814com.android.server.InputMethodManagerService.getInputMethodList(InputMethodManagerService.java:945)



102. 102 01-01 00:20:52.450 D/InputMethodManagerService(  621): --- calledFromForegroundUserOrSystemProcess ? calling uid = 10011 system uid = 1000 calling userId = 0, foreground user id = 0, calling pid = 814com.android.server.InputMethodManagerService.getCurrentInputMethodSubtype(InputMethodManagerService.java:3084)



103. 103 01-01 00:20:52.450 V/ActivityManager(  621): Finish receiver: android.os.BinderProxy@22571b78



104. 104 01-01 00:20:52.450 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



105. 105 01-01 00:20:52.450 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{221b3108 u0 ReceiverList{22291410 814 com.android.systemui/10011/u0 remote:22291d30}}: BroadcastRecord{222e2420 u-1 android.intent.action.SCREEN_ON}



106. 106 01-01 00:20:52.450 I/BroadcastQueue(  621): Delivering to BroadcastFilter{221b3108 u0 ReceiverList{22291410 814 com.android.systemui/10011/u0 remote:22291d30}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



107. 107 01-01 00:20:52.450 D/ActivityManager(  621): oom: memFactor=0 last=0 allowLow=false numProcs=34 last=34



108. 108 01-01 00:20:52.450 D/ActivityManager(  621): Did OOM ADJ in 0ms



109. 109 01-01 00:20:52.480 V/KeyguardServiceDelegate(  621): **** SHOWN CALLED ****



110. 110 01-01 00:20:52.480 I/WindowManager(  621): Lock screen displayed!



111. 111 01-01 00:20:52.490 V/ActivityManager(  621): Finish receiver: android.os.BinderProxy@22291d30



112. 112 01-01 00:20:52.490 V/BroadcastQueue(  621): processNextBroadcast [foreground]: 0 broadcasts, 1 ordered broadcasts



113. 113 01-01 00:20:52.490 V/BroadcastQueue(  621): Delivering ordered [foreground] to registered BroadcastFilter{221529d8 u0 ReceiverList{21f8a0a8 621 system/1000/u0 local:21f8b640}}: BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



114. 114 01-01 00:20:52.490 I/BroadcastQueue(  621): Delivering to BroadcastFilter{221529d8 u0 ReceiverList{21f8a0a8 621 system/1000/u0 local:21f8b640}} (seq=-1): BroadcastRecord{222e2420 u-1android.intent.action.SCREEN_ON}



115. 115 01-01 00:20:52.490 V/ActivityManager(  621): Finish receiver: android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@21f8b640



116. 116 01-01 00:20:52.490       



https://www.xamrdz.com/mobile/4u61925748.html

相关文章: