Android14 蓝牙 BluetoothService 启动和相关代码介绍

Android14 蓝牙 BluetoothService 启动和相关代码

文章目录

一、前言

蓝牙开关和使能开发主要用到:BluetoothService、BluetoothManagerService、BluetoothManager、BluetoothAdapter 这几个系统相关类。

某个蓝牙的配对、连接、断开 使用的是 BluetoothDevice 对象。

蓝牙开关状态不记忆或者打开异常就可以看看BluetoothManagerService的日志,

里面有打开关闭相关过程日志和时间点,这个对问题分析有一定的帮助。

本文主要介绍一下 framework 相关的几个类,

对于蓝牙应用的开发和分析解决系统上层的需求和简单问题有一定的作用,有兴趣的可以收藏看看。

本文的代码和日志是基于Android14 的设备上的。

二、代码分析介绍

1、蓝牙 BluetoothService 启动和相关代码

(1)蓝牙服务相关的有几个类有:

BluetoothService、BluetoothManagerService、BluetoothManager、BluetoothAdapter

估计没几个人熟悉这个关系的,这里简单梳理一下。

(2)几个蓝牙类对象的关系

这里先公布一下简单关系:

1、BluetoothService 是上层蓝牙最开始的服务,是在SystemServer 拉起系统其他应用的时候拉起的

2、BluetoothManagerService 是BluetoothService 创建的时候创建的服务服务对象
具体实现是在 BluetoothManagerService 里面

3、BluetoothManager 是暴露的蓝牙Manager,但是这个Manager并不是绑定 BluetoothManagerService;
BluetoothManager 只有简单的几个暴露方法,里面就有获取BluetoothAdapter对象的方法 getAdapter()

4、BluetoothAdapter 是才是操作蓝牙的主要功能暴露类
BluetoothAdapter的大部分实现都是调用到 BluetoothManagerService 里面的。

蓝牙实现的功能基本都是要System api,都是要系统应用正常才能调用到相关实现。

BluetoothManager 和 BluetoothAdapter 都是应用能正常获取到的对象。

所以看Settings那些系统应用都是直接获取和使用的 BluetoothAdapter 进行蓝牙开关,设置蓝牙名称,获取蓝牙状态等实现。

上面只是对这几个系统中蓝牙的控制类有一定的了解,想要更深入的了解还是要看看代码。

2、几个蓝牙类对象的主要关联代码

具体代码关联:

(1)SystemServer.java

framework\base\services\java\com\android\server\SystemServer.java

/**
 * Entry point to {@code system_server}.
 */
public final class SystemServer implements Dumpable {

    private static final String TAG = "SystemServer";
 
    //蓝牙服务字符串定义
     private static final String BLUETOOTH_SERVICE_CLASS =
            "com.android.server.bluetooth.BluetoothService";
    
    //启动其他服务:蓝牙,手机网络、输入法等系统服务都是这里
    private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
        t.traceBegin("startOtherServices");

            //启动蓝牙服务
            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 {  //底层hal 没问题才启动上层蓝牙服务
                t.traceBegin("StartBluetoothService");
                mSystemServiceManager.startServiceFromJar(BLUETOOTH_SERVICE_CLASS,
                    BLUETOOTH_APEX_SERVICE_JAR_PATH);
                t.traceEnd();
            }
        。。。
    }

}

SystemServer.java 这个类系统开发的都比较熟悉了,可以拉起各种上层服务的类,执行相应的初始化;

如果是定义的服务类要比较早拉起来,可以加入到这里。

(2)BluetoothService.kt

package\modules\Bluetooth\service\src\com\android\server\bluetooth\BluetoothService.kt

class BluetoothService(context: Context) : SystemService(context) {
    private val mBluetoothManagerService = BluetoothManagerService(context)
    private var mInitialized = false

    private fun initialize() {
        if (!mInitialized) {
            mBluetoothManagerService.handleOnBootPhase() //初始化
            mInitialized = true
        }
    }

