Android之Bluetooth日志

1.开发者选项->启用蓝牙HCI信息收集日志。

    private void writeBtHciSnoopLogOptions() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        adapter.configHciSnoopLog(mBtHciSnoopLog.isChecked());
        Settings.Secure.putInt(getActivity().getContentResolver(),
                Settings.Secure.BLUETOOTH_HCI_LOG,
                mBtHciSnoopLog.isChecked() ? 1 : 0);
    }
    
    蓝牙默认开关
    ./hardware/realtek/rtkbt/code/bt/conf/bt_stack.conf
    
    ./system/bt/conf/bt_stack.conf

2.android 8版本,默认位置/data/misc/bluetooth/logs

/data/misc/bluetooth/logs # ls -l
total 3904
-rw-rw-r-- 1 bluetooth bluetooth      16 2019-04-25 17:27 btsnoop_hci.log
-rw-rw-r-- 1 bluetooth bluetooth      16 2019-04-25 17:27 btsnoop_hci.log.last
-rw-rw-r-- 1 bluetooth bluetooth 2755648 2019-04-25 18:16 hci_snoop20190425172740.cfa
-rw-rw-r-- 1 bluetooth bluetooth 1221959 2019-04-25 18:21 hci_snoop20190425173638.cfa

3.android 7, 默认位置 /sdcard/

4.配置hci路径:

cat /etc/bluetooth/bt_statck.conf
# BtSnoop log output file
BtSnoopFileName=/sdcard/btsnoop_hci.log

5.用wireshark等工具打开即可。

https://www.jianshu.com/p/73f7366161d1
https://blog.csdn.net/feelinghappy/article/details/112846076
https://blog.csdn.net/qq_43824618/article/details/113878433

6.RSSI

RSSI(接收信号强度)Received Signal Strength Indicator
Rss=10logP,
只需将接受到的信号功率P代入就是接收信号强度(灵敏度)。
[例1] 如果发射功率P为1mw,折算为dBm后为0dBm。
[例2] 对于40W的功率,按dBm单位进行折算后的值应为:
10lg(40W/1mw)=10lg(40000)=10lg4+10lg10+10lg1000=46dBm。

Rssi和接收功率有关,单位是dBm
一般为负值,反应的是信号的衰减程度,理想状态下(无衰减),Rssi = 0dBm,实际情况是,即使蓝牙设备挨得非常近,Rssi也只有-50dBm的强度,在传输过程中,不可避免要损耗。

经典蓝牙强度:
-50 ~ 0dBm   信号强
-70 ~-50dBm  信号中
<-70dBm      信号弱

低功耗蓝牙分四级:
-60 ~  0   4
-70 ~ -60  3
-80 ~ -70  2
<-80       1

代码用例
/**
 * A和n的值,需要根据实际环境进行检测得出
 */
public class RssiUtils {

    /** A 发射端和接收端相隔1米时的信号强度 */
    private static final double A_Value = 50;
    /** n 环境衰减因子 */
    private static final double n_Value = 2.5;

    /**
     * 根据Rssi的值,计算距离,单位m
     * @param rssi 信号强度,单位dB
     */
    public static double getLeDistance(int rssi) {
        double power = (Math.abs(rssi) - A_Value) / (10 * n_Value);
        return Math.pow(10, power);
    }

    /**
     * 经典蓝牙强度 
     * -50 ~ 0dBm  信号强
     * -70 ~ -50dBm    信号中
     * <-70dBm      信号弱
     */
    public static byte getBredrLevel(int rssi) {
        if(rssi > -50) {
            return 3;
        } else if(rssi > -70) {
            return 2;
        } else {
            return 1;
        }
    }

    /**
     * 低功耗蓝牙分四级
     * -60 ~ 0     4
     * -70 ~ -60   3
     * -80 ~ -70   2
     * <-80         1
     */
    public static byte getLeLevel(int rssi) {
        if(rssi > -60) {
            return 4;
        } else if(rssi > -70) {
            return 3;
        } else if(rssi > -80) {
            return 2;
        } else {
            return 1;
        }
    }
}

7.gatt错误码

hardware/realtek/rtkbt/code/bt/stack/include/gatt_api.h
system/bt/stack/include/gatt_api.h


https://blog.csdn.net/weixin_30627381/article/details/101761801

你可能感兴趣的:(Android之Bluetooth日志)