Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the structure and code distribution of Bluedroid

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is about the structure and code distribution of Bluedroid. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Android development's introduction to 4.3Bluetooth:

Android provides BlueDroid as the default protocol stack, and BlueDroid is divided into two parts:

1. Bluetooth Embedded System (BTE), which implements the core function of BT.

2. Bluetooth Application Layer (BTA), which is used to interact with android framework layer.

BT system services interact with BT stack through JNI and with applications through Binder IPC communication. This system service also provides RD with access to different BT profiles; icons below to show a general structure of BT stack:

1. Application Framework

The code of this layer mainly uses android.bluetooth APIS and bluetooth hardware to interact. That is, to call the bluetooth process through the Binder IPC mechanism

The code is under framework/base/core/java/android.bluetooth/.

Such as the connection to A2DP: the connect (Bluetoothevice) method in framework/base/core/java/android.bluetooth/BluetoothA2dp.java.

12 3 4 5 6 7 8 91011121314public boolean connect (BluetoothDevice device) {if (DBG) log ("connect (" + device + ")); if (mService! = null & & isEnabled () & & isValidDevice (device)) {try {return mService.connect (device);} catch (RemoteException e) {Log.e (TAG," Stack: "+ Log.getStackTraceString (new Throwable () Return false;} if (mService = = null) Log.w (TAG, "Proxy not attached to service"); return false;}

Through Binder IPC communication mechanism, call to packages/apps/Bluetooth/src/com.android.bluetooth.a2dp/A2dpService.java 's next internal private class

A2dpService inherits from ProfileService, while ProfileService inherits from Service.

The connect (BluetoothDevice) method of private static class BluetoothA2dpBinder extends IBluetoothA2dp.Stub {}.

12345public boolean connect (BluetoothDevice device) {A2dpService service = getService (); if (service = = null) return false; return service.connect (device);}

Then call the connect (BluetoothDevice) method of A2dpService.

12 3 4 5 6 7 8 91011121314151617public boolean connect (BluetoothDevice device) {enforceCallingOrSelfPermission (BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); if (getPriority (device) = = BluetoothProfile.PRIORITY_OFF) {return false;} int connectionState = mStateMachine.getConnectionState (device); if (connectionState = = BluetoothProfile.STATE_CONNECTED | | connectionState = = BluetoothProfile.STATE_CONNECTING) {return false } mStateMachine.sendMessage (A2dpStateMachine.CONNECT, device); return true;}

This procedure is the calling procedure of Bluetooth Application Framework and Bluetooth Process.

II. Bluetooth System service

Bluetooth System service is located under packages/apps/Bluetooth, which is packaged into an android app package and implements BT service at the android framework layer

And all kinds of profile. BT app is called to the HAL layer through JNI.

The connect method of A2dpService sends a message of StateMachine.sendMessage (A2dpStateMachine.CONNECT, device), and this message is received by the processMessage (Message) method of the A2dpStateMachine object:

12 3 4 5 6 7 8 910111213141516171819case CONNECT: BluetoothDevice device = (BluetoothDevice) message.obj; broadcastConnectionState (device, BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_DISCONNECTED) If (! connectA2dpNative (getByteAddress (device) {broadcastConnectionState (device, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING); break;} synchronized (A2dpStateMachine.this) {mTargetDevice = device; transitionTo (mPending) } / / TODO (BT) remove CONNECT_TIMEOUT when the stack / / sends back events consistently sendMessageDelayed (CONNECT_TIMEOUT, 30000); break

The most important sentence: connectA2dpNative (getByteAddress (device))

It will be called to Native via JNI

Private native boolean connectA2dpNative (byte [] address)

III. JNI

The JNI code associated with android.bluetooth is located under packages/apps/bluetooth/jni, and the code of JNI is called to the HAL layer, and when it is convinced that some BT operations are triggered, it will be transferred from the HAL

Get some callbacks. For example, when a BT device is discovered.

Going back to the example of an A2dp connection, BT System Service is called into com_android_bluetooth_a2dp.cpp through JNI:

12 3 4 5 6 7 8 9101112131415161718192021static jboolean connectA2dpNative (JNIEnv * env, jobject object, jbyteArray address) {jbyte * addr;bt_bdaddr_t * btAddr;bt_status_t status;ALOGI ("% s: sBluetoothA2dpInterface:% p", _ FUNCTION__, sBluetoothA2dpInterface); if (! sBluetoothA2dpInterface) return JNI_FALSE;addr = env- > GetByteArrayElements (address, NULL); btAddr = (bt_bdaddr_t *) addr;if (! addr) {jniThrowIOException (env, EINVAL); return JNI_FALSE } if ((status = sBluetoothA2dpInterface- > connect ((bt_bdaddr_t *) addr))! = BT_STATUS_SUCCESS) {ALOGE ("Failed HF connection, status:% d", status);} env- > ReleaseByteArrayElements (address, addr, 0); return (status = = BT_STATUS_SUCCESS)? JNI_TRUE: JNI_FALSE;}

The key code is: status = sBluetoothA2dpInterface- > connect ((bt_bdaddr_t *) addr)

This sBluetoothA2dpInterface structure object is obtained in the initNative (JNIEnv * env, jobject object) method.

12345if ((sBluetoothA2dpInterface = (btav_interface_t *) btInf- > get_profile_interface (BT_PROFILE_ADVANCED_AUDIO_ID)) = = NULL) {ALOGE ("Failed to get Bluetooth A2DP Interface"); return;}

IV. HAL

The hardware abstraction layer defines standard interfaces for android.bluetooth APIs and BT process calls, and you must implement these interfaces to make your BT hardware functions work properly. BT HAL's

The header file of is located in the hardware/libhardware/include/hardware/bluetooth.h and hardware/libhardware/include/hardware/bt_*.h files.

SBluetoothA2dpInterface in JNI is a btav_interface_t structure, which is located in hardware/libhardware/include/hardware/bt_av.h and is defined as:

12 3 4 5 6 7 8 910111213141516171819typedef struct {size_t size; bt_status_t (* init) (btav_callbacks_t* callbacks); bt_status_t (* connect) (bt_bdaddr_t * bd_addr); bt_status_t (* disconnect) (bt_bdaddr_t * bd_addr); void (* cleanup) (void);} btav_interface_t

5. BT stack

As the default BT stack, (4.2was previously used by bluez as the protocol stack)

The code is under external/bluetooth/bluedroid, which implements the generic BT HAL and can also be customized by extending and changing the configuration.

The connection to A2dp calls the connect method of external/bluetooth/bluedroid/btif/src/btif_av.c.

1234567static bt_status_t connect (bt_bdaddr_t * bd_addr) {BTIF_TRACE_EVENT1 ("% s", _ _ FUNCTION__); CHECK_BTAV_INIT (); return btif_queue_connect (UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);}

VI. Vendor extension

To track the addition of custom extensions and a HCI layer, you can create a libbt-vendor module and specify these components.

The above is the structure and code distribution of Bluedroid, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report