详细讲解下面的代码:
{
// 准备存放设备名称和 UUID(16-bit 和 128-bit)的缓存区。
/* inquiry result */
bt_bdname_t bdname;
uint8_t remote_name_len;
uint8_t num_uuids = 0, num_uuids128 = 0, max_num_uuid = 32;
uint8_t uuid_list[32 * Uuid::kNumBytes16];
uint8_t uuid_list128[32 * Uuid::kNumBytes128];
// 根据 EIR 数据判断是否需要发起 Remote Name Request(即设备是否已广播出名称)。
p_search_data->inq_res.remt_name_not_required =
check_eir_remote_name(p_search_data, NULL, NULL);
// 获取发现设备的地址。
RawAddress& bdaddr = p_search_data->inq_res.bd_addr;
BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__,
bdaddr.ToString().c_str(),
p_search_data->inq_res.device_type);
bdname.name[0] = 0;
// 尝试从 EIR 中读取名称,失败则从缓存中获取。
if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
/* Check EIR for services */
// 获取 UUID(服务)信息:
if (p_search_data->inq_res.p_eir) {
BTM_GetEirUuidList(p_search_data->inq_res.p_eir,
p_search_data->inq_res.eir_len, Uuid::kNumBytes16,
&num_uuids, uuid_list, max_num_uuid);
BTM_GetEirUuidList(p_search_data->inq_res.p_eir,
p_search_data->inq_res.eir_len, Uuid::kNumBytes128,
&num_uuids128, uuid_list128, max_num_uuid);
}
{
/*
构建属性列表:
将设备属性打包成数组,逐项填充:
- 蓝牙地址
- 名称
- 类别(COD)
- 类型(BR/EDR、BLE 或 DUAL)
- RSSI
- 是否支持 CSIP 协调组
- UUID(服务)
UUID 部分还会缓存入 eir_uuids_cache,并使用 btif_update_uuid 做去重更新。
*/
bt_property_t properties[7];
bt_device_type_t dev_type;
uint32_t num_properties = 0;
bt_status_t status;
tBLE_ADDR_TYPE addr_type = BLE_ADDR_PUBLIC;
memset(properties, 0, sizeof(properties));
/* RawAddress */
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
num_properties++;
/* BD_NAME */
/* Don't send BDNAME if it is empty */
if (bdname.name[0]) {
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_BDNAME,
strlen((char*)bdname.name), &bdname);
num_properties++;
}
/* DEV_CLASS */
uint32_t cod = devclass2uint(p_search_data->inq_res.dev_class);
BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
if (cod != 0) {
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod),
&cod);
num_properties++;
}
/* DEV_TYPE */
/* FixMe: Assumption is that bluetooth.h and BTE enums match */
/* Verify if the device is dual mode in NVRAM */
int stored_device_type = 0;
if (btif_get_device_type(bdaddr, &stored_device_type) &&
((stored_device_type != BT_DEVICE_TYPE_BREDR &&
p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
(stored_device_type != BT_DEVICE_TYPE_BLE &&
p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO;
} else {
dev_type = (bt_device_type_t)p_search_data->inq_res.device_type;
}
if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
addr_type = p_search_data->inq_res.ble_addr_type;
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type),
&dev_type);
num_properties++;
/* RSSI */
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
&(p_search_data->inq_res.rssi));
num_properties++;
/* CSIP supported device */
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,
sizeof(bool),
&(p_search_data->inq_res.include_rsi));
num_properties++;
/* Cache EIR queried services */
if ((num_uuids + num_uuids128) > 0) {
uint16_t* p_uuid16 = (uint16_t*)uuid_list;
auto uuid_iter = eir_uuids_cache.find(bdaddr);
Uuid new_remote_uuid[BT_MAX_NUM_UUIDS];
size_t dst_max_num = sizeof(new_remote_uuid)/sizeof(Uuid);
size_t new_num_uuid = 0;
Uuid remote_uuid[BT_MAX_NUM_UUIDS];
if (uuid_iter == eir_uuids_cache.end()) {
auto triple = eir_uuids_cache.try_emplace(bdaddr, std::set<Uuid>{});
uuid_iter = std::get<0>(triple);
}
//LOG_INFO("EIR UUIDs for %s:", bdaddr.ToString().c_str());
for (int i = 0; i < num_uuids; ++i) {
Uuid uuid = Uuid::From16Bit(p_uuid16[i]);
//LOG_INFO(" %s", uuid.ToString().c_str());
uuid_iter->second.insert(uuid);
if (i < BT_MAX_NUM_UUIDS) {
remote_uuid[i] = uuid;
} else {
LOG_INFO("%d >= %d", i, BT_MAX_NUM_UUIDS);
}
}
for (int i = 0; i < num_uuids128; ++i) {
Uuid uuid = Uuid::From128BitBE((uint8_t *)&uuid_list128[i * Uuid::kNumBytes128]);
//LOG_INFO(" %s", uuid.ToString().c_str());
uuid_iter->second.insert(uuid);
if (i < BT_MAX_NUM_UUIDS) {
remote_uuid[num_uuids + i] = uuid;
} else {
LOG_INFO("%d >= %d", i, BT_MAX_NUM_UUIDS);
}
}
//LOG_INFO("%s %d : update EIR UUIDs.", __func__, __LINE__);
new_num_uuid = btif_update_uuid(bdaddr, remote_uuid,
(num_uuids + num_uuids128), new_remote_uuid,
sizeof(new_remote_uuid),
dst_max_num);
BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
BT_PROPERTY_UUIDS,
new_num_uuid * Uuid::kNumBytes128, new_remote_uuid);
//LOG_INFO("%s %d : fill BT_PROPERTY_UUIDS property.", __func__, __LINE__);
num_properties ++;
}
// 持久化 & 回调:
status =
btif_storage_add_remote_device(&bdaddr, num_properties, properties); // 添加进本地数据库
ASSERTC(status == BT_STATUS_SUCCESS,
"failed to save remote device (inquiry)", status);
status = btif_storage_set_remote_addr_type(&bdaddr, addr_type); // 保存 BLE 地址类型
ASSERTC(status == BT_STATUS_SUCCESS,
"failed to save remote addr type (inquiry)", status);
bool restrict_report = osi_property_get_bool(
"bluetooth.restrict_discovered_device.enabled", false);
// 限制上报策略, 可选地根据系统属性是否限制上报某些设备。
if (restrict_report &&
p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE &&
!(p_search_data->inq_res.ble_evt_type & BTM_BLE_CONNECTABLE_MASK)) {
LOG_INFO("%s: Ble device is not connectable",
bdaddr.ToString().c_str());
break;
}
// 上报到上层(Java 层): 触发 BluetoothAdapter.onBluetoothDeviceFound() 等 Java 层通知。
/* Callback to notify upper layer of device */
invoke_device_found_cb(num_properties, properties);
}
}