    override fun onStart() {}

    override fun onBootPhase(phase: Int) {
        if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
            publishBinderService(
                BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE,
                mBluetoothManagerService
            )
        }
    }

    override fun onUserStarting(user: TargetUser) {
        if (!UserManager.isHeadlessSystemUserMode()) {
            initialize()
        }
    }
}

BluetoothService 怎么使用kotlin写的?

这个是Android14 才这样写的,Android13 上看了下还是 BluetoothService.java

不过这个没啥影响,只有几十行代码,没啥打印,

理解起来也是比较简单的,主要作用就是在系统起来后拉起 BluetoothManagerService。

所以到这里你就明白了,BluetoothManagerService 才是重头戏,BluetoothService只是一个简单的架子。

(3)BluetoothManagerService.java

package\modules\Bluetooth\service\src\com\android\server\bluetooth\BluetoothManagerService.java

public class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final String TAG = "BluetoothManagerService";
    private static final boolean DBG = true;

    BluetoothManagerService(Context context) {
    //基础设置,广播监听等
    }

    //初始化
    public void handleOnBootPhase() {
        if (DBG) {
            Log.d(TAG, "Bluetooth boot completed");
        }
    }
    
    //获取蓝牙是否打开
    public boolean isEnabled() {
        return getState() == BluetoothAdapter.STATE_ON;
    }

    //打开蓝牙
    public boolean enable(AttributionSource attributionSource) throws RemoteException {
    
        
    }
    
    //关闭蓝牙,persist 表示是否记忆蓝牙状态,普通蓝牙关闭都是记忆状态
    public boolean disable(AttributionSource attributionSource, boolean persist)
            throws RemoteException {

            if (persist) {
                persistBluetoothSetting(BLUETOOTH_OFF);
            }
    
    }
    
    。。。
}

其实蓝牙功能使用的就上面几个接口方法,还有一些是gatt 低功耗的判断。

看起来实际功能的类也不多,主要是控制蓝牙开关的。

(4)BluetoothManager.java

package\modules\Bluetooth\framework\java\android\bluetooth\BluetoothManager.java

/**
 * High level manager used to obtain an instance of an {@link BluetoothAdapter}
 * and to conduct overall Bluetooth Management.
 * @see Context#getSystemService
 * @see BluetoothAdapter#getDefaultAdapter()
 */
@SystemService(Context.BLUETOOTH_SERVICE)
@RequiresFeature(PackageManager.FEATURE_BLUETOOTH)
public final class BluetoothManager {

    /**
     * @hide , 1、私有的,需要SystemService 方式获取
     */
    public BluetoothManager(Context context) {
        mAttributionSource = (context != null) ? context.getAttributionSource() :
                AttributionSource.myAttributionSource();
        mAdapter = BluetoothAdapter.createAdapter(mAttributionSource);
    }

    //2、获取BluetoothAdapter,进行蓝牙开关控制等功能
    @RequiresNoPermission
    public BluetoothAdapter getAdapter() {
        return mAdapter;
    }

    //3、获取某个蓝牙对象的连接状态
    public int getConnectionState(BluetoothDevice device, int profile) {
        if (DBG) Log.d(TAG, "getConnectionState()");

        List<BluetoothDevice> connectedDevices = getConnectedDevices(profile);
        for (BluetoothDevice connectedDevice : connectedDevices) {
            if (device.equals(connectedDevice)) {
                return BluetoothProfile.STATE_CONNECTED;
            }
        }

        return BluetoothProfile.STATE_DISCONNECTED;
    }

    //4、获取已连接的蓝牙列表 ? 系统上没看到有用这个的。
    public List<BluetoothDevice> getConnectedDevices(int profile) {
        if (DBG) Log.d(TAG, "getConnectedDevices");
        return getDevicesMatchingConnectionStates(profile, new int[] {
                BluetoothProfile.STATE_CONNECTED
        });
    }

