Android蓝牙使能流程图(Android Bluetooth enable) -- java & jni层

原创 专注于无线通信的蓬勃 2023-03-26 11:44

在介绍Android enable流程之前,我们首先来看下Android的流程图


以上图示是android8以上的,主要跟Android8之前的架构在于Bluetooth stack跟vendor层之间增加一层HIDL,主要是抽象硬件层的作用

OK,回归主题,我们来看下Android的enable流程,首先上一张架构图


另外声明下:我们做了一些列的蓝牙教程(包括视频)。我们会以连载的方式持续更新,内容如下:

本专栏文章我们会以连载的方式持续更新,本专栏计划更新内容如下:


第一篇:蓝牙综合介绍 ,主要介绍蓝牙的一些概念,产生背景,发展轨迹,市面蓝牙介绍,以及蓝牙开发板介绍。

第二篇:Transport层介绍,主要介绍蓝牙协议栈跟蓝牙芯片之前的硬件传输协议,比如基于UART的H4,H5,BCSP,基于USB的H2等

第三篇:传统蓝牙controller介绍,主要介绍传统蓝牙芯片的介绍,包括射频层(RF),基带层(baseband),链路管理层(LMP)等

第四篇:传统蓝牙host介绍,主要介绍传统蓝牙的协议栈,比如HCI,L2CAP,SDP,RFCOMM,HFP,SPP,HID,AVDTP,AVCTP,A2DP,AVRCP,OBEX,PBAP,MAP等等一系列的协议吧。

第五篇:低功耗蓝牙controller介绍,主要介绍低功耗蓝牙芯片,包括物理层(PHY),链路层(LL)

第六篇:低功耗蓝牙host介绍,低功耗蓝牙协议栈的介绍,包括HCI,L2CAP,ATT,GATT,SM等

第七篇:蓝牙芯片介绍,主要介绍一些蓝牙芯片的初始化流程,基于HCI vendor command的扩展

第八篇:附录,主要介绍以上常用名词的介绍以及一些特殊流程的介绍等。

另外,开发板如下所示,对于想学习蓝牙协议栈的最好人手一套。以便更好的学习蓝牙协议栈,相信我,学完这一套视频你将拥有修改任何协议栈的能力(比如Linux下的bluez,Android下的bluedroid)。

-------------------------------------------------------------------------------------------------------------------------

蓝牙视频教程(跟韦东山老师合作)https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-22329603914.10.77201fc98qgCWh&id=679276693032

蓝牙交流扣扣群:970324688

Github代码:GitHub - sj15712795029/bluetooth_stack: 这是一个开源的双模蓝牙协议栈(bluetooth.stack)(btstack),可以运行在STM32,Linux.,包含HCI,L2CAP,SDP,RFCOMM,HFP,SPP,A2DP,AVRCP,AVDTP,AVCTP,OBEX,PBAP等协议,后续会继续维护,以达到商用的目的

入手开发板:https://shop220811498.taobao.com/category-1542116976.htm?spm=a1z10.5-c-s.w4010-22329603913.7.39ca7dbe2EA0K3&search=y&catName=%C0%B6%D1%C0%BF%AA%B7%A2%B0%E5#bd


--------------------------------------------------------------------------------------------------------------------------

步骤1:开机会启动SystemServer类的main函数

在Android系统启动过程中,SystemServer是第一个被启动的进程。当Android设备被开机后,内核会创建一个名为init的进程,init进程会启动Zygote进程,Zygote进程会启动SystemServer进程。因此,SystemServer是整个Android系统启动的核心进程,它负责初始化并启动大部分系统服务和应用程序,是整个Android系统的主要启动入口。

具体来说,SystemServer主要完成以下几个任务:

  • 启动和初始化系统服务:SystemServer会启动和初始化大部分系统服务,例如ActivityManagerService、WindowManagerService、PackageManagerService、PowerManagerService等等。这些服务会在启动过程中被创建并注册到系统服务中心,供其他应用程序和服务调用。

  • 启动和初始化核心应用程序:SystemServer会启动和初始化Android系统中的核心应用程序,例如SystemUI、Settings等等。这些应用程序会在启动过程中被创建并运行,提供各种用户界面和功能。

  • 加载和初始化系统属性:SystemServer会加载和初始化/system/build.prop文件中定义的系统属性,例如设备型号、厂商信息等等。这些属性可以在系统运行时被访问和修改。

  • 启动Adb守护进程:SystemServer会启动Adb守护进程,使得开发者可以通过adb工具来访问设备。

总之,SystemServer是整个Android系统启动过程中的核心进程,负责启动和初始化大部分系统服务和应用程序,为整个系统的运行提供基础支持。SystemServer的启动时间通常在内核启动之后的几秒钟内,具体时间取决于设备的硬件性能和系统配置。

/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
    new SystemServer().run();
}

SystemServer().run();执行startOtherServices(t);

然后startOtherServices(t);执行如下代码,开启Bluetooth service类

private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
    ...
    if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
        Slog.i(TAG, "No Bluetooth Service (factory test)");
    } else if (!context.getPackageManager().hasSystemFeature
            (PackageManager.FEATURE_BLUETOOTH)) {
        Slog.i(TAG, "No Bluetooth Service (Bluetooth Hardware Not Present)");
    } else {
        t.traceBegin("StartBluetoothService");
        mSystemServiceManager.startService(BluetoothService.class);
        t.traceEnd();
    }
    ...
}

