天天看点

BLE协议栈入门四(Client端读写属性表)

客户端读写服务器端属性表中的Characteristic。

/**
 * Write Request format.
 */
 //发送数据打包,然后调用GATT_WriteCharValue发送
 // Do a write
typedef struct
{
  uint16 handle;               //!< Handle of the attribute to be written (must be first field)
  uint8 len;                   //!< Length of value
  uint8 value[ATT_MTU_SIZE-3]; //!< Value of the attribute to be written
  uint8 sig;                   //!< Authentication Signature status (not included (0), valid (1), invalid (2))
  uint8 cmd;                   //!< Command Flag
} attWriteReq_t;

attWriteReq_t req;

req.handle = simpleBLECharHdl; //simpleBLECharHdl要写的属性在属性表中的handle,如何获得的?
req.len = 15;
req.value[0] = 0x01;
req.sig = 0;
req.cmd = 0;
status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );


// Do a read
/**
 * Read Request format.
 */
typedef struct
{
  uint16 handle; //!< Handle of the attribute to be read (must be first field)
} attReadReq_t;

attReadReq_t req;

req.handle = simpleBLECharHdl;//simpleBLECharHdl要读的属性在属性表中的handle,如何获得的?
status = GATT_ReadCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
           

simpleBLECharHdl要读写的属性在属性表中的handle,如何获得的?

通过GATT_ReadUsingCharUUID

// If procedure complete
    if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP  && 
           pMsg->hdr.status == bleProcedureComplete ) ||
         ( pMsg->method == ATT_ERROR_RSP ) )
    {
      if ( simpleBLESvcStartHdl != 0 )
      {
        // Discover characteristic
        simpleBLEDiscState = BLE_DISC_STATE_CHAR;
        
        req.startHandle = simpleBLESvcStartHdl;
        req.endHandle = 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_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId );
      }
    }


    // Characteristic found, store handle
    if ( pMsg->method == ATT_READ_BY_TYPE_RSP && 
         pMsg->msg.readByTypeRsp.numPairs > 0 )
    {
      simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],
                                       pMsg->msg.readByTypeRsp.dataList[1] );
      
      LCD_WRITE_STRING( "Simple Svc Found", HAL_LCD_LINE_1 );
      simpleBLEProcedureInProgress = FALSE;
    }