Android Bluetooth蓝牙设备地址修改

本文介绍蓝牙设备地址的获取、修改及地址类型的调整方法。详细解释了通过不同途径获取蓝牙地址的过程,包括从文件读取、配置文件获取以及生成随机地址等步骤。同时,还介绍了如何根据隐私设置来调整蓝牙地址类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、蓝牙设备地址获取
bt_status_t btif_init_bluetooth()
{
    /* As part of the init, fetch the local BD ADDR */
    memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
    btif_fetch_local_bdaddr(&btif_local_bd_addr);
}
#ifndef PROPERTY_BT_BDADDR_PATH // Btif_common.h (external\bluetooth\bluedroid\btif\include)
#define PROPERTY_BT_BDADDR_PATH         "ro.bt.bdaddr_path"
#endif

#ifndef PERSIST_BDADDR_PROPERTY
#define PERSIST_BDADDR_PROPERTY         "persist.service.bdroid.bdaddr"
#endif
       
static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
{
    char val[256];
    uint8_t valid_bda = FALSE;
    int val_size = 0;
    const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};

    /* Get local bdaddr storage path from property */
    if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
    {
        int addr_fd;

        BTIF_TRACE_DEBUG("local bdaddr is stored in %s", val);

        if ((addr_fd = open(val, O_RDONLY)) != -1)
        {
            memset(val, 0, sizeof(val));
            read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
            str2bd(val, local_addr);
            /* If this is not a reserved/special bda, then use it */
            if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)
            {
                valid_bda = TRUE;
                BTIF_TRACE_DEBUG("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X",
                    local_addr->address[0], local_addr->address[1], local_addr->address[2],
                    local_addr->address[3], local_addr->address[4], local_addr->address[5]);
            }

            close(addr_fd);
        }
    }

    if(!valid_bda)
    {
        val_size = sizeof(val);
        if(btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
        {
            str2bd(val, local_addr);
            BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is  %s", val);
            return;
        }
     }

    /* No factory BDADDR found. Look for previously generated random BDA */
    if ((!valid_bda) && \
        (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
    {
        str2bd(val, local_addr);
        valid_bda = TRUE;
        BTIF_TRACE_DEBUG("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X",
            local_addr->address[0], local_addr->address[1], local_addr->address[2],
            local_addr->address[3], local_addr->address[4], local_addr->address[5]);
    }

    /* Generate new BDA if necessary */
    if (!valid_bda)
    {
        bdstr_t bdstr;
        /* Seed the random number generator */
        srand((unsigned int) (time(0)));

        /* No autogen BDA. Generate one now. */
        local_addr->address[0] = 0x22;
        local_addr->address[1] = 0x22;
        local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);

        /* Convert to ascii, and store as a persistent property */
        bd2str(local_addr, &bdstr);

        BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
             (char*)bdstr, PERSIST_BDADDR_PROPERTY);

        if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
            BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
    }

    //save the bd address to config file
    bdstr_t bdstr;
    bd2str(local_addr, &bdstr);
    val_size = sizeof(val);
    if (btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
    {
        if (strcmp(bdstr, val) ==0)
        {
            // BDA is already present in the config file.
            return;
        }
    }
    btif_config_set_str("Local", "Adapter", "Address", bdstr);
    btif_config_save();
}
二、蓝牙设备地址修改

获取地址的顺序:

  1. 获取属性ro.bt.bdaddr_path里面蓝牙设备地址的路径,然后打开保存蓝牙地址的文件从中读取设备地址。
  2. 从bt_config.xml中读取蓝牙设备地址。
  3. 生成随机地址。
  4. 将地址保存到属性persist.service.bdroid.bdaddr和bt_config.xml中。

根据以上提到的四点去修改设备地址。

三、蓝牙设备地址类型修改

BlueDroid协议栈根据是否开启Privacy特性选择使用的地址类型。
然后在设置广播参数、扫描参数和发起连接的时候会把地址类型一起发送出去。

Btif_core.c (external\bluetooth\bluedroid\btif\src)

static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
	BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
		p_msg->hdr.event = BTA_DM_API_LOCAL_PRIVACY_EVT;
        p_msg->privacy_enable   = privacy_enable;
        bta_sys_sendmsg(p_msg);

BTA_DM_API_LOCAL_PRIVACY_EVT消息对应执行函数是bta_dm_action里面的bta_dm_ble_config_local_privacy。

const tBTA_DM_ACTION bta_dm_action[] =
#if BLE_PRIVACY_SPT == TRUE
    bta_dm_ble_config_local_privacy,   /* BTA_DM_API_LOCAL_PRIVACY_EVT */
    BTM_BleConfigPrivacy (p_data->ble_local_privacy.privacy_enable);
#endif
void BTM_BleConfigPrivacy(BOOLEAN enable)
{
    tBTM_BLE_CB     *p_cb = &btm_cb.ble_ctr_cb;

    if (p_cb->privacy != enable)
    {
        p_cb->privacy = enable;
        if (p_cb->privacy)
        {
            /* generate resolvable private address */
            btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
        }
        else /* if privacy disabled, always use public address */
        {
            p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
        }

        if (BTM_BleMaxMultiAdvInstanceCount() > 0)
            btm_ble_multi_adv_enb_privacy(p_cb->privacy);
    }
}

修改方法:
1) 直接修改Bt_target.h中BLE_LOCAL_PRIVACY_ENABLED宏的值
Bt_target.h (external\bluetooth\bluedroid\include)

#ifdef HAS_BDROID_BUILDCFG
#include "bdroid_buildcfg.h"
#endif
/*
 * Toggles support for general LE privacy features such as remote address
 * resolution, local address rotation etc.
 */
#ifndef BLE_PRIVACY_SPT
#define BLE_PRIVACY_SPT         TRUE
#endif

/*
 * Enables or disables support for local privacy (ex. address rotation)
 */
#ifndef BLE_LOCAL_PRIVACY_ENABLED
#define BLE_LOCAL_PRIVACY_ENABLED         TRUE
#endif

2) 在bdroid_buildcfg.h中定义BLE_LOCAL_PRIVACY_ENABLED。

device/generic/common/bluetooth/bdroid_buildcfg.h

#ifndef _BDROID_BUILDCFG_H
#define _BDROID_BUILDCFG_H

#define BLE_LOCAL_PRIVACY_ENABLED         FALSE

#endif
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值