步骤2:BluetoothService构造函数中执行创建BluetoothManagerService

mBluetoothManagerService = new BluetoothManagerService(context);

BluetoothManagerService(Context context) {

        Slog.e(TAG, "new BluetoothManagerService");

        // BluetoothManagerService的私有类,主要用于处理一些message
        mHandler = new BluetoothHandler(IoThread.get().getLooper()); 

        mContext = context;

        mWirelessConsentRequired = context.getResources()
                .getBoolean(com.android.internal.R.bool.config_wirelessConsentRequired);

        mCrashes = 0;
        mBluetooth = null;
        mBluetoothBinder = null;
        mBluetoothGatt = null;
        mBinding = false;
        mUnbinding = false;
        mEnable = false;
        mState = BluetoothAdapter.STATE_OFF;
        mQuietEnableExternal = false;
        mEnableExternal = false;
        mAddress = null;
        mName = null;
        mErrorRecoveryRetryCounter = 0;
        mContentResolver = context.getContentResolver();
        // Observe BLE scan only mode settings change.
        registerForBleScanModeChange();
        mCallbacks = new RemoteCallbackList();
        mStateChangeCallbacks = new RemoteCallbackList();

        mIsHearingAidProfileSupported = context.getResources()
                .getBoolean(com.android.internal.R.bool.config_hearing_aid_profile_supported);

        // TODO: We need a more generic way to initialize the persist keys of FeatureFlagUtils
        String value = SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + FeatureFlagUtils.HEARING_AID_SETTINGS);
        if (!TextUtils.isEmpty(value)) {
            boolean isHearingAidEnabled = Boolean.parseBoolean(value);
            Log.v(TAG, "set feature flag HEARING_AID_SETTINGS to " + isHearingAidEnabled);
            FeatureFlagUtils.setEnabled(context, FeatureFlagUtils.HEARING_AID_SETTINGS, isHearingAidEnabled);
            if (isHearingAidEnabled && !mIsHearingAidProfileSupported) {
                // Overwrite to enable support by FeatureFlag
                mIsHearingAidProfileSupported = true;
            }
        }

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_BLUETOOTH_ADDRESS_CHANGED);
        filter.addAction(Intent.ACTION_SETTING_RESTORED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiver(mReceiver, filter);

        loadStoredNameAndAddress();
        if (isBluetoothPersistedStateOn()) {
            if (DBG) {
                Slog.d(TAG, "Startup: Bluetooth persisted state is ON.");
            }
            mEnableExternal = true;
        }

        String airplaneModeRadios =
                Settings.Global.getString(mContentResolver, Settings.Global.AIRPLANE_MODE_RADIOS);
        if (airplaneModeRadios == null || airplaneModeRadios.contains(
                Settings.Global.RADIO_BLUETOOTH)) {
            mBluetoothAirplaneModeListener = new BluetoothAirplaneModeListener(
                    this, IoThread.get().getLooper(), context);
        }

        int systemUiUid = -1;
        // Check if device is configured with no home screen, which implies no SystemUI.
        boolean noHome = mContext.getResources().getBoolean(R.bool.config_noHomeScreen);
        if (!noHome) {
            PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
            systemUiUid = pm.getPackageUid(pm.getSystemUiServiceComponent().getPackageName(),
                    MATCH_SYSTEM_ONLY, USER_SYSTEM);
        }
        if (systemUiUid >= 0) {
            Slog.d(TAG, "Detected SystemUiUid: " + Integer.toString(systemUiUid));
        } else {
            // Some platforms, such as wearables do not have a system ui.
            Slog.w(TAG, "Unable to resolve SystemUI's UID.");
        }
        mSystemUiUid = systemUiUid;
    }

步骤3:启动默认用户调用的方法处理

public void onSwitchUser(int userHandle) {
    if (!mInitialized) {
        initialize();
    } else {
        mBluetoothManagerService.handleOnSwitchUser(userHandle);
    }
}

onSwitchUser 是 Android 框架中的一个回调方法,用于在用户切换时接收通知。当有多个用户在同一个设备上使用时,可以使用此方法来执行一些特定于用户的操作,例如切换到其它用户时保存当前用户的状态并加载新用户的数据。

在 Android 系统中,只有在设备启用多用户功能并启用多个用户帐户时才会调用此方法。在单用户设备上,该方法不会被调用。

当一个用户从其它用户切换到当前用户时,系统会调用 onSwitchUser 方法。

当设备启动时,如果已经启用了多用户功能,则系统会自动创建一个默认的用户,并在该用户的环境下启动应用程序。此时,onSwitchUser 方法也会被调用,此时传递给方法的 userId 参数为默认用户的 ID。

备注:该方法已经在 Android 12 中被标记为过时。因此,在新的应用程序中,应该避免使用该方法,而应该考虑使用更现代的 Android 架构组件或 API 来实现多用户管理和数据保护。