    //5、打开低功耗蓝牙 ?
    public BluetoothGattServer openGattServer(Context context,
            BluetoothGattServerCallback callback) {

        return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO));
    }

}

其实没看到有啥大作用的方法,只有个 获取BluetoothAdapter对象方法是比较有用的 ,但是 BluetoothAdapter 是可以直接获取的。

所以 BluetoothManager 在开发场景中是没啥作用的。

类定义的最开头的备注也写了高版本用 BluetoothAdapter 即可。

(5)BluetoothAdapter.java

package\modules\Bluetooth\framework\java\android\bluetooth\BluetoothAdapter.java

public final class BluetoothAdapter {
    private static final String TAG = "BluetoothAdapter";
    private static final String DESCRIPTOR = "android.bluetooth.BluetoothAdapter";
    private static final boolean DBG = true;
    private static final boolean VDBG = false;

    private final IBluetoothManager mManagerService;
    private IBluetooth mService;

    //1、获取 BluetoothAdapter 的暴露的静态方法
    @RequiresNoPermission
    public static synchronized BluetoothAdapter getDefaultAdapter() {
        if (sAdapter == null) {
            sAdapter = createAdapter(AttributionSource.myAttributionSource());
        }
        return sAdapter;
    }

    //2、打开蓝牙的暴露方法
    @RequiresLegacyBluetoothAdminPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean enable() {
        if (isEnabled()) {
            if (DBG) {
                Log.d(TAG, "enable(): BT already enabled!");
            }
            return true;
        }
        try {
            return mManagerService.enable(mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
        return false;
    }

    //2、判断蓝牙是否打开的暴露方法
    public boolean isEnabled() {
        return getState() == BluetoothAdapter.STATE_ON;
    }
    
    //3、设置蓝牙关闭的暴露方法,默认记忆状态
    public boolean disable() {
        return disable(true);
    }

    //4、设置蓝牙关闭的暴露方法,参数表示是否记忆,true 为记忆
    public boolean disable(boolean persist) {
        try {
            return mManagerService.disable(mAttributionSource, persist);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
        return false;
    }
    
    //5、获取蓝牙mac地址
    public String getAddress() {
        try {
            return mManagerService.getAddress(mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
        return null;
    }

    //6、获取当前设备蓝牙名称
    public String getName() {
        try {
            return mManagerService.getName(mAttributionSource);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
        return null;
    }

下面这些api都是和硬件比较相关的,具体实现都是用 IBluetooth

    //7、设置当前设备蓝牙名称
    public boolean setName(String name) {
        if (getState() != STATE_ON) {
            return false;
        }
        mServiceLock.readLock().lock();
        mService.setName(name, mAttributionSource, recv);
        。。。
        return false;
    }

    //8、获取当前设备蓝牙模式,是否被其他应用发现和连接的模式
    public int getScanMode() {
        if (getState() != STATE_ON) {
            return SCAN_MODE_NONE;
        }
        mServiceLock.readLock().lock();
        。。。
        return SCAN_MODE_NONE;
    }

    //9、设置当前设备蓝牙模式,
    //20无,21,可以连接不可搜索到,23 可被连接可被搜索到
   public int setScanMode(@ScanMode int mode) {
        if (getState() != STATE_ON) {
            return BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
        }
      。。。
        return BluetoothStatusCodes.ERROR_UNKNOWN;
    }

    //10、开始蓝牙扫描
    public boolean startDiscovery() {
        if (getState() != STATE_ON) {
            return false;
        }
        mServiceLock.readLock().lock();
        。。。
        return false;
    }

    //11、取消蓝牙扫描
    public boolean cancelDiscovery() {
        if (getState() != STATE_ON) {
            return false;
        }
        mServiceLock.readLock().lock();
        。。。
        return false;
    }

    //12、判断是否正在进行蓝牙扫描
    public boolean isDiscovering() {
        if (getState() != STATE_ON) {
            return false;
        }
        。。。
        return false;
    }

    //13、获取一次蓝牙被扫描发现的时间,对应的是ScanMode为23的情况
    public @Nullable Duration getDiscoverableTimeout() {
        if (getState() != STATE_ON) {
            return null;
        }
        。。。
        return null;
    }

    //14、获取一次蓝牙被扫描发现的时间还剩下多少时间,
    public long getDiscoveryEndMillis() {
        mServiceLock.readLock().lock();
        。。。
        return -1;
    }

    //15、设置被其他设备发现的时间长度,单位秒
    public int setDiscoverableTimeout(@NonNull Duration timeout) {
        if (getState() != STATE_ON) {
            return BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
        }
        。。。
        return BluetoothStatusCodes.ERROR_UNKNOWN;
    }

    //16、获取当前设备保存的蓝牙列表,配对过的
    public Set<BluetoothDevice> getBondedDevices() {
        if (getState() != STATE_ON) {
            return toDeviceSet(Arrays.asList());
        }
        mServiceLock.readLock().lock();
        。。。
        return null;
    }

}

看一下上面这里api 方法,常用的就是上面的这些方法。

IBluetooth 再往下到 AdapterService 和 AdapterProperties,距离底层代码还有一段距离!

AdapterService 和 AdapterProperties 的源码位置:

package\modules\Bluetooth\android\app\src\com\android\bluetooth\btservice\AdapterService.java

package\modules\Bluetooth\android\app\src\com\android\bluetooth\btservice\AdapterProperties.java

有兴趣的可以自己看看。

(6)应用获取 BluetoothManager 和 BluetoothAdapter 对象示例
//BluetoothManager 的获取
BluetoothManager bluetoothManager = context.getSystemService(BluetoothManager.class);

//BluetoothAdapter 的获取
BluetoothAdapter bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

看起来还是 BluetoothAdapter 获取比较简单,并且实用。

三、其他

上面的代码都是开关和扫描控制相关,下面介绍一下断开、连接的代码。

1、蓝牙配对和断开、连接的代码

//蓝牙配对,连接
BluetoothDevice.createBond();

//蓝牙断开
BluetoothAdapter.disconnectAllEnabledProfiles(BluetoothDevice)
//蓝牙忘记
BluetoothDevice.removeBond();

这个 BluetoothDevice 怎么得来的?

就是通过蓝牙扫描收集起来的,或者通过 SettingsLib 库的api或者到当前扫描到的蓝牙列表。

 Collection<CachedBluetoothDevice> cachedDevices = LocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();

LocalBluetoothManager 是 SettingsLib 里面的类,里面应该也是有服务一直在监听蓝牙广播,收集蓝牙列表。

2、Android 蓝牙相关广播介绍

主要介绍Android 蓝牙相关的广播,并非硬件相关的蓝牙广播信号,而是蓝牙app中的广播接收器onReceive 的蓝牙相关广播。

最近刚好开发了一下蓝牙相关的功能,所以进行一些知识总结和介绍,本文应该是全网最面的接收Android广播介绍知识的文章。

Android 蓝牙广播是系统应用或者蓝牙功能应用必备知识。

https://blog.csdn.net/wenzhi20102321/article/details/134956116

3、Android无线蓝牙开发总结

早期写的普通应用端的蓝牙开发:

https://blog.csdn.net/wenzhi20102321/article/details/53870789

4、Android 蓝牙设备类型判断代码介绍

Android 蓝牙设备有各种类型,比如蓝牙手机,蓝牙耳机,蓝牙手环,蓝牙鼠标键盘,蓝牙玩具,健康检测设备等等。

有些场景需要具体区分就需要进行判断了。一个是扫描到后图标显示设备类型,还是可能是具体处理的区别。

这些设备的类型,硬件设备本身其实是有定义的,

但是也不排除有些设备定义不正确,这些都是要我们开发者进行调试才清楚的。

https://blog.csdn.net/wenzhi20102321/article/details/133777224

5、蓝牙开关日志

通过logcat通过grep "BluetoothManagerService" 可以看到蓝牙相关日志;

这个在开关机过程可以看到主要日志,判断设备是否进行开蓝牙的开关控制。

下面主要是展示 BluetoothManagerService 的相关日志

(1)蓝牙开启
130|console:/ # logcat | grep -i BluetoothManagerService           
09-14 17:32:17.558   643   741 D BluetoothManagerService: Trying to bind to profile: 21, while Bluetooth was disabled //等待蓝牙打开
09-14 17:32:18.448   643   740 D BluetoothManagerService: enable(com.skg.settings):  mBluetooth =null mBinding = false mState = OFF
09-14 17:32:18.449   643   771 D BluetoothManagerService: MESSAGE_ENABLE(0): mBluetooth = null
09-14 17:32:18.449   643   771 D BluetoothManagerService: Persisting Bluetooth Setting: 1
09-14 17:32:18.449   643   740 D BluetoothManagerService: enable returning
09-14 17:32:18.450   643   771 D BluetoothManagerService: binding Bluetooth service
09-14 17:32:18.585   643   643 D BluetoothManagerService: BluetoothServiceConnection: com.android.bluetooth.btservice.AdapterService
09-14 17:32:18.585   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_CONNECTED: 1
09-14 17:32:18.587   643   771 D BluetoothManagerService: Broadcasting onBluetoothServiceUp() to 7 receivers.
09-14 17:32:18.589   643   771 D BluetoothManagerService: MESSAGE_GET_NAME_AND_ADDRESS
09-14 17:32:18.591   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: OFF > BLE_TURNING_ON
09-14 17:32:18.591   643   771 D BluetoothManagerService: Sending BLE State Change: OFF > BLE_TURNING_ON
09-14 17:32:20.506   643   643 D BluetoothManagerService: Bluetooth Adapter address changed to 22:22:76:B0:09:00
09-14 17:32:20.506   643   643 D BluetoothManagerService: Stored Bluetoothaddress: 22:22:76:B0:09:00
09-14 17:32:20.507   643   643 D BluetoothManagerService: Bluetooth Adapter name changed to W81B by android
09-14 17:32:20.507   643   643 D BluetoothManagerService: Stored Bluetooth name: W81B
09-14 17:32:20.508   643   643 D BluetoothManagerService: Bluetooth Adapter address changed to 22:22:76:B0:09:00
09-14 17:32:20.509   643   643 D BluetoothManagerService: Stored Bluetoothaddress: 22:22:76:B0:09:00
09-14 17:32:20.509   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_TURNING_ON > BLE_ON
09-14 17:32:20.509   643   771 D BluetoothManagerService: Bluetooth is in LE only mode
09-14 17:32:20.509   643   771 D BluetoothManagerService: Binding Bluetooth GATT service
09-14 17:32:20.510   643   771 D BluetoothManagerService: Sending BLE State Change: BLE_TURNING_ON > BLE_ON
09-14 17:32:20.511   643   643 D BluetoothManagerService: BluetoothServiceConnection: com.android.bluetooth.gatt.GattService
09-14 17:32:20.511   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_CONNECTED: 2
09-14 17:32:20.511   643   771 D BluetoothManagerService: continueFromBleOnState()
09-14 17:32:20.513   643   771 D BluetoothManagerService: Persisting Bluetooth Setting: 1  //设置了蓝牙的settings属性 bluetooth_on 为 1
09-14 17:32:20.513   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_ON > TURNING_ON
09-14 17:32:20.513   643   771 D BluetoothManagerService: Sending BLE State Change: BLE_ON > TURNING_ON
09-14 17:32:20.514   643   771 D BluetoothManagerService: Sending State Change: OFF > TURNING_ON
09-14 17:32:20.665   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: TURNING_ON > ON
09-14 17:32:20.665   643   771 D BluetoothManagerService: Broadcasting onBluetoothStateChange(true) to 20 receivers.
09-14 17:32:20.671   643   771 D BluetoothManagerService: Sending State Change: TURNING_ON > ON
09-14 17:32:20.705   643   643 D BluetoothManagerService: Bluetooth Adapter name changed to IWB by android
09-14 17:32:20.705   643   643 D BluetoothManagerService: Stored Bluetooth name: IWB

//查看蓝牙开启状态为1,开启
130|console:/ # settings get global bluetooth_on
1
console:/ # 

手动打开蓝牙就会有上面的 BluetoothManagerService 相关日志

(2)蓝牙关闭日志
09-14 17:32:28.422   643  1125 D BluetoothManagerService: disable(): mBluetooth = android.bluetooth.IBluetooth$Stub$Proxy@d9debac mBinding = false
09-14 17:32:28.422   643  1125 D BluetoothManagerService: Persisting Bluetooth Setting: 0 //设置了蓝牙的settings属性 bluetooth_on 为 0
09-14 17:32:28.423   643   771 D BluetoothManagerService: MESSAGE_DISABLE: mBluetooth = android.bluetooth.IBluetooth$Stub$Proxy@d9debac, mBinding = false
09-14 17:32:28.724   643   771 D BluetoothManagerService: MESSAGE_HANDLE_DISABLE_DELAYED: disabling:false
09-14 17:32:28.724   643   771 D BluetoothManagerService: Sending off request.
09-14 17:32:28.727   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: ON > TURNING_OFF
09-14 17:32:28.727   643   771 D BluetoothManagerService: Sending BLE State Change: ON > TURNING_OFF
09-14 17:32:28.727   643   771 D BluetoothManagerService: Sending State Change: ON > TURNING_OFF
09-14 17:32:28.775   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: TURNING_OFF > BLE_ON
09-14 17:32:28.775   643   771 D BluetoothManagerService: Intermediate off, back to LE only mode
09-14 17:32:28.775   643   771 D BluetoothManagerService: Sending BLE State Change: TURNING_OFF > BLE_ON
09-14 17:32:28.775   643   771 D BluetoothManagerService: Broadcasting onBluetoothStateChange(false) to 36 receivers.
09-14 17:32:28.776   643   771 D BluetoothManagerService: Calling sendBrEdrDownCallback callbacks
09-14 17:32:28.776   643   771 D BluetoothManagerService: isBleAppPresent() count: 0
09-14 17:32:28.778   643   771 D BluetoothManagerService: Sending State Change: TURNING_OFF > OFF
09-14 17:32:28.779   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_ON > BLE_TURNING_OFF
09-14 17:32:28.779   643   771 D BluetoothManagerService: Sending BLE State Change: BLE_ON > BLE_TURNING_OFF
09-14 17:32:29.026   643   771 D BluetoothManagerService: MESSAGE_HANDLE_DISABLE_DELAYED: disabling:true
09-14 17:32:29.026   643   771 D BluetoothManagerService: Handle disable is finished
09-14 17:32:29.060   643   771 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_TURNING_OFF > OFF
09-14 17:32:29.060   643   771 D BluetoothManagerService: Bluetooth is complete send Service Down
09-14 17:32:29.060   643   771 D BluetoothManagerService: Broadcasting onBluetoothServiceDown() to 7 receivers.
09-14 17:32:29.061   643   771 D BluetoothManagerService: unbindAndFinish(): android.bluetooth.IBluetooth$Stub$Proxy@d9debac mBinding = false mUnbinding = false
09-14 17:32:29.065   643   771 D BluetoothManagerService: Sending BLE State Change: BLE_TURNING_OFF > OFF

//查看蓝牙开启状态为0,关闭
130|console:/ # settings get global bluetooth_on
0
console:/ # 

上面是手动关闭蓝牙的日志。

(3)开机前蓝牙开启的开机日志
console:/ # logcat | grep BluetoothManagerService
09-11 17:52:32.516   810  1200 D BluetoothManagerService: Trying to bind to profile: 21, while Bluetooth was disabled
09-11 17:52:32.620   810   810 D BluetoothManagerService: Trying to bind to profile: 1, while Bluetooth was disabled
...
09-11 17:52:32.943   810   810 V BluetoothManagerService: Checking activity com.android.bluetooth.opp.BluetoothOppLauncherActivity
09-11 17:52:33.033   810   827 D BluetoothManagerService: Trying to bind to profile: 1, while Bluetooth was disabled
09-11 17:52:33.879   810   844 D BluetoothManagerService: User UserHandle{0} unlocked
09-11 17:52:33.879   810   939 D BluetoothManagerService: MESSAGE_USER_UNLOCKED
09-11 17:52:34.947   810   810 D BluetoothManagerService: Bluetooth Adapter name changed to tt by android //蓝牙名称
09-11 17:52:34.947   810   810 D BluetoothManagerService: Stored Bluetooth name: tt
09-11 17:52:34.965   810   810 D BluetoothManagerService: BluetoothServiceConnection: com.android.bluetooth.btservice.AdapterService
09-11 17:52:34.965   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_CONNECTED: 1
09-11 17:52:34.973   810   939 D BluetoothManagerService: Broadcasting onBluetoothServiceUp() to 8 receivers.
09-11 17:52:34.977   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: OFF > BLE_TURNING_ON
09-11 17:52:34.977   810   939 D BluetoothManagerService: Sending BLE State Change: OFF > BLE_TURNING_ON
09-11 17:52:34.977   810   939 W BroadcastLoopers: Found previously unknown looper Thread[BluetoothManagerService,5,main]
09-11 17:52:35.018   810   810 D BluetoothManagerService: Bluetooth Adapter address changed to 22:22:4B:FA:0C:00
09-11 17:52:35.019   810   810 D BluetoothManagerService: Stored Bluetoothaddress: 22:22:4B:FA:0C:00
09-11 17:52:35.023   810   810 D BluetoothManagerService: Bluetooth Adapter name changed to tt by android
09-11 17:52:35.023   810   810 D BluetoothManagerService: Stored Bluetooth name: tt
09-11 17:52:35.033   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_TURNING_ON > BLE_ON
09-11 17:52:35.033   810   939 D BluetoothManagerService: Bluetooth is in LE only mode
09-11 17:52:35.033   810   939 D BluetoothManagerService: Binding Bluetooth GATT service
09-11 17:52:35.035   810   939 D BluetoothManagerService: Sending BLE State Change: BLE_TURNING_ON > BLE_ON
09-11 17:52:35.036   810   810 D BluetoothManagerService: BluetoothServiceConnection: com.android.bluetooth.gatt.GattService
09-11 17:52:35.036   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_CONNECTED: 2
09-11 17:52:35.041   810   939 D BluetoothManagerService: continueFromBleOnState()
09-11 17:52:35.042   810   939 D BluetoothManagerService: Persisting Bluetooth Setting: 1
09-11 17:52:35.043   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: BLE_ON > TURNING_ON
09-11 17:52:35.043   810   939 D BluetoothManagerService: Sending BLE State Change: BLE_ON > TURNING_ON
09-11 17:52:35.044   810   939 D BluetoothManagerService: Sending State Change: OFF > TURNING_ON
09-11 17:52:35.163   810   939 D BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: TURNING_ON > ON
09-11 17:52:35.163   810   939 D BluetoothManagerService: Broadcasting onBluetoothStateChange(true) to 17 receivers.
09-11 17:52:35.163   810   939 D BluetoothManagerService: Creating new ProfileServiceConnections object for profile: 1
...
09-11 17:52:35.175   810   939 D BluetoothManagerService: Sending BLE State Change: TURNING_ON > ON
09-11 17:52:35.176   810   939 D BluetoothManagerService: Sending State Change: TURNING_ON > ON

这个是开机后打印的日志,可以看到有蓝牙从关闭到开启的状态过程打印。

还有蓝牙名称打印。

(4)开机前蓝牙关闭的开机日志
console:/ # logcat | grep BluetoothManagerService
09-11 17:52:32.098   811  1015 D BluetoothManagerService: Trying to bind to profile: 1, while Bluetooth was disabled
09-11 17:52:32.102   811  1015 D BluetoothManagerService: Trying to bind to profile: 2, while Bluetooth was disabled
09-11 17:52:32.104   811  1015 D BluetoothManagerService: Trying to bind to profile: 11, while Bluetooth was disabled
09-11 17:52:32.107   811  1015 D BluetoothManagerService: Trying to bind to profile: 21, while Bluetooth was disabled
09-11 17:52:32.111   811  1015 D BluetoothManagerService: Trying to bind to profile: 22, while Bluetooth was disabled
09-11 17:52:32.116   811  1015 D BluetoothManagerService: Trying to bind to profile: 26, while Bluetooth was disabled
09-11 17:52:32.294   811   811 D BluetoothManagerService: Bluetooth boot completed
09-11 17:52:32.295   811   811 D BluetoothManagerService: Trying to bind to profile: 2, while Bluetooth was disabled
...
09-11 17:52:32.871   811   811 V BluetoothManagerService: Searching package com.android.bluetooth
09-11 17:52:32.872   811   811 V BluetoothManagerService: Checking activity com.android.bluetooth.BluetoothPrefs
09-11 17:52:32.872   811   811 V BluetoothManagerService: Checking activity com.android.bluetooth.map.BluetoothMapSettings
09-11 17:52:32.872   811   811 V BluetoothManagerService: Checking activity com.android.bluetooth.opp.BluetoothOppLauncherActivity
09-11 17:52:32.934   811   828 D BluetoothManagerService: Trying to bind to profile: 1, while Bluetooth was disabled
09-11 21:12:06.753   811   846 D BluetoothManagerService: User UserHandle{0} unlocked
09-11 21:12:06.754   811   943 D BluetoothManagerService: MESSAGE_USER_UNLOCKED

蓝牙关闭的情况, 从上面代码是看不到蓝牙打开的过程的,

但是也有一些蓝牙准备的过程,蓝牙一些服务绑定需要等待底层初始化OK才能正常绑定。

上面的日志比较多,可能不一定全部有用,

只需要知道,蓝牙开关过程中 BluetoothManagerService 都会有一些打印就可以了。

祝各位:中秋节日快乐。

相关推荐
xiaoxiongip6663 分钟前
HTTP 和 HTTPS
网络·爬虫·网络协议·tcp/ip·http·https·ip
Mr Lee_5 分钟前
android 配置鼠标右键快捷对apk进行反编译
android
JaneJiazhao9 分钟前
HTTPSOK:智能SSL证书管理的新选择
网络·网络协议·ssl
CXDNW9 分钟前
【网络面试篇】HTTP(2)(笔记)——http、https、http1.1、http2.0
网络·笔记·http·面试·https·http2.0
顾北川_野40 分钟前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&1 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
无所谓จุ๊บ1 小时前
树莓派开发相关知识十 -小试服务器
服务器·网络·树莓派
道法自然04021 小时前
Ethernet 系列(8)-- 基础学习::ARP
网络·学习·智能路由器
EasyCVR2 小时前
萤石设备视频接入平台EasyCVR多品牌摄像机视频平台海康ehome平台(ISUP)接入EasyCVR不在线如何排查?
运维·服务器·网络·人工智能·ffmpeg·音视频
明月看潮生3 小时前
青少年编程与数学 02-003 Go语言网络编程 15课题、Go语言URL编程
开发语言·网络·青少年编程·golang·编程与数学