BLE 主机 获取不到 从机 NOTIFY属性的特征值的问题

一、情况

最近使用CC2541做主机来连接CH573做测试,发现获取不到CH573的notify属性的特征值,后面发现,通过 GATT_ReadUsingCharUUID() 是搜索不到的,而通过 GATT_DiscCharsByUUID() 是可以搜索到的。


二、需要注意的点

  1. GATT_ReadUsingCharUUID() 和 GATT_DiscCharsByUUID()的区别:
    由于一个characteristic定义是由:characteristic declaration,characteristic value,characteristic configuration组成;GATT_ReadUsingCharUUID()读取到的是 characteristic value的hdl,而 GATT_DiscCharsByUUID() 读取到的 是 characteristic declaration 的hdl。

  2. 对于 characteristic configuration ,只能通过 GATT_ReadUsingCharUUID() 来操作,所以我们这里可以这样做: 对于特征值的 value的hdl ,我们可以通过 disc找到hdl +1 处理,对于需要ccc打开notify选项,则通过read来直接读取即可。

三、代码

  1. 主机
/*********************************************************************
 * @fn      simpleBLEGATTDiscoveryEvent
 *
 * @brief   Process GATT discovery event
 *
 * @return  none
 */
static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg )
{
  attReadByTypeReq_t req;
  BLE_DEV *p =  simpleBLECentralGetDev(pMsg->connHandle);
  
   if ( p->simpleBLEDiscState == BLE_DISC_STATE_SVC )   
   {    
      // Service found, store handles    
      if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&       
       pMsg->msg.findByTypeValueRsp.numInfo > 0 )   
      {      
        p->simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;  
        p->simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;    
      }    
  
      // If procedure complete    
      if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP  &&           
      pMsg->hdr.status == bleProcedureComplete ) ||         
      ( pMsg->method == ATT_ERROR_RSP ) )   
      {      
        if ( p->simpleBLESvcStartHdl != 0 )      
        {        
          // Discover characteristic       
          p->simpleBLEDiscState = BLE_DISC_STATE_CHAR1;      
          req.startHandle = p->simpleBLESvcStartHdl;    
          req.endHandle = p->simpleBLESvcEndHdl;    
          req.type.len = ATT_BT_UUID_SIZE;
          req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);    
          req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);      
          GATT_DiscCharsByUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId );     
        }    
      }  
  }  
  else if ( p->simpleBLEDiscState == BLE_DISC_STATE_CHAR1 )  //先找CHAR1
  {    
    NPI_PrintValue("1 pMsg->method", pMsg->method, 10);
    NPI_PrintValue("1 numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
    // Characteristic found, store handle    
     if ( pMsg->method == ATT_READ_BY_TYPE_RSP &&      
        pMsg->msg.readByTypeRsp.numPairs > 0 )
        {      
           p->simpleBLECharHdl[0] = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],                         
           pMsg->msg.readByTypeRsp.dataList[1] );      
           
           NPI_PrintValue("1 hdl", p->simpleBLECharHdl[0], 10);
    
           LCD_WRITE_STRING( "CHAR 1 Found", HAL_LCD_LINE_3 );      
           p->simpleBLEProcedureInProgress = TRUE;      
       }
     else // pMsg->msg.readByTypeRsp.numPairs is 0.
     {
       p->simpleBLEDiscState = BLE_DISC_STATE_CHAR6;    
       req.startHandle = p->simpleBLESvcStartHdl;  
       req.endHandle = p->simpleBLESvcEndHdl;   
       req.type.len = ATT_BT_UUID_SIZE;    
       req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR6_UUID);   
       req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR6_UUID);    
       GATT_DiscCharsByUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId );  
     }
  }        
  else if (p->simpleBLEDiscState == BLE_DISC_STATE_CHAR6)  //再找CHAR6
  {     // Characteristic found, store handle    
    
    NPI_PrintValue("6 pMsg->method", pMsg->method, 10);
    NPI_PrintValue("6 numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
    
    if ( (pMsg->method == ATT_READ_BY_TYPE_RSP)  &&     
    pMsg->msg.readByTypeRsp.numPairs > 0 )    
    {      
      p->simpleBLECharHdl[5] = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],                                      
      pMsg->msg.readByTypeRsp.dataList[1] );     
      LCD_WRITE_STRING( "CHAR 6 Found", HAL_LCD_LINE_4 );
     // p->simpleBLEProcedureInProgress = FALSE;  
      p->simpleBLEProcedureInProgress = TRUE;   
      
      NPI_PrintValue("6 hdl", p->simpleBLECharHdl[5], 10);
    }
    else
    {
      p->simpleBLEDiscState = BLE_DISC_STATE_CCC;
      
       req.startHandle = p->simpleBLESvcStartHdl;  
       req.endHandle = p->simpleBLESvcEndHdl;   
       req.type.len = ATT_BT_UUID_SIZE;    
       req.type.uuid[0] = LO_UINT16(0x2902);   
       req.type.uuid[1] = HI_UINT16(0x2902);    
       GATT_ReadUsingCharUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId ); 
    }
    //p->simpleBLEDiscState = BLE_DISC_STATE_IDLE;
  }
  else if (p->simpleBLEDiscState == BLE_DISC_STATE_CCC) //再找CCC
  {
    NPI_PrintValue("ccc pMsg->method", pMsg->method, 10);
    NPI_PrintValue("ccc numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
    if ( (pMsg->method == ATT_READ_BY_TYPE_RSP)  &&     
    pMsg->msg.readByTypeRsp.numPairs > 0 )  
    {
      
      ccc_dhl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],                                      
      pMsg->msg.readByTypeRsp.dataList[1] ); 
      NPI_PrintValue("ccc hdl", ccc_dhl, 10);
      
      
    }
    else													//CHAR6完全找到后 写CCC了直接
    {
       p->simpleBLEDiscState = BLE_DISC_STATE_IDLE;
       p->simpleBLEProcedureInProgress = FALSE;
       
      // Do a write
      attWriteReq_t req;
      
      req.handle = ccc_dhl;
      req.len = 2;
      req.sig = 0;
      req.cmd = 0;
      
      req.value[0] = 1; //写 1 打开notify选项
      req.value[1] = 0;
      
      uint8 status;
      status = GATT_WriteCharValue( p->simpleBLEConnHandle, &req, simpleBLETaskId ); // 发送数据
      if(SUCCESS != status)
      {
          NPI_PrintValue("Write Error :", status, 10);
      }
      else
      {
        NPI_PrintValue("ccc Write success :", status, 10);
      }
       
       
    }
   
  }
}
  1. LOG
  [KEY UP pressed!]
