ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

【沁恒蓝牙开发】接收与区分APP通过128位 UUID下发数据

【沁恒蓝牙开发】接收与区分APP通过128位 UUID下发数据 名称UUID用途Generic Access Profile (GAP)00001800-0000-1000-8000-00805f9b34fb设备名称、外观、连接参数Generic Attribute Profile (GATT)00001801-0000-1000-8000-00805f9b34fb服务变更通知Device Information Service0000180a-0000-1000-8000-00805f9b34fb制造商、型号、固件版本等Battery Service0000180f-0000-1000-8000-00805f9b34fb电池电量Heart Rate Service0000180d-0000-1000-8000-00805f9b34fb心率监测默认例程只提供了接收16位UUID的Demo如果修改了128位UUID后就需要添加下文代码实现打印 和 区分 APP通过128位UUID下发的数据。有两个方式可以区分不同的uuid具体如下方式1直接完整的对比128位UUID。方式2找出两个128位UUID不同的1/2/3/4个不同的字节就对比这几个字节。代码方式1匹配128位simpleProfilechar1UUID 换为自定义的UUIDif(tmos_memcmp(pAttr-type.uuid,simpleProfilechar1UUID,ATT_UUID_SIZE)){PRINT(------1-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}elseif(tmos_memcmp(pAttr-type.uuid,simpleProfilechar2UUID,ATT_UUID_SIZE)){PRINT(------2-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}else{// Should never get here! (characteristics 2 and 4 do not have write permissions)statusATT_ERR_ATTR_NOT_FOUND;}方式2匹配特定位uint16_tuuidBUILD_UINT16(pAttr-type.uuid[0],pAttr-type.uuid[1]);//可以按照实际情况调整合并的数组位PRINT( 128-bit UUID %0x\n,uuid);switch(uuid){caseSIMPLEPROFILE_CHAR1_UUID:{PRINT(------1-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}break;caseSIMPLEPROFILE_CHAR2_UUID:{PRINT(------2-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}break;}完整代码/********************************************************************* * fn simpleProfile_WriteAttrCB * * brief Validate attribute data prior to a write operation * * param connHandle - connection message was received on * param pAttr - pointer to attribute * param pValue - pointer to data to be written * param len - length of data * param offset - offset of the first octet to be written * * return Success or Failure */staticbStatus_tsimpleProfile_WriteAttrCB(uint16_tconnHandle,gattAttribute_t*pAttr,uint8_t*pValue,uint16_tlen,uint16_toffset,uint8_tmethod){bStatus_tstatusSUCCESS;uint8_tnotifyApp0xFF;// If attribute permissions require authorization to write, return errorif(gattPermitAuthorWrite(pAttr-permissions)){// Insufficient authorizationreturn(ATT_ERR_INSUFFICIENT_AUTHOR);}if(pAttr-type.lenATT_BT_UUID_SIZE){// 16-bit UUIDuint16_tuuidBUILD_UINT16(pAttr-type.uuid[0],pAttr-type.uuid[1]);switch(uuid){caseSIMPLEPROFILE_CHAR1_UUID://Validate the value// Make sure its not a blob operif(offset0){if(lenSIMPLEPROFILE_CHAR1_LEN){statusATT_ERR_INVALID_VALUE_SIZE;}}else{statusATT_ERR_ATTR_NOT_LONG;}//Write the valueif(statusSUCCESS){tmos_memcpy(pAttr-pValue,pValue,SIMPLEPROFILE_CHAR1_LEN);notifyAppSIMPLEPROFILE_CHAR1;}break;caseSIMPLEPROFILE_CHAR3_UUID://Validate the value// Make sure its not a blob operif(offset0){if(lenSIMPLEPROFILE_CHAR3_LEN){statusATT_ERR_INVALID_VALUE_SIZE;}}else{statusATT_ERR_ATTR_NOT_LONG;}//Write the valueif(statusSUCCESS){tmos_memcpy(pAttr-pValue,pValue,SIMPLEPROFILE_CHAR3_LEN);notifyAppSIMPLEPROFILE_CHAR3;}break;caseGATT_CLIENT_CHAR_CFG_UUID:statusGATTServApp_ProcessCCCWriteReq(connHandle,pAttr,pValue,len,offset,GATT_CLIENT_CFG_NOTIFY);break;default:// Should never get here! (characteristics 2 and 4 do not have write permissions)statusATT_ERR_ATTR_NOT_FOUND;break;}}else{/*-------------------方式 1 (两种方式二选一即可) ---------------------------*/// 128-bit UUIDif(tmos_memcmp(pAttr-type.uuid,simpleProfilechar1UUID,ATT_UUID_SIZE)){PRINT(------1-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}elseif(tmos_memcmp(pAttr-type.uuid,simpleProfilechar1UUID,ATT_UUID_SIZE)){PRINT(------2-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}else{// Should never get here! (characteristics 2 and 4 do not have write permissions)statusATT_ERR_ATTR_NOT_FOUND;}/*-------------------方式 2 (两种方式二选一即可) ---------------------------*/uint16_tuuidBUILD_UINT16(pAttr-type.uuid[0],pAttr-type.uuid[1]);PRINT( 128-bit UUID %0x\n,uuid);switch(uuid){caseSIMPLEPROFILE_CHAR1_UUID:{PRINT(------1-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}break;caseSIMPLEPROFILE_CHAR2_UUID:{PRINT(------2-------\n);for(uint16_ti0;ilen;i){PRINT( %02x ,pValue[i]);}PRINT(\n-------------\n);}break;}}// If a charactersitic value changed then callback function to notify application of changeif((notifyApp!0xFF)simpleProfile_AppCBssimpleProfile_AppCBs-pfnSimpleProfileChange){simpleProfile_AppCBs-pfnSimpleProfileChange(notifyApp,pValue,len);}return(status);}
返回列表