private void initialize() {
    if (!mInitialized) {
        mBluetoothManagerService.handleOnBootPhase();
        mInitialized = true;
    }
}
public void handleOnBootPhase() {
    ...
    final boolean isSafeMode = mContext.getPackageManager().isSafeMode();
    if (mEnableExternal && isBluetoothPersistedStateOnBluetooth() && !isSafeMode) {
        if (DBG) {
            Slog.d(TAG, "Auto-enabling Bluetooth.");
        }
        //调用
        sendEnableMsg(mQuietEnableExternal,
                BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT,
                mContext.getPackageName());
    } 
    ....
}

步骤4:BluetoothManagerService.java处理enable

private void sendEnableMsg(boolean quietMode, int reason, String packageName) {
    mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, quietMode ? 1 : 0, 0));
    addActiveLog(reason, packageName, true);
    mLastEnabledTime = SystemClock.elapsedRealtime();
}
public void handleMessage(Message msg)
{
    switch (msg.what)
    {
        case MESSAGE_ENABLE:
            handleEnable(mQuietEnable);
            break;
    }
}
private void handleEnable(boolean quietMode)
{
    ...
    Intent i = new Intent(IBluetooth.class.getName());
    if (!doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
            UserHandle.CURRENT)) {
        mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
    } else {
        mBinding = true;
    }
    ...
}

bond后会调用这个函数onServiceConnected

private class BluetoothServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        String name = componentName.getClassName();
        if (DBG) {
            Slog.d(TAG, "BluetoothServiceConnection: " + name);
        }
        Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
        if (name.equals("com.android.bluetooth.btservice.AdapterService")) {
            msg.arg1 = SERVICE_IBLUETOOTH;
        } else if (name.equals("com.android.bluetooth.gatt.GattService")) {
            msg.arg1 = SERVICE_IBLUETOOTHGATT;
        } else {
            Slog.e(TAG, "Unknown service connected: " + name);
            return;
        }
        msg.obj = service;
        mHandler.sendMessage(msg);
    }

    public void onServiceDisconnected(ComponentName componentName) {
        // Called if we unexpectedly disconnect.
        String name = componentName.getClassName();
        if (DBG) {
            Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + name);
        }
        Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
        if (name.equals("com.android.bluetooth.btservice.AdapterService")) {
            msg.arg1 = SERVICE_IBLUETOOTH;
        } else if (name.equals("com.android.bluetooth.gatt.GattService")) {
            msg.arg1 = SERVICE_IBLUETOOTHGATT;
        } else {
            Slog.e(TAG, "Unknown service disconnected: " + name);
            return;
        }
        mHandler.sendMessage(msg);
    }
}
public void handleMessage(Message msg)
{
    switch (msg.what)
    {
        case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
            ...
            mBluetooth.registerCallback(mBluetoothCallback);
            ...
            sendBluetoothServiceUpCallback();
            ...
            mBluetooth.enable(mQuietEnable);
            break;
    }
}

步骤5:AdapterService.java处理enable

注意在里面还有一个AdapterServiceBinder的类,不是这个类下面的enable

public synchronized boolean enable(boolean quietMode) {
    // Enforce the user restriction for disallowing Bluetooth if it was set.
    if (mUserManager.hasUserRestriction(UserManager.DISALLOW_BLUETOOTH, UserHandle.SYSTEM)) {
        debugLog("enable() called when Bluetooth was disallowed");
        return false;
    }

    Log.e(TAG, "AdapterService enable");

    debugLog("enable() - Enable called with quiet mode status =  " + quietMode);
    mQuietmode = quietMode;
    mAdapterStateMachine.sendMessage(AdapterState.BLE_TURN_ON);
    return true;
}

这个会触发status machine,会调用到OffState中processMessage

public boolean processMessage(Message msg) {
    switch (msg.what) {
        case BLE_TURN_ON:
            transitionTo(mTurningBleOnState);
            break;

        default:
            infoLog("Unhandled message - " + messageString(msg.what));
            return false;
    }
    return true;
}

然后进入到TurningBleOnState类中的enter

public void enter() {
    super.enter();
    sendMessageDelayed(BLE_START_TIMEOUT, BLE_START_TIMEOUT_DELAY);
    mAdapterService.bringUpBle();
}
void bringUpBle() {
    ...
    //Start Gatt service
    setProfileServiceState(GattService.class, BluetoothAdapter.STATE_ON);
}

开启gattservice服务

另外,ProfileService的状态改变会触发AdapterService的onProfileServiceStateChanged

public void onProfileServiceStateChanged(ProfileService profile, int state) {
    if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF) {
        throw new IllegalArgumentException(BluetoothAdapter.nameForState(state));
    }
    Message m = mHandler.obtainMessage(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);
    m.obj = profile;
    m.arg1 = state;
    mHandler.sendMessage(m);
}
private void processProfileServiceStateChanged(ProfileService profile, int state) {
    switch (state) {
        case BluetoothAdapter.STATE_ON:
            if (!mRegisteredProfiles.contains(profile)) {
                Log.e(TAG, profile.getName() + " not registered (STATE_ON).");
                return;
            }
            if (mRunningProfiles.contains(profile)) {
                Log.e(TAG, profile.getName() + " already running.");
                return;
            }
            mRunningProfiles.add(profile);
            if (GattService.class.getSimpleName().equals(profile.getName())) {

                Log.e(TAG, "processProfileServiceStateChanged enableNative");

                // 这个地方就是调用libjni.so的方法,通过jni native调用
                enableNative();
            } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
                    && mRegisteredProfiles.size() == mRunningProfiles.size()) {
                mAdapterProperties.onBluetoothReady();
                updateUuids();
                setBluetoothClassFromConfig();
                initProfileServices();
                getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_LOCAL_IO_CAPS);
                getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_LOCAL_IO_CAPS_BLE);
                mAdapterStateMachine.sendMessage(AdapterState.BREDR_STARTED);
            }
            break;
        default:
            Log.e(TAG, "Unhandled profile state: " + state);
    }
}

