Added support for obtaining BLE device name by parsing advertising data.
Fork of BLE_BlueNRG by
Diff: BlueNRGGap.cpp
- Revision:
- 7:e293b0f43dc7
- Parent:
- 6:08cfc94b5f49
diff -r 08cfc94b5f49 -r e293b0f43dc7 BlueNRGGap.cpp --- a/BlueNRGGap.cpp Fri Jul 25 09:09:13 2014 +0000 +++ b/BlueNRGGap.cpp Thu Jul 31 11:09:28 2014 +0000 @@ -16,10 +16,13 @@ #include "BlueNRGDevice.h" #include "mbed.h" +#include "Payload.h" //Local Variables const uint8_t *device_name; +Serial pc1(USBTX, USBRX); // tx, rx + /**************************************************************************/ /*! @brief Sets the advertising parameters and payload for the device @@ -65,16 +68,53 @@ } /* Make sure we have a payload! */ - if (advData.getPayloadLen() == 0) { + if (advData.getPayloadLen() <= 0) { return BLE_ERROR_PARAM_OUT_OF_RANGE; + } else { //set the advData here in some local variable so that startAdvertising can use it. + Payload load(advData.getPayload(), advData.getPayloadLen()); + + for(uint8_t index=0; index<load.getPayloadUnitCount(); index++) { + //UnitPayload unitLoad = load.getPayLoadAtIndex(index); + switch(load.getIDAtIndex(index)) { + case GapAdvertisingData::FLAGS: /* ref *Flags */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS: /**< Incomplete list of 16-bit Service IDs */ + break; + case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS: /**< Complete list of 16-bit Service IDs */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS: /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ + break; + case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS: /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ + break; + case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */ + break; + case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS: /**< Complete list of 128-bit Service IDs */ + break; + case GapAdvertisingData::SHORTENED_LOCAL_NAME: /**< Shortened Local Name */ + break; + case GapAdvertisingData::COMPLETE_LOCAL_NAME: /**< Complete Local Name */ + device_name = load.getDataAtIndex(index); + break; + case GapAdvertisingData::TX_POWER_LEVEL: /**< TX Power Level (in dBm) */ + break; + case GapAdvertisingData::DEVICE_ID: /**< Device ID */ + break; + case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE: /**< Slave :Connection Interval Range */ + break; + case GapAdvertisingData::SERVICE_DATA: /**< Service Data */ + break; + case GapAdvertisingData::APPEARANCE: /**< \ref Appearance */ + break; + case GapAdvertisingData::ADVERTISING_INTERVAL: /**< Advertising Interval */ + break; + case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA: /**< Manufacturer Specific Data */ + break; + + } + } + //const uint8_t *payload = advData.getPayload(); + } - - //set the advData here in some local variable so that startAdvertising can use it. - if (advData.getPayloadLen() > 0) { - const uint8_t *payload = advData.getPayload(); - device_name = advData.getPayload(); - } - return BLE_ERROR_NONE; } @@ -139,8 +179,27 @@ //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28],device_name[29],device_name[30], device_name[31], // device_name[32], device_name[33], device_name[34], device_name[35], device_name[36]}; - const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17], - device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]}; + #if 0 + pc1.printf("Hello Teraterm\n"); + /*const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[13],device_name[14],device_name[15],device_name[16], device_name[17], + device_name[18], device_name[19], device_name[20], device_name[21], device_name[22]};*/ + const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'h', 'd', 'd'}; + #else + + char *namePtr = new char[1+sizeof(device_name)]; + namePtr[0] = AD_TYPE_COMPLETE_LOCAL_NAME; + pc1.printf("now setting name to: %s...\n", device_name); + pc1.printf("device_name length=%d", sizeof(namePtr)); + int i=0; + while(device_name[i]!=0) { + namePtr[i+1] = device_name[i]; + pc1.printf("%c\n", namePtr[i+1]); + i++; + } + + const char* local_name = (const char*)namePtr; + + #endif //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[27],device_name[28]}; const LongUUID_t HRM_SERVICE_UUID_128 = {0x18, 0x0D};