Birkbeck College Mobile and Ubiquitous Computing IoT Lab Exercise 2

Dependencies:   BLE_API_Native_blog

Committer:
gkroussos
Date:
Sat Mar 07 16:34:53 2015 +0000
Revision:
0:e8fdba0ed044
MUC IoT Workshop v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gkroussos 0:e8fdba0ed044 1 /*----------------------------------------------------------------------------
gkroussos 0:e8fdba0ed044 2 LAB EXERCISE - Simple BLE Beacon
gkroussos 0:e8fdba0ed044 3 ----------------------------------------
gkroussos 0:e8fdba0ed044 4 In this exercise we will create a BLE beacon which transmits data to
gkroussos 0:e8fdba0ed044 5 other devices.
gkroussos 0:e8fdba0ed044 6
gkroussos 0:e8fdba0ed044 7 GOOD LUCK!
gkroussos 0:e8fdba0ed044 8 *----------------------------------------------------------------------------*/
gkroussos 0:e8fdba0ed044 9
gkroussos 0:e8fdba0ed044 10 #include "mbed.h"
gkroussos 0:e8fdba0ed044 11 #include "nRF51822n.h"
gkroussos 0:e8fdba0ed044 12
gkroussos 0:e8fdba0ed044 13 // Create an instance of the BLE radio driver and an instance of a Ticker
gkroussos 0:e8fdba0ed044 14 // <Write your code here>
gkroussos 0:e8fdba0ed044 15
gkroussos 0:e8fdba0ed044 16
gkroussos 0:e8fdba0ed044 17 // Create two DigitalOut instances for LEDs
gkroussos 0:e8fdba0ed044 18 // <Write your code here>
gkroussos 0:e8fdba0ed044 19
gkroussos 0:e8fdba0ed044 20
gkroussos 0:e8fdba0ed044 21 // Give your beacon a name and add some service data
gkroussos 0:e8fdba0ed044 22 const static char deviceName[] = "** <Insert beacon name> **";
gkroussos 0:e8fdba0ed044 23 const static int serviceData[] = {0x0000, 0xAAAABBB};
gkroussos 0:e8fdba0ed044 24
gkroussos 0:e8fdba0ed044 25 // Create two GapAdvertisingData instances. One for advertising data and another for the scan response
gkroussos 0:e8fdba0ed044 26 // <Write your code here>
gkroussos 0:e8fdba0ed044 27
gkroussos 0:e8fdba0ed044 28
gkroussos 0:e8fdba0ed044 29 // Create a GapAdvertisingParams instance for advertising parameters
gkroussos 0:e8fdba0ed044 30 // <Write your code here>
gkroussos 0:e8fdba0ed044 31
gkroussos 0:e8fdba0ed044 32
gkroussos 0:e8fdba0ed044 33 class GapEventHandler : public GapEvents {
gkroussos 0:e8fdba0ed044 34 // When a client device connects, turn off the advertising LED
gkroussos 0:e8fdba0ed044 35 virtual void onConnected(void){
gkroussos 0:e8fdba0ed044 36 // <Write your code here>
gkroussos 0:e8fdba0ed044 37
gkroussos 0:e8fdba0ed044 38 }
gkroussos 0:e8fdba0ed044 39
gkroussos 0:e8fdba0ed044 40 // When a client device disconnects we need to start advertising again and turn on the advertising LED
gkroussos 0:e8fdba0ed044 41 virtual void onDisconnected(void){
gkroussos 0:e8fdba0ed044 42 // <Write your code here>
gkroussos 0:e8fdba0ed044 43
gkroussos 0:e8fdba0ed044 44 }
gkroussos 0:e8fdba0ed044 45 };
gkroussos 0:e8fdba0ed044 46
gkroussos 0:e8fdba0ed044 47 void blinky(void){
gkroussos 0:e8fdba0ed044 48 // Blink one LED every 1 second
gkroussos 0:e8fdba0ed044 49 // <Write your code here>
gkroussos 0:e8fdba0ed044 50
gkroussos 0:e8fdba0ed044 51
gkroussos 0:e8fdba0ed044 52 }
gkroussos 0:e8fdba0ed044 53
gkroussos 0:e8fdba0ed044 54 /*----------------------------------------------------------------------------
gkroussos 0:e8fdba0ed044 55 MAIN function
gkroussos 0:e8fdba0ed044 56 *----------------------------------------------------------------------------*/
gkroussos 0:e8fdba0ed044 57
gkroussos 0:e8fdba0ed044 58 int main(void){
gkroussos 0:e8fdba0ed044 59 // Attach the Ticker
gkroussos 0:e8fdba0ed044 60 // <Write your code here>
gkroussos 0:e8fdba0ed044 61
gkroussos 0:e8fdba0ed044 62
gkroussos 0:e8fdba0ed044 63 // Set the GAP event handler
gkroussos 0:e8fdba0ed044 64 nrf.getGap().setEventHandler(new GapEventHandler());
gkroussos 0:e8fdba0ed044 65
gkroussos 0:e8fdba0ed044 66 // Start and reset the BLE radio
gkroussos 0:e8fdba0ed044 67 // <Write your code here>
gkroussos 0:e8fdba0ed044 68
gkroussos 0:e8fdba0ed044 69
gkroussos 0:e8fdba0ed044 70 // Add BLE-Only flag and advertising data as well as scan response data to the payload
gkroussos 0:e8fdba0ed044 71 advData.addFlags((GapAdvertisingData::Flags)(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE));
gkroussos 0:e8fdba0ed044 72 advData.addData(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t*)deviceName, sizeof(deviceName));
gkroussos 0:e8fdba0ed044 73 scanResponse.addData(GapAdvertisingData::SERVICE_DATA, (uint8_t*)serviceData, sizeof(serviceData));
gkroussos 0:e8fdba0ed044 74 advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
gkroussos 0:e8fdba0ed044 75 // Add the advertising and scan response data to the payload
gkroussos 0:e8fdba0ed044 76 // <Write your code here>
gkroussos 0:e8fdba0ed044 77
gkroussos 0:e8fdba0ed044 78
gkroussos 0:e8fdba0ed044 79 // Start advertising (make sure you've added all your data first) and set the advertising LED to high
gkroussos 0:e8fdba0ed044 80 // <Write your code here>
gkroussos 0:e8fdba0ed044 81
gkroussos 0:e8fdba0ed044 82
gkroussos 0:e8fdba0ed044 83 while(1) {
gkroussos 0:e8fdba0ed044 84 // Leave the while loop empty
gkroussos 0:e8fdba0ed044 85 }
gkroussos 0:e8fdba0ed044 86 }
gkroussos 0:e8fdba0ed044 87
gkroussos 0:e8fdba0ed044 88 // *******************************ARM University Program Copyright © ARM Ltd 2014*************************************