NOTED:能够直接调用到libbluetooth_jni.so的原因是AdapterApp 类在启动的时候加载了

public class AdapterApp extends Application
{
    private static final String TAG = "BluetoothAdapterApp";
    private static final boolean DBG = false;
    //For Debugging only
    private static int sRefCount = 0;

    static {
        if (DBG) {
            Log.d(TAG, "Loading JNI Library");
        }
        System.loadLibrary("bluetooth_jni");
    }

    ...
}

然后这个动作会触发com_android_bluetooth_btservice_adapterService.cpp中的

jint JNI_OnLoad(JavaVM* jvm, void* reserved) {
    JNIEnv* e;
    int status;


    ALOGE("Bluetooth Adapter Service : loading JNI\n");
    ALOGV("Bluetooth Adapter Service : loading JNI\n");

    // Check JNI version
    if (jvm->GetEnv((void**)&e, JNI_VERSION_1_6)) {
      ALOGE("JNI version mismatch error");
      return JNI_ERR;
  }

    status = android::register_com_android_bluetooth_btservice_AdapterService(e);
    if (status < 0) { ALOGE("jni adapter service registration failure, status: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_btservice_BluetoothKeystore(e); if (status < 0) { ALOGE("jni BluetoothKeyStore registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_hfp(e); if (status < 0) { ALOGE("jni hfp registration failure, status: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_hfpclient(e); if (status < 0) { ALOGE("jni hfp client registration failure, status: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_a2dp(e); if (status < 0) { ALOGE("jni a2dp source registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_a2dp_sink(e); if (status < 0) { ALOGE("jni a2dp sink registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_avrcp_target(e); if (status < 0) { ALOGE("jni new avrcp target registration failure: %d", status); } status = android::register_com_android_bluetooth_avrcp_controller(e); if (status < 0) { ALOGE("jni avrcp controller registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_hid_host(e); if (status < 0) { ALOGE("jni hid registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_hid_device(e); if (status < 0) { ALOGE("jni hidd registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_pan(e); if (status < 0) { ALOGE("jni pan registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_gatt(e); if (status < 0) { ALOGE("jni gatt registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_sdp(e); if (status < 0) { ALOGE("jni sdp registration failure: %d", status); return JNI_ERR; } status = android::register_com_android_bluetooth_hearing_aid(e); if (status < 0) { ALOGE("jni hearing aid registration failure: %d", status); return JNI_ERR; } return JNI_VERSION_1_6; }

步骤6:bluetooth jni的enableNative处理

static jboolean enableNative(JNIEnv* env, jobject obj) {
  ALOGV("%s", __func__);

  if (!sBluetoothInterface) return JNI_FALSE;
  int ret = sBluetoothInterface->enable();
  return (ret == BT_STATUS_SUCCESS || ret == BT_STATUS_DONE) ? JNI_TRUE
                                                             : JNI_FALSE;
}

其中sBluetoothInterface 是hal接口,定义如下:

typedef struct {
    /** set to sizeof(bt_interface_t) */
    size_t size;
    /**
* Opens the interface and provides the callback routines
* to the implemenation of this interface.
* The |is_atv| flag indicates whether the local device is an Android TV
*/
    int (*init)(bt_callbacks_t* callbacks, bool is_atv);

    /** Enable Bluetooth. */
    int (*enable)(bool guest_mode);

    /** Disable Bluetooth. */
    int (*disable)(void);

    /** Closes the interface. */
    void (*cleanup)(void);

    /** Get all Bluetooth Adapter properties at init */
    int (*get_adapter_properties)(void);

    /** Get Bluetooth Adapter property of 'type' */
    int (*get_adapter_property)(bt_property_type_t type);

    /** Set Bluetooth Adapter property of 'type' */
    /* Based on the type, val shall be one of
* RawAddress or bt_bdname_t or bt_scanmode_t etc
*/
    int (*set_adapter_property)(const bt_property_t *property);

    /** Get all Remote Device properties */
    int (*get_remote_device_properties)(RawAddress *remote_addr);

    /** Get Remote Device property of 'type' */
    int (*get_remote_device_property)(RawAddress *remote_addr,
                                      bt_property_type_t type);

    /** Set Remote Device property of 'type' */
    int (*set_remote_device_property)(RawAddress *remote_addr,
                                      const bt_property_t *property);

    /** Get Remote Device's service record  for the given UUID */
    int (*get_remote_service_record)(const RawAddress& remote_addr,
                                     const bluetooth::Uuid& uuid);

    /** Start SDP to get remote services */
    int (*get_remote_services)(RawAddress *remote_addr);

    /** Start Discovery */
    int (*start_discovery)(void);

    /** Cancel Discovery */
    int (*cancel_discovery)(void);

    /** Create Bluetooth Bonding */
    int (*create_bond)(const RawAddress *bd_addr, int transport);

    /** Create Bluetooth Bond using out of band data */
    int (*create_bond_out_of_band)(const RawAddress *bd_addr, int transport,
                                   const bt_out_of_band_data_t *oob_data);

    /** Remove Bond */
    int (*remove_bond)(const RawAddress *bd_addr);

    /** Cancel Bond */
    int (*cancel_bond)(const RawAddress *bd_addr);

    /**
* Get the connection status for a given remote device.
* return value of 0 means the device is not connected,
* non-zero return status indicates an active connection.
*/
    int (*get_connection_state)(const RawAddress *bd_addr);

    /** BT Legacy PinKey Reply */
    /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
    int (*pin_reply)(const RawAddress *bd_addr, uint8_t accept,
                     uint8_t pin_len, bt_pin_code_t *pin_code);

    /** BT SSP Reply - Just Works, Numeric Comparison and Passkey
* passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &
* BT_SSP_VARIANT_CONSENT
* For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey
* shall be zero */
    int (*ssp_reply)(const RawAddress *bd_addr, bt_ssp_variant_t variant,
                     uint8_t accept, uint32_t passkey);

    /** Get Bluetooth profile interface */
    const void* (*get_profile_interface) (const char *profile_id);

    /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */
    /* Configure DUT Mode - Use this mode to enter/exit DUT mode */
    int (*dut_mode_configure)(uint8_t enable);

    /* Send any test HCI (vendor-specific) command to the controller. Must be in DUT Mode */
    int (*dut_mode_send)(uint16_t opcode, uint8_t *buf, uint8_t len);
    /** BLE Test Mode APIs */
    /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End */
    int (*le_test_mode)(uint16_t opcode, uint8_t *buf, uint8_t len);

    /** Sets the OS call-out functions that bluedroid needs for alarms and wake locks.
* This should be called immediately after a successful |init|.
*/
    int (*set_os_callouts)(bt_os_callouts_t *callouts);

    /** Read Energy info details - return value indicates BT_STATUS_SUCCESS or BT_STATUS_NOT_READY
* Success indicates that the VSC command was sent to controller
*/
    int (*read_energy_info)();

    /**
* Native support for dumpsys function
* Function is synchronous and |fd| is owned by caller.
* |arguments| are arguments which may affect the output, encoded as
* UTF-8 strings.
*/
    void (*dump)(int fd, const char **arguments);

    /**
* Clear /data/misc/bt_config.conf and erase all stored connections
*/
    int (*config_clear)(void);

    /**
* Clear (reset) the dynamic portion of the device interoperability database.
*/
    void (*interop_database_clear)(void);

    /**
* Add a new device interoperability workaround for a remote device whose
* first |len| bytes of the its device address match |addr|.
* NOTE: |feature| has to match an item defined in interop_feature_t (interop.h).
*/
    void (*interop_database_add)(uint16_t feature, const RawAddress *addr, size_t len);
} bt_interface_t;

这个enable是调用libbluetooth.so中的enable,也就是bluedroid bluetooth.cc或者android8之前的bluetooth.c

static int enable() {
    if (!interface_ready()) return BT_STATUS_NOT_READY;

    stack_manager_get_interface()->start_up_stack_async();
    return BT_STATUS_SUCCESS;
}

然后就是走bluedroid流程了,host的初始化流程可以看我协议栈的其他文章

步骤7:底层bluedroid enable成功后回调处理

bluedroid底层enable跟controller交互成功后,调用这个函数,通过HAL_CBACK回调到bt service apk的jni

static void event_signal_stack_up(UNUSED_ATTR void* context) {
  // Notify BTIF connect queue that we've brought up the stack. It's
  // now time to dispatch all the pending profile connect requests.
  btif_queue_connect_next();
  HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON);
}

思考:为什么能调用到callback呢?

是因为adapterService 服务启动的时候调用到onCreate方法,里面有

public void onCreate() {
    super.onCreate();
    debugLog("onCreate()");
    ...
    // 调用jni的方法
    initNative(isGuest(), isNiapMode(), configCompareResult, isAtvDevice);
    ...
}
static bool initNative(JNIEnv* env, jobject obj, jboolean isGuest,
                       jboolean isNiapMode, int configCompareResult,
                       jboolean isAtvDevice) {
    ALOGV("%s", __func__);

    // 注册jni的callback到bluedroid,bluedroid有回调的时候回调用到sBluetoothCallbacks
    int ret = sBluetoothInterface->init(
      &sBluetoothCallbacks, isGuest == JNI_TRUE ? 1 : 0,
      isNiapMode == JNI_TRUE ? 1 : 0, configCompareResult,
      isAtvDevice == JNI_TRUE ? 1 : 0);
    if (ret != BT_STATUS_SUCCESS) {
      ALOGE("Error while setting the callbacks: %d\n", ret);
      sBluetoothInterface = NULL;
      return JNI_FALSE;
  }

}

好了,回归会提,如果bluedroid enable成功后,会调用到sBluetoothCallbacks的adapter_state_change_callback函数

static void adapter_state_change_callback(bt_state_t status) {
    ALOGE("Bluetooth adapter_state_change_callback:%d\n",status);
    
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;
  ALOGV("%s: Status is: %d", __func__, status);

    // 通过jni的回调方法调用到AdapterService的stateChangeCallback
  sCallbackEnv->CallVoidMethod(sJniCallbacksObj, method_stateChangeCallback,
                               (jint)status);
}

步骤8:bt service的处理enable成功后的回调过程

void stateChangeCallback(int status) {
    Log.e(TAG, "stateChangeCallback:"+status);

    if (status == AbstractionLayer.BT_STATE_OFF) {
        debugLog("stateChangeCallback: disableNative() completed");
        mAdapterStateMachine.sendMessage(AdapterState.BLE_STOPPED);
    } else if (status == AbstractionLayer.BT_STATE_ON) {
        mAdapterStateMachine.sendMessage(AdapterState.BLE_STARTED);
    } else {
        Log.e(TAG, "Incorrect status " + status + " in stateChangeCallback");
    }
}

此时进入这个类中的processMessage处理

public boolean processMessage(Message msg) {
    switch (msg.what) {
        case BLE_STARTED:
            transitionTo(mBleOnState);
            break;

        case BLE_START_TIMEOUT:
            errorLog(messageString(msg.what));
            transitionTo(mTurningBleOffState);
            break;

        default:
            infoLog("Unhandled message - " + messageString(msg.what));
            return false;
    }
    return true;
}

迁徙到mBleOnState状态,然后处理,另外状态概念,会触发AdapterService.java中的updateAdapterState

void updateAdapterState(int prevState, int newState) {
    mAdapterProperties.setState(newState);
    invalidateBluetoothGetStateCache();
    if (mCallbacks != null) {
        int n = mCallbacks.beginBroadcast();
        debugLog("updateAdapterState() - Broadcasting state " + BluetoothAdapter.nameForState(
                newState) + " to " + n + " receivers.");
        for (int i = 0; i < n; i++) { try { mCallbacks.getBroadcastItem(i).onBluetoothStateChange(prevState, newState); } catch (RemoteException e) { debugLog("updateAdapterState() - Callback #" + i + " failed (" + e + ")"); } } mCallbacks.finishBroadcast(); } .... }
private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
    @Override
    public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
        Message msg =
                mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE, prevState, newState);
        mHandler.sendMessage(msg);
    }
};

case处理

case MESSAGE_BLUETOOTH_STATE_CHANGE: {


    ....
    bluetoothStateChangeHandler(prevState, newState);

    break;
}
private void bluetoothStateChangeHandler(int prevState, int newState) {
    
    if (newState == BluetoothAdapter.STATE_BLE_ON || newState == BluetoothAdapter.STATE_OFF) {
            .......
            continueFromBleOnState();
    }
 
}
private void continueFromBleOnState() {
    ...
    mBluetooth.onLeServiceUp();
    ...
}
public void onLeServiceUp() {
    AdapterService service = getService();
    if (service == null || !callerIsSystemOrActiveUser(TAG, "onLeServiceUp")) {
        return;
    }

    enforceBluetoothPrivilegedPermission(service);

    service.mAdapterStateMachine.sendMessage(AdapterState.USER_TURN_ON);
}
private class BleOnState extends BaseAdapterState {

    @Override
    int getStateValue() {
        return BluetoothAdapter.STATE_BLE_ON;
    }

    @Override
    public boolean processMessage(Message msg) {
        switch (msg.what) {
            case USER_TURN_ON:
                transitionTo(mTurningOnState);
                break;

            case BLE_TURN_OFF:
                transitionTo(mTurningBleOffState);
                break;

            default:
                infoLog("Unhandled message - " + messageString(msg.what));
                return false;
        }
        return true;
    }
}

然后进入到TurningOnState class的enter处理

public void enter() {
    super.enter();
    sendMessageDelayed(BREDR_START_TIMEOUT, BREDR_START_TIMEOUT_DELAY);
    mAdapterService.startProfileServices();
}

步骤9:开启所有profile的service

void startProfileServices() {
    ...
    setAllProfileServiceStates(supportedProfileServices, BluetoothAdapter.STATE_ON);
    
}
private static final ProfileConfig[] PROFILE_SERVICES_AND_FLAGS = {
            new ProfileConfig(HeadsetService.class, R.bool.profile_supported_hs_hfp,
                    (1 << BluetoothProfile.HEADSET)), new ProfileConfig(A2dpService.class, R.bool.profile_supported_a2dp, (1 << BluetoothProfile.A2DP)), new ProfileConfig(A2dpSinkService.class, R.bool.profile_supported_a2dp_sink, (1 << BluetoothProfile.A2DP_SINK)), new ProfileConfig(HidHostService.class, R.bool.profile_supported_hid_host, (1 << BluetoothProfile.HID_HOST)), new ProfileConfig(PanService.class, R.bool.profile_supported_pan, (1 << BluetoothProfile.PAN)), new ProfileConfig(GattService.class, R.bool.profile_supported_gatt, (1 << BluetoothProfile.GATT)), new ProfileConfig(BluetoothMapService.class, R.bool.profile_supported_map, (1 << BluetoothProfile.MAP)), new ProfileConfig(HeadsetClientService.class, R.bool.profile_supported_hfpclient, (1 << BluetoothProfile.HEADSET_CLIENT)), new ProfileConfig(AvrcpTargetService.class, R.bool.profile_supported_avrcp_target, (1 << BluetoothProfile.AVRCP)), new ProfileConfig(AvrcpControllerService.class, R.bool.profile_supported_avrcp_controller, (1 << BluetoothProfile.AVRCP_CONTROLLER)), new ProfileConfig(SapService.class, R.bool.profile_supported_sap, (1 << BluetoothProfile.SAP)), new ProfileConfig(PbapClientService.class, R.bool.profile_supported_pbapclient, (1 << BluetoothProfile.PBAP_CLIENT)), new ProfileConfig(MapClientService.class, R.bool.profile_supported_mapmce, (1 << BluetoothProfile.MAP_CLIENT)), new ProfileConfig(HidDeviceService.class, R.bool.profile_supported_hid_device, (1 << BluetoothProfile.HID_DEVICE)), new ProfileConfig(BluetoothOppService.class, R.bool.profile_supported_opp, (1 << BluetoothProfile.OPP)), new ProfileConfig(BluetoothPbapService.class, R.bool.profile_supported_pbap, (1 << BluetoothProfile.PBAP)), new ProfileConfig(HearingAidService.class, com.android.internal.R.bool.config_hearing_aid_profile_supported, (1 << BluetoothProfile.HEARING_AID)) };

因为我们之前在前面说过,开启成功后就会调用profileService change的消息,所以如下

private void processProfileServiceStateChanged(ProfileService profile, int state) {
            switch (state) {
                case BluetoothAdapter.STATE_ON:
                        ........
                    if (GattService.class.getSimpleName().equals(profile.getName())) {
                        enableNative();
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
                            && mRegisteredProfiles.size() == mRunningProfiles.size()) {  //所有ProfileService都启动完毕
                       .......
                        mAdapterStateMachine.sendMessage(AdapterState.BREDR_STARTED);
                    }
                    break;
public boolean processMessage(Message msg) {
    switch (msg.what) {
        case BREDR_STARTED:
            transitionTo(mOnState);
            break;

    }
    return true;
}
private void bluetoothStateChangeHandler(int prevState, int newState) {
    ...
    sendBluetoothStateCallback(isUp); //广播发出数据
    sendBleStateChanged(prevState, newState);
    ...
}
专注于无线通信的蓬勃 朝气蓬勃——不积跬步 无以至千里, 不积小流 无以成江海
评论
  • 自动化已成为现代制造业的基石,而驱动隔离器作为关键组件,在提升效率、精度和可靠性方面起到了不可或缺的作用。随着工业技术不断革新,驱动隔离器正助力自动化生产设备适应新兴趋势,并推动行业未来的发展。本文将探讨自动化的核心趋势及驱动隔离器在其中的重要角色。自动化领域的新兴趋势智能工厂的崛起智能工厂已成为自动化生产的新标杆。通过结合物联网(IoT)、人工智能(AI)和机器学习(ML),智能工厂实现了实时监控和动态决策。驱动隔离器在其中至关重要,它确保了传感器、执行器和控制单元之间的信号完整性,同时提供高
    腾恩科技-彭工 2025-01-03 16:28 130浏览
  • 在快速发展的能源领域,发电厂是发电的支柱,效率和安全性至关重要。在这种背景下,国产数字隔离器已成为现代化和优化发电厂运营的重要组成部分。本文探讨了这些设备在提高性能方面的重要性,同时展示了中国在生产可靠且具有成本效益的数字隔离器方面的进步。什么是数字隔离器?数字隔离器充当屏障,在电气上将系统的不同部分隔离开来,同时允许无缝数据传输。在发电厂中,它们保护敏感的控制电路免受高压尖峰的影响,确保准确的信号处理,并在恶劣条件下保持系统完整性。中国国产数字隔离器经历了重大创新,在许多方面达到甚至超过了全球
    克里雅半导体科技 2025-01-03 16:10 84浏览
  • 物联网(IoT)的快速发展彻底改变了从智能家居到工业自动化等各个行业。由于物联网系统需要高效、可靠且紧凑的组件来处理众多传感器、执行器和通信设备,国产固态继电器(SSR)已成为满足中国这些需求的关键解决方案。本文探讨了国产SSR如何满足物联网应用的需求,重点介绍了它们的优势、技术能力以及在现实场景中的应用。了解物联网中的固态继电器固态继电器是一种电子开关设备,它使用半导体而不是机械触点来控制负载。与传统的机械继电器不同,固态继电器具有以下优势:快速切换:确保精确快速的响应,这对于实时物联网系统至
    克里雅半导体科技 2025-01-03 16:11 129浏览
  • 影像质量应用于多个不同领域,无论是在娱乐、医疗或工业应用中,高质量的影像都是决策的关键基础。清晰的影像不仅能提升观看体验,还能保证关键细节的准确传达,例如:在医学影像中,它对诊断结果有着直接的影响!不仅如此,影像质量还影响了:▶ 压缩技术▶ 存储需求▶ 传输效率随着技术进步,影像质量的标准不断提高,对于研究与开发领域,理解并提升影像质量已成为不可忽视的重要课题。在图像处理的过程中,硬件与软件除了各自扮演着不可或缺的基础角色,有效地协作能够确保图像处理过程既高效又具有优异的质量。软硬件各扮演了什么
    百佳泰测试实验室 2025-01-03 10:39 87浏览
  • 光耦合器,也称为光隔离器,是一种利用光在两个隔离电路之间传输电信号的组件。在医疗领域,确保患者安全和设备可靠性至关重要。在众多有助于医疗设备安全性和效率的组件中,光耦合器起着至关重要的作用。这些紧凑型设备经常被忽视,但对于隔离高压和防止敏感医疗设备中的电气危害却是必不可少的。本文深入探讨了光耦合器的功能、其在医疗应用中的重要性以及其实际使用示例。什么是光耦合器?它通常由以下部分组成:LED(发光二极管):将电信号转换为光。光电探测器(例如光电晶体管):检测光并将其转换回电信号。这种布置确保输入和
    腾恩科技-彭工 2025-01-03 16:27 123浏览
  • 前言近年来,随着汽车工业的快速发展,尤其是新能源汽车与智能汽车领域的崛起,汽车安全标准和认证要求日益严格,应用范围愈加广泛。ISO 26262和ISO 21448作为两个重要的汽车安全标准,它们在“系统安全”中扮演的角色各自不同,但又有一定交集。在智能网联汽车的高级辅助驾驶系统(ADAS)应用中,理解这两个标准的区别及其相互关系,对于保障车辆的安全性至关重要。ISO 26262:汽车功能安全的基石如图2.1所示,ISO 26262对“功能安全”的定义解释为:不存在由于电子/电气系统失效引起的危害
    广电计量 2025-01-02 17:18 199浏览
  • Matter加持:新世代串流装置如何改变智能家居体验?随着现在智能家庭快速成长,串流装置(Streaming Device,以下简称Streaming Device)除了提供更卓越的影音体验,越来越多厂商开始推出支持Matter标准的串流产品,使其能作为智能家庭中枢,连结多种智能家电。消费者可以透过Matter的功能执行多样化功能,例如:开关灯、控制窗帘、对讲机开门,以及操作所有支持Matter的智能家电。此外,再搭配语音遥控器与语音助理,打造出一个更加智能、便捷的居家生活。支持Matter协议
    百佳泰测试实验室 2025-01-03 10:29 105浏览
  • 本文继续介绍Linux系统查看硬件配置及常用调试命令,方便开发者快速了解开发板硬件信息及进行相关调试。触觉智能RK3562开发板演示,搭载4核A53处理器,主频高达2.0GHz;内置独立1Tops算力NPU,可应用于物联网网关、平板电脑、智能家居、教育电子、工业显示与控制等行业。查看系统版本信息查看操作系统版本信息root@ido:/# cat /etc/*releaseDISTRIB_ID=UbuntuDISTRIB_RELEASE=20.04DISTRIB_CODENAME=focalDIS
    Industio_触觉智能 2025-01-03 11:37 102浏览
  • 车身域是指负责管理和控制汽车车身相关功能的一个功能域,在汽车域控系统中起着至关重要的作用。它涵盖了车门、车窗、车灯、雨刮器等各种与车身相关的功能模块。与汽车电子电气架构升级相一致,车身域发展亦可以划分为三个阶段,功能集成愈加丰富:第一阶段为分布式架构:对应BCM车身控制模块,包含灯光、雨刮、门窗等传统车身控制功能。第二阶段为域集中架构:对应BDC/CEM域控制器,在BCM基础上集成网关、PEPS等。第三阶段为SOA理念下的中央集中架构:VIU/ZCU区域控制器,在BDC/CEM基础上集成VCU、
    北汇信息 2025-01-03 16:01 137浏览
  • 【工程师故事】+半年的经历依然忧伤,带着焦虑和绝望  对于一个企业来说,赚钱才是第一位的,对于一个人来说,赚钱也是第一位的。因为企业要活下去,因为个人也要活下去。企业打不了倒闭。个人还是要吃饭的。企业倒闭了,打不了从头再来。个人失业了,面对的不仅是房贷车贷和教育,还有找工作的焦虑。企业说,一个公司倒闭了,说明不了什么,这是正常的一个现象。个人说,一个中年男人失业了,面对的压力太大了,焦虑会摧毁你的一切。企业说,是个公司倒闭了,也不是什么大的问题,只不过是这些公司经营有问题吧。
    curton 2025-01-02 23:08 237浏览
  • 在测试XTS时会遇到修改产品属性、SElinux权限、等一些内容,修改源码再编译很费时。今天为大家介绍一个便捷的方法,让OpenHarmony通过挂载镜像来修改镜像内容!触觉智能Purple Pi OH鸿蒙开发板演示。搭载了瑞芯微RK3566四核处理器,树莓派卡片电脑设计,支持开源鸿蒙OpenHarmony3.2-5.0系统,适合鸿蒙开发入门学习。挂载镜像首先,将要修改内容的镜像传入虚拟机当中,并创建一个要挂载镜像的文件夹,如下图:之后通过挂载命令将system.img镜像挂载到sys
    Industio_触觉智能 2025-01-03 11:39 90浏览
我要评论
0
点击右上角,分享到朋友圈 我知道啦
请使用浏览器分享功能 我知道啦