Example of BLE scan/connect/service discovery
Fork of BLE_LEDBlinker by
Diff: main.cpp
- Revision:
- 12:f0ffc006e62d
- Parent:
- 11:023d96b0e427
- Child:
- 13:75f95a5cf9c1
--- a/main.cpp Tue Jan 12 10:49:03 2016 +0000
+++ b/main.cpp Fri Dec 09 09:12:57 2016 +0000
@@ -31,7 +31,7 @@
}
void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
- if (params->peerAddr[0] != 0x37) { /* !ALERT! Alter this filter to suit your device. */
+ if (params->peerAddr[0] != 0xF9) { /* !ALERT! Alter this filter to suit your device. */
return;
}
printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
@@ -42,6 +42,7 @@
}
void serviceDiscoveryCallback(const DiscoveredService *service) {
+ printf("serviceDiscoveryCallback\n");
if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
} else {
@@ -55,10 +56,20 @@
}
void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
- printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
- if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
- ledCharacteristic = *characteristicP;
- triggerLedCharacteristic = true;
+ if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
+ printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
+ if (characteristicP->getUUID().getShortUUID() == 0x2a19) { /* !ALERT! Alter this filter to suit your device. */
+ ledCharacteristic = *characteristicP;
+ triggerLedCharacteristic = true;
+ }
+ }
+ else {
+ printf(" C UUID-");
+ const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
+ for (unsigned i = (UUID::LENGTH_OF_LONG_UUID) - 1; i < UUID::LENGTH_OF_LONG_UUID; i--) {
+ printf("%02x ", longUUIDBytes[i]);
+ }
+ printf(" valueAttr[%u] props[%x]\r\n", characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
}
}
@@ -67,9 +78,10 @@
}
void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+ printf("connectionCallback\n");
if (params->role == Gap::CENTRAL) {
BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
- BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
+ BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback);
}
}
@@ -144,7 +156,7 @@
/* SpinWait for initialization to complete. This is necessary because the
* BLE object is used in the main loop below. */
while (ble.hasInitialized() == false) { /* spin loop */ }
-
+ printf("init DONE\r\n");
while (true) {
if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
triggerLedCharacteristic = false;
