一从机多主机蓝牙连接里面很多UUID,为什么你这里值比较一个UUID呢?请给出多UUID比较的例子
时间: 2024-03-16 10:44:58 浏览: 104
如果在一个从机可以同时与多个主机设备建立蓝牙连接的情况下,每个主机设备可能使用不同的UUID来识别自己,那么需要使用一个UUID数组来存储这些UUID,并在BLE_GAP_EVT_ADV_REPORT事件处理程序中遍历数组以查找与收到的广播包匹配的UUID。
下面是一个示例代码,用于比较多个UUID:
```c
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_soc.h"
#include "nrf_delay.h"
#include "app_uart.h"
#include "ble.h"
#include "ble_gap.h"
#define UUID_LENGTH 16
static uint8_t m_uuid1[UUID_LENGTH] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
static uint8_t m_uuid2[UUID_LENGTH] = {0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20};
static uint8_t m_uuid3[UUID_LENGTH] = {0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30};
static uint32_t m_device_count1 = 0;
static uint32_t m_device_count2 = 0;
static uint32_t m_device_count3 = 0;
static uint8_t* m_uuid_array[] = {m_uuid1, m_uuid2, m_uuid3};
static uint32_t m_device_count_array[] = {&m_device_count1, &m_device_count2, &m_device_count3};
static uint8_t m_uuid_count = 3;
static void ble_evt_handler(ble_evt_t * p_ble_evt)
{
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_ADV_REPORT:
{
ble_gap_evt_adv_report_t * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;
if (p_adv_report->type.connectable)
{
for (int i = 0; i < m_uuid_count; i++)
{
if (memcmp(p_adv_report->data.uuid.uuid128, m_uuid_array[i], UUID_LENGTH) == 0)
{
(*m_device_count_array[i])++;
}
}
}
} break;
default:
// No implementation needed.
break;
}
}
int main(void)
{
uint32_t err_code;
// Initialize BLE stack.
err_code = sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_enable(&m_ram_start);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_start(&m_adv_params);
APP_ERROR_CHECK(err_code);
while (true)
{
// Wait for events.
err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
// Handle BLE events.
ble_evt_handler(p_ble_evt);
}
}
```
在此示例代码中,我们定义了三个不同的UUID,并使用一个UUID数组来存储它们。在BLE_GAP_EVT_ADV_REPORT事件处理程序中,我们遍历UUID数组以查找与收到的广播包匹配的UUID。如果找到匹配的UUID,则增加相应的m_device_count变量的值。
阅读全文