Peripheral_1

Dependencies:   BLE_API mbed nRF51822

Files at this revision

API Documentation at this revision

Comitter:
DDUdui
Date:
Tue Aug 18 07:06:07 2015 +0000
Commit message:
Peripheral_1

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 482d3fcc94f4 BLE_API.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_API.lib	Tue Aug 18 07:06:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#8d316a3271a8
diff -r 000000000000 -r 482d3fcc94f4 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 18 07:06:07 2015 +0000
@@ -0,0 +1,117 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "ble/DiscoveredCharacteristic.h"
+#include "ble/DiscoveredService.h"
+BLE        ble;
+DigitalOut led1(LED1);
+Serial pc(USBTX, USBRX);
+
+
+void periodicCallback(void)
+{
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+}
+
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
+     if (params->peerAddr[0] == 0xcb) { /* !ALERT! Alter this filter to suit your device. */
+        return;
+    }
+    pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
+           params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
+           params->rssi, params->isScanResponse, params->type);
+
+    for (unsigned index = 0; index < params->advertisingDataLen; index++) {
+        pc.printf("%02x ", params->advertisingData[index]);
+    }
+    pc.printf("\r\n");
+ /* DUMP_ADV_DATA */
+/*
+     ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+*/
+}
+
+void serviceDiscoveryCallback(const DiscoveredService *service) {
+    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
+        pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
+    } else {
+        printf("S UUID-");
+        const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
+        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
+            printf("%02x", longUUIDBytes[i]);
+        }
+        pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
+    }
+}
+ 
+void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
+  
+}
+ 
+void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
+    pc.printf("terminated SD for handle %u\r\n", connectionHandle);
+}
+ 
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+    pc.printf("Connected!!\r\n");
+    ble.stopAdvertising();
+    wait(1);
+    if (params->role == Gap::CENTRAL) {
+        ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
+    }
+    ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION);
+}
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    pc.printf("Disconnected\r\n");
+    pc.printf("Rescan\r\n");
+    ble.startScan(advertisementCallback);
+    ble.startAdvertising();
+}
+    
+
+int main(void)
+{
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+    ble.onConnection(connectionCallback);
+    ble.onDisconnection(disconnectionCallback);
+
+    ble.init();
+    
+    pc.baud(9600);
+    pc.printf("Observer Init \r\n");
+
+
+    /* */
+     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                    (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1);
+   
+    ble.gap().setAdvertisingInterval(100); /* 1second. */
+    ble.gap().startAdvertising();
+
+ 
+    
+    while (true) {
+        ble.waitForEvent();
+    }
+}
diff -r 000000000000 -r 482d3fcc94f4 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Aug 18 07:06:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b9ad9a133dc7
\ No newline at end of file
diff -r 000000000000 -r 482d3fcc94f4 nRF51822.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nRF51822.lib	Tue Aug 18 07:06:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#ca9c9c2cfc6a