CH58xBLE服务增减讲解
在使用蓝牙从机的时候,为了兼容之前的上位机软件,可能需要修改服务与之适应。
WCH的Peripheral例程在Peripheral_Init初始化中注册了四个服务,顺序如下:
前三个是蓝牙联盟规定的一些服务且不是必须的,如果需要手机上不显示此服务则频闭对应的初始化即可,改变显示顺序则改变初始化函数的顺序,
第四个是我们自定义的服务,可以在gattprofile.c文件里自定义改变uuid,支持属性等等。
GGS_AddService(GATT_ALL_SERVICES); // GAP
GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
DevInfo_AddService(); // Device Information Service
SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile
一.服务内容修改:
1.UUID修改:
16bit的uuid直接修改下方宏定义即可。
// Simple GATT Profile Service UUID: 0xFFF0
const uint8_t simpleProfileServUUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_SERV_UUID), HI_UINT16(SIMPLEPROFILE_SERV_UUID)};
// Characteristic 1 UUID: 0xFFF1
const uint8_t simpleProfilechar1UUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_CHAR1_UUID), HI_UINT16(SIMPLEPROFILE_CHAR1_UUID)};
// Characteristic 2 UUID: 0xFFF2
const uint8_t simpleProfilechar2UUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_CHAR2_UUID), HI_UINT16(SIMPLEPROFILE_CHAR2_UUID)};
// Characteristic 3 UUID: 0xFFF3
const uint8_t simpleProfilechar3UUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_CHAR3_UUID), HI_UINT16(SIMPLEPROFILE_CHAR3_UUID)};
// Characteristic 4 UUID: 0xFFF4
const uint8_t simpleProfilechar4UUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_CHAR4_UUID), HI_UINT16(SIMPLEPROFILE_CHAR4_UUID)};
// Characteristic 5 UUID: 0xFFF5
const uint8_t simpleProfilechar5UUID[ATT_BT_UUID_SIZE] = {
LO_UINT16(SIMPLEPROFILE_CHAR5_UUID), HI_UINT16(SIMPLEPROFILE_CHAR5_UUID)};
// Simple Profile Service UUID
#define SIMPLEPROFILE_SERV_UUID 0xFFE0
// Key Pressed UUID
#define SIMPLEPROFILE_CHAR1_UUID 0xFFE1
#define SIMPLEPROFILE_CHAR2_UUID 0xFFE2
#define SIMPLEPROFILE_CHAR3_UUID 0xFFE3
#define SIMPLEPROFILE_CHAR4_UUID 0xFFE4
#define SIMPLEPROFILE_CHAR5_UUID 0xFFE5
2.Properties修改:
char1支持读/写功能,那么在读写回调函数中就需要针对char1的uuid进行对应的操作,这部分原先例程中有处理,
// Simple Profile Characteristic 1 Properties static uint8_t simpleProfileChar1Props = GATT_PROP_READ | GATT_PROP_WRITE;
char2支持读功能,那么在读回调函数中就需要针对char2的uuid进行对应的操作,这部分原先例程中有处理,
// Simple Profile Characteristic 2 Properties static uint8_t simpleProfileChar2Props = GATT_PROP_READ;
char3支持写功能,那么在写回调函数中就需要针对char3的uuid进行对应的操作,这部分原先例程中有处理,
// Simple Profile Characteristic 3 Properties static uint8_t simpleProfileChar3Props = GATT_PROP_WRITE;
char4支持通知功能,
// Simple Profile Characteristic 4 Properties static uint8_t simpleProfileChar4Props = GATT_PROP_NOTIFY;
char5支持读功能,那么在读回调函数中就需要针对char5的uuid进行对应的操作,这部分原先例程中有处理,
// Simple Profile Characteristic 5 Properties static uint8_t simpleProfileChar5Props = GATT_PROP_READ;
下面的这个Attributes - Table就决定了服务里的内容,若想去掉某个char则频闭下面table里对应的char内容,若需要增加一个则模仿char1来写即可。
static gattAttribute_t simpleProfileAttrTbl[] = {
// Simple Profile Service
{
{ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
GATT_PERMIT_READ, /* permissions */
0, /* handle */
(uint8_t *)&simpleProfileService /* pValue */
},
// Characteristic 1 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar1Props},
// Characteristic Value 1
{
{ATT_BT_UUID_SIZE, simpleProfilechar1UUID},
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
simpleProfileChar1},
// Characteristic 1 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar1UserDesp},
// Characteristic 2 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar2Props},
// Characteristic Value 2
{
{ATT_BT_UUID_SIZE, simpleProfilechar2UUID},
GATT_PERMIT_READ,
0,
simpleProfileChar2},
// Characteristic 2 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar2UserDesp},
// Characteristic 3 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar3Props},
// Characteristic Value 3
{
{ATT_BT_UUID_SIZE, simpleProfilechar3UUID},
GATT_PERMIT_WRITE,
0,
simpleProfileChar3},
// Characteristic 3 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar3UserDesp},
// Characteristic 4 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar4Props},
// Characteristic Value 4
{
{ATT_BT_UUID_SIZE, simpleProfilechar4UUID},
0,
0,
simpleProfileChar4},
// Characteristic 4 configuration
{
{ATT_BT_UUID_SIZE, clientCharCfgUUID},
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8_t *)simpleProfileChar4Config},
// Characteristic 4 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar4UserDesp},
// Characteristic 5 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar5Props},
// Characteristic Value 5
{
{ATT_BT_UUID_SIZE, simpleProfilechar5UUID},
GATT_PERMIT_AUTHEN_READ,
0,
simpleProfileChar5},
// Characteristic 5 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar5UserDesp},
}
二.增加一个服务:
最方便的增加服务的方法就是将BLE UART的服务直接加进来,初始化时加上以下代码即可:
ble_uart_add_service(on_bleuartServiceEvt);
//ble uart service callback handler
void on_bleuartServiceEvt(uint16_t connection_handle, ble_uart_evt_t *p_evt)
{
switch(p_evt->type)
{
case BLE_UART_EVT_TX_NOTI_DISABLED:
PRINT("%02x:bleuart_EVT_TX_NOTI_DISABLED\r\n", connection_handle);
break;
case BLE_UART_EVT_TX_NOTI_ENABLED:
PRINT("%02x:bleuart_EVT_TX_NOTI_ENABLED\r\n", connection_handle);
break;
case BLE_UART_EVT_BLE_DATA_RECIEVED:
PRINT("BLE RX DATA len:%d\r\n", p_evt->data.length);
break;
default:
break;
}
}
peripheralService2Notify(notiData, SIMPLEPROFILE_CHAR4_LEN);
static void peripheralService2Notify(uint8_t *pValue, uint16_t len)
{
attHandleValueNoti_t noti;
if(len > (peripheralMTU - 3))
{
PRINT("Too large noti\n");
return;
}
noti.len = len;
noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
if(noti.pValue)
{
tmos_memcpy(noti.pValue, pValue, noti.len);
if(ble_uart_notify(peripheralConnList.connHandle, ¬i, Peripheral_TaskID)!= SUCCESS)
{
GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
}
}
}

浙公网安备 33010602011771号