{"id":398,"date":"2015-08-04T21:36:24","date_gmt":"2015-08-05T04:36:24","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=398"},"modified":"2015-08-04T21:39:19","modified_gmt":"2015-08-05T04:39:19","slug":"creating-a-bluetooth-le-beacon-with-nrf51-and-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/nrf51\/beacon\/","title":{"rendered":"Creating a Bluetooth LE Beacon with nRF51 and Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to create a Bluetooth LE Beacon using the Nordic nRF51-DK board and VisualGDB. Before you begin, install VisualGDB 5.0 and get a Bluetooth LE-capable Android Phone. It is also recommended to follow the <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/nrf51\">basic nRF51 tutorial<\/a> to ensure that your board hardware and drivers work without any problems.<\/p>\n<ol>\n<li>Open the VisualGDB Embedded Project wizard in Visual Studio and specify the name and location for your project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/01-beaconprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-400\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/01-beaconprj.png\" alt=\"01-beaconprj\" width=\"800\" height=\"476\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/01-beaconprj.png 800w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/01-beaconprj-300x179.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/li>\n<li>Proceed with the default settings on the first page (New Project -&gt; Embedded Binary):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/02-binprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-401\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/02-binprj.png\" alt=\"02-binprj\" width=\"702\" height=\"571\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/02-binprj.png 702w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/02-binprj-300x244.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/li>\n<li>On the Device Selection page select your Nordic device and specify a softdevice supporting the Bluetooth LE Peripheral role:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/03-device4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-402\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/03-device4.png\" alt=\"03-device\" width=\"702\" height=\"638\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/03-device4.png 702w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/03-device4-300x273.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/li>\n<li>On the Sample Selection page select the Bluetooth LE Beacon sample and specify your board type below (the board type is printed on a sticker on your board):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/04-beacon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-403\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/04-beacon.png\" alt=\"04-beacon\" width=\"702\" height=\"638\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/04-beacon.png 702w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/04-beacon-300x273.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/li>\n<li>Finally select a debug method. The nRF51-DK board comes with an on-board Segger J-Link, so in this tutorial we will use the Segger debug method. Ensure that you select the &#8220;Reset device after programming&#8221; checkbox, as otherwise the softdevice initialization code will be bypassed resulting in random crashes:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/05-debug2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-404\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/05-debug2.png\" alt=\"05-debug\" width=\"702\" height=\"638\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/05-debug2.png 702w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/05-debug2-300x273.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. Then build it with Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/06-build2.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-405\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/06-build2.png\" alt=\"06-build\" width=\"695\" height=\"608\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/06-build2.png 695w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/06-build2-300x262.png 300w\" sizes=\"(max-width: 695px) 100vw, 695px\" \/><\/a><\/li>\n<li>Start debugging the firmware with F5. Then install and open the nRF Toolbox app on your Android device:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/07-beacons.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-406\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/07-beacons.png\" alt=\"07-beacons\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/07-beacons.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/07-beacons-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Click the &#8220;Beacons&#8221; item. If you have not added any beacons before, the list will be empty. Click the &#8220;+&#8221; sign to add the beacon:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/08-addbeacon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-407\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/08-addbeacon.png\" alt=\"08-addbeacon\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/08-addbeacon.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/08-addbeacon-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Click the left arrow near the &#8220;Beacon Config&#8221; text to return to the beacon list:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/09-beacon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-408\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/09-beacon.png\" alt=\"09-beacon\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/09-beacon.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/09-beacon-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a>If the beacon parameters window never appears, try closing all Bluetooth-related apps and if that does not help, restart your phone.<\/li>\n<li>Ensure that the beacon list now shows your beacon.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/10-beaconlist.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-409\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/10-beaconlist.png\" alt=\"10-beaconlist\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/10-beaconlist.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/10-beaconlist-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Now we will modify the beacon example to emulate 2 different beacons alternating between them when button #1 is pressed. The beacon information is contained in the advertisement packet that is stored in the <strong>m_beacon_info<\/strong> variable:\n<pre class=\"\">static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] =\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/**&lt; Information advertised by the Beacon. *\/\r\n{\r\n\u00a0\u00a0\u00a0 APP_DEVICE_TYPE,\u00a0\u00a0\u00a0\u00a0 \/\/ Manufacturer specific information. Specifies the device type in this \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ implementation. \r\n\u00a0\u00a0\u00a0 APP_ADV_DATA_LENGTH, \/\/ Manufacturer specific information. Specifies the length of the \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ manufacturer specific data in this implementation.\r\n\u00a0\u00a0\u00a0 APP_BEACON_UUID,\u00a0\u00a0\u00a0\u00a0 \/\/ 128 bit UUID value. \r\n\u00a0\u00a0\u00a0 APP_MAJOR_VALUE,\u00a0\u00a0\u00a0\u00a0 \/\/ Major arbitrary value that can be used to distinguish between Beacons. \r\n\u00a0\u00a0\u00a0 APP_MINOR_VALUE,\u00a0\u00a0\u00a0\u00a0 \/\/ Minor arbitrary value that can be used to distinguish between Beacons. \r\n\u00a0\u00a0\u00a0 APP_MEASURED_RSSI\u00a0\u00a0\u00a0 \/\/ Manufacturer specific information. The Beacon's measured TX power in \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ this implementation. \r\n};<\/pre>\n<\/li>\n<li>The contents of the advertisement packet is passed to the softdevice by the <strong>advertising_init()<\/strong> function:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/11-adv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-410\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/11-adv.png\" alt=\"11-adv\" width=\"695\" height=\"607\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/11-adv.png 695w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/11-adv-300x262.png 300w\" sizes=\"(max-width: 695px) 100vw, 695px\" \/><\/a><\/li>\n<li>You can use the code map to visualize the functions involved in the initialization:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/14-codemap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-414\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/14-codemap.png\" alt=\"14-codemap\" width=\"695\" height=\"550\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/14-codemap.png 695w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/14-codemap-300x237.png 300w\" sizes=\"(max-width: 695px) 100vw, 695px\" \/><\/a><\/li>\n<li>To add the necessary modifications we will first need to enable button initialization in the call to bsp_init(). Modify the line calling bsp_init() from main() to include the button initialization:\n<pre class=\"\">\u00a0\u00a0\u00a0 err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);<\/pre>\n<p>Then include the &#8220;nrf_delay.h&#8221; file and replace the loop inside main() with the following:<\/p>\n<pre class=\"\">\u00a0\u00a0\u00a0 for (;; )\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 power_manage();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 bool state;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 if (bsp_button_is_pressed(0, &amp;state) == NRF_SUCCESS &amp;&amp; state)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 m_beacon_info[21] ^= 1;\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 advertising_init();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 nrf_delay_ms(1000);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }<\/pre>\n<\/li>\n<li>Now each time the button #1 is pressed, the least-significant bit in the beacon minor ID will change. The delay after a call to <strong>advertising_init()<\/strong> ensures that the same button press won&#8217;t be handled twice. Press F5 to start debugging and rapidly press the button #1:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/button1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-411\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/button1.jpg\" alt=\"button1\" width=\"640\" height=\"320\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/button1.jpg 640w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/button1-300x150.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/li>\n<li>Observe how the beacon disappears from the list. Click the &#8220;+&#8221; button again and notice how the beacon ID has changed by 1:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/12-beacon2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-412\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/12-beacon2.png\" alt=\"12-beacon2\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/12-beacon2.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/12-beacon2-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Return to the beacon list and observe how pressing the button #1 causes another beacon to appear and the previous one to disappear soon after:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/13-2beacons.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-413\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/13-2beacons.png\" alt=\"13-2beacons\" width=\"700\" height=\"394\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/13-2beacons.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/13-2beacons-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a Bluetooth LE Beacon using the Nordic nRF51-DK board and VisualGDB. Before you begin,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[53,96,56,95],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/398\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}