BLE UART example for Nucloe board and Bluetooth LE Nucleo shield.

Dependencies:   Nucleo_BLE_API Nucleo_BLE_BlueNRG mbed

Warning: Deprecated!

Supported drivers and applications can be found at this link.

Files at this revision

API Documentation at this revision

Comitter:
sjallouli
Date:
Wed Dec 24 18:05:05 2014 +0000
Parent:
0:3e2ce2bb50b9
Commit message:
Use USER_BUTTON interrupt to send data
; use ble.updateCharacteristicValue() to send data

Changed in this revision

Nucleo_BLE_BlueNRG.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
diff -r 3e2ce2bb50b9 -r 69e44344edaa Nucleo_BLE_BlueNRG.lib
--- a/Nucleo_BLE_BlueNRG.lib	Fri Dec 19 19:53:05 2014 +0000
+++ b/Nucleo_BLE_BlueNRG.lib	Wed Dec 24 18:05:05 2014 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ST-Americas-mbed-Team/code/Nucleo_BLE_BlueNRG/#be1cb4be316f
+http://developer.mbed.org/teams/ST-Americas-mbed-Team/code/Nucleo_BLE_BlueNRG/#905715088a9b
diff -r 3e2ce2bb50b9 -r 69e44344edaa main.cpp
--- a/main.cpp	Fri Dec 19 19:53:05 2014 +0000
+++ b/main.cpp	Wed Dec 24 18:05:05 2014 +0000
@@ -16,87 +16,148 @@
  
 #include "mbed.h"
 #include "BLEDevice.h"
+#include "DeviceInformationService.h"
 #include "UARTService.h"
 #include "Utils.h"
 
+const static char     DEVICE_NAME[]        = "BlueNRG_UART";
+uint8_t c = 'A';
 
-#define BLE_UART_SEND(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); }
-
+extern bool user_button_pressed;
+bool connected = false;
+bool UpdatedEnabled = false;
 
 BLEDevice  ble;
-DigitalOut led1(LED1);
-DigitalIn mybutton(USER_BUTTON);
+UARTService *uartServicePtr;
 
-bool connected = false;
-
-UARTService *uartServicePtr;
- 
+/* Callback called when the device is disconnected */
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
-    ble.startAdvertising();
-    connected = false;
+  DEBUG("Disconnected!\n\r");
+  DEBUG("Restarting the advertising process\n\r");
+
+  ble.startAdvertising();
+  connected = false;
 }
 
+/* Callback called when the device is connected */
 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
 {
   DEBUG("Connected\r\n");
+
   connected = true;
 }
 
+/* Not working */
+void onDataSent(unsigned count)
+{
+  DEBUG("onDataSent\r\n");
+}
+
+/* Not working */
 void onDataWritten(const GattCharacteristicWriteCBParams *params)
 {
-    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
-        uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
-        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+  DEBUG("--onDataWritten\r\n");
+  DEBUG("---charHandle               : %d\r\n", params->charHandle);
+  DEBUG("---getTXCharacteristicHandle: %d\r\n", uartServicePtr->getTXCharacteristicHandle());
+  DEBUG("---len: %d\r\n", params->len);
+    
+  if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) 
+  {
+    DEBUG("received %u bytes\n\r", params->len);
+             
+    if(params->data[0] == 0x00)
+    {
+      //Do something
     }
+    else
+    {
+      //Do something else
+    }
+  }
+}
+
+/* Callback called when the client enables updates */
+void onUpdatesEnabled(uint16_t attributeHandle)
+{
+  DEBUG("**onUpdatesEnabled**\r\n");
+  UpdatedEnabled = true;
+}
+
+/* Callback called when the client disable updates */
+void onUpdatesDisabled(uint16_t attributeHandle)
+{
+  DEBUG("**onUpdatesDisabled**\r\n");
+  UpdatedEnabled = false;
 }
  
-void periodicCallback(void)
-{
-    led1 = !led1;
-}
- 
+/* Main */
 int main(void)
 {
-    led1 = 1;
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
+  DEBUG("Initialising \n\r");
+  ble.init();
+#if 1
+/* Set callback functions */
+  ble.onDisconnection(disconnectionCallback);
+  ble.onConnection(connectionCallback);
+  ble.onDataWritten(onDataWritten);
+  ble.onDataSent(onDataSent);
+  ble.onUpdatesEnabled(onUpdatesEnabled);
+  ble.onUpdatesDisabled(onUpdatesDisabled);
  
-    DEBUG("Initialising \n\r");
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onConnection(connectionCallback);
-    ble.onDataWritten(onDataWritten);
- 
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
- 
-    ble.setAdvertisingInterval(160);
-    ble.startAdvertising();
+  DeviceInformationService deviceInfo(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+  /* setup advertising */
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+  ble.setAdvertisingType          (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME            , (const uint8_t *)"BlueNRG_UART"          , sizeof("BlueNRG_UART") - 1);
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME             , (uint8_t *)DEVICE_NAME                   , sizeof(DEVICE_NAME));
+
+  /* Start advertising */
+  ble.setAdvertisingInterval(160);
+  ble.startAdvertising();
  
-    UARTService uartService(ble);
-    uartServicePtr = &uartService;
- 
- 
-    while (true) 
+  UARTService uartService(ble);
+  uartServicePtr = &uartService;
+
+
+  while (true) 
+  {
+    ble.waitForEvent();
+        
+    if(connected == true)
     {
-        ble.waitForEvent();
-        
-        if(connected == true)
+      if ((user_button_pressed == true) && (UpdatedEnabled == true))
+      {
+        user_button_pressed = false;
+        DEBUG("Current Char: %c\r\n",c);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), &c, 1);
+                
+        c++;
+                
+        if(c == ('Z'+1))
         {
-            if (mybutton == 0)
-            {
-                DEBUG("ABCDEFGHIJKLMNOPQRST");
-                BLE_UART_SEND("ABCDEFGHIJKLMNOPQRST");
-
-                while(mybutton == 0);
-            }
+          c = 'A';
         }
+      }
     }
+  }
+#else
+  while (true) 
+  {
+    if (user_button_pressed == true)
+    {
+      user_button_pressed = false;
+      DEBUG("Current Char: %c\r\n",c);
+                
+      c++;
+                
+      if(c == ('Z'+1))
+      {
+        c = 'A';
+      }
+    }
+  }
+#endif
 }