TechplexEngineer
7/26/2017 - 10:57 AM

Check for a series of bytes in a byte array. https://repl.it/JjHx/1

Check for a series of bytes in a byte array. https://repl.it/JjHx/1

#include <stdint.h>
#include <stdio.h>

bool checkUuidInScanData(uint8_t* advData, uint8_t advDataLen, uint8_t* bleUUID, uint8_t bleUUIDLen)
{
	for (int i = advDataLen; i > bleUUIDLen; --i)
	{
		for (int j = 0; j < bleUUIDLen; ++j)
		{
		  printf("i=%d a=%x \t j=%d u=%x\n", i, advData[i-j], j, bleUUID[j]);
			if (advData[i-j] != bleUUID[j])
				break;
			if (j == bleUUIDLen-1)
				return true;
		}
	}
	return false;
}
#define SERVICE_UUID_LENGTH 16
int main(void) {

  uint8_t metawear_service_uuid[SERVICE_UUID_LENGTH] = {0x32,0x6a,0x90,0x00,0x85,0xcb,0x91,0x95,0xd9,0xdd,0x46,0x4c,0xfb,0xba,0xe7,0x5a };

  uint8_t dat1[] = {0x02,0x01,0x06,0x13,0xFF,0x4C,0x00,0x0C,0x0E,0x00,0x0B,0x9C,0xFA,0xFD,0x21,0x67,0xFE,0x75,0x09,0xC3,0xEE,0x42,0x0A,0x95,0x91,0xCB,0x85,0x00,0x90,0x6A,0x32};
  uint8_t data0[] =  {0x90,0x08,0x4D,0x65,0x74,0x61,0x57,0x65,0x61,0x72,0x02,0x01,0x06,0x11,0x06,0x5A,0xE7,0xBA,0xFB,0x4C,0x46,0xDD,0xD9,0x95,0x91,0xCB,0x85,0x00,0x90,0x6A,0x32};
  uint8_t dataLen = 32;

  bool result = checkUuidInScanData(data, dataLen, metawear_service_uuid, SERVICE_UUID_LENGTH);
  if (result)
    printf("True");
  else
    printf("False");
}