Discovering...



>>>GAP_DEVICE_INFO_EVENT
>>>GAP_DEVICE_INFO_EVENT
>>>GAP_DEVICE_INFO_EVENT
Key DOWN = Read RSSI
>>>GAP_DEVICE_DISCOVERY_EVENT
Devices Found 1[0]=0x84C2E43F4F47
Key CENTER = Connect
Connecting......
Idx= 0
Connecting
0x84C2E43F4F47
Idx= 0
>>>GAP_LINK_ESTABLISHED_EVENT
0x84C2E43F4F47
Key DOWN = Read RSSI
1 pMsg->method 9
1 numPairs 1
1 hdl 34
CHAR 1 Found
1 pMsg->method 96 pMsg->method 9
6 numPairs 1
CHAR 6 Found
6 hdl 43
6 pMsg->method 9
6 numPairs 0
ccc pMsg->method 9
ccc numPairs 1
ccc hdl 45ccc pMsg->method 9
ccc numPairs 0
ccc Write success : 0
Write sent: 0
ATT_HANDLE_VALUE_NOTI
ATT_HANDLE_VALUE_NOTI
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的划船机蓝牙从机连接手机app和手柄主机的配置例程: ``` #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; // 定义手柄和手机app主机的UUID #define HANDSET_UUID "0000fff0-0000-1000-8000-00805f9b34fb" #define APP_UUID "0000fff1-0000-1000-8000-00805f9b34fb" // 定义手柄和手机app主机特征值 uint8_t handsetValue[20] = {0}; uint8_t appValue[20] = {0}; // 划船机蓝牙从机的回调函数 class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { // 连接成功后初始化特征值 pCharacteristic->setValue(handsetValue, 20); pCharacteristic->notify(); } void onDisconnect(BLEServer* pServer) { // 断开连接后清空特征值 memset(handsetValue, 0, 20); } }; // 手柄主机的回调函数 class HandsetCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { // 接收手柄主机的数据并更新特征值 std::string value = pCharacteristic->getValue(); memcpy(handsetValue, value.c_str(), value.length()); } }; // 手机app主机的回调函数 class AppCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { // 接收手机app主机的数据并更新特征值 std::string value = pCharacteristic->getValue(); memcpy(appValue, value.c_str(), value.length()); } }; void setup() { BLEDevice::init("RowingMachine"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // 添加手柄主机特征值 BLEService* pHandsetService = pServer->createService(BLEUUID(HANDSET_UUID)); pCharacteristic = pHandsetService->createCharacteristic( BLEUUID(HANDSET_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->setCallbacks(new HandsetCallbacks()); // 添加手机app主机特征值 BLEService* pAppService = pServer->createService(BLEUUID(APP_UUID)); pCharacteristic = pAppService->createCharacteristic( BLEUUID(APP_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->setCallbacks(new AppCallbacks()); // 开始广播 pHandsetService->start(); pAppService->start(); BLEAdvertising* pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void loop() { // 定时更新特征值 pCharacteristic->setValue(handsetValue, 20); pCharacteristic->notify(); delay(10); } ``` 在上述代码中,我们使用了ESP32 BLE库来实现划船机蓝牙从机的功能。首先,在`setup()`函数中初始化BLE设备并创建BLE server,然后添加手柄主机和手机app主机的服务和特征值,并设置回调函数。在`loop()`函数中定时更新特征值。在手柄主机和手机app主机连接到从机时,会触发`onConnect()`回调函数,我们可以在该函数中初始化特征值。当手柄主机或手机app主机发送数据到从机时,会触发相应的回调函数,我们可以在回调函数中接收数据并更新特征值。当手柄主机或手机app主机断开连接时,会触发`onDisconnect()`回调函数,我们可以在该函数中清空特征值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值