Skip to content

Commit

Permalink
Merge pull request ARMmbed#65 from pan-/fix_led_blinker_instruction
Browse files Browse the repository at this point in the history
Improve BLE_LEDBlinker example.
  • Loading branch information
pan- committed Mar 14, 2016
2 parents 8e4183f + 04f8769 commit 00535f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
17 changes: 8 additions & 9 deletions BLE_LEDBlinker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ The example uses two applications running on two different devices:

1. The first device - the central - runs the application ``BLE_LEDBlinker`` from this repository. This application sends an on/off toggle over BLE.

1. The second device - the peripheral - runs the application [``BLE_LED``](https://github.com/ARMmbed/ble-examples/tree/master/BLE_LED) to respond to the toggle.
1. The second device - the peripheral - runs the application [``BLE_LED``](https://github.com/ARMmbed/ble-examples/tree/master/BLE_LED) to respond to the toggle.

The toggle simply turns the LED on the peripheral device on and off.

# Running the application

## Requirements

Hardware requirements are in the [main readme](https://github.com/ARMmbed/ble-examples/blob/master/README.md).

This example requires *two* devices.
Expand All @@ -22,7 +22,7 @@ This example requires *two* devices.

You will need to build both applications and flash each one to a different board.

Please note: The application ``BLE_LEDBlinker`` in this repository checks a device-specific parameter: ``peerAddr``. Before you build the application, you need to give this parameter the MAC address of your *peripheral* device (the device running the ``BLE_LED`` application).
Please note: The application ``BLE_LEDBlinker`` in this repository initiate a connection to all ble devices which advertise "LED" as complete local name. By default, the application `BLE_LED` advertise "LED" as complete local name. If you change the local name advertised by the application `BLE_LED` you should reflect your change in this application by changing the value of the constant `PEER_NAME` in `main.cpp`.

**Tip:** You may notice that the application also checks the LED characteristic's UUID; you don't need to change this parameter's value, because it already matches the UUID provided by the second application, ``BLE_LED``.

Expand All @@ -44,19 +44,18 @@ Building instructions for all mbed OS samples are in the [main readme](https://g

## Monitoring the application through a serial port

You can run ``BLE_LEDBlinker`` and see that it works properly by monitoring its serial output.
You can run ``BLE_LEDBlinker`` and see that it works properly by monitoring its serial output.

You need a terminal program to listen to the output through a serial port. You can download one, for example:

* Tera Term for Windows.
* CoolTerm for Mac OS X.
* GNU Screen for Linux.

To see the application's output:
To see the application's output:

1. Check which serial port your device is connected to.
1. Check the baud rate of your device.
1. Run a terminal program with the correct serial port and baud rate. For example, to use GNU Screen, run: ``screen /dev/tty.usbmodem1412 9600``.
1. The application should start printing the toggle's value to the terminal.
1. Run a terminal program with the correct serial port and set the baud rate to 9600. For example, to use GNU Screen, run: ``screen /dev/tty.usbmodem1412 9600``.
1. The application should start printing the toggle's value to the terminal.

**Note:** ``BLE_LEDBlinker`` will not run properly if the ``BLE_LED`` application is not running on a second device. The terminal will show a few print statements, but you will not be able to see the application in full operation.
**Note:** ``BLE_LEDBlinker`` will not run properly if the ``BLE_LED`` application is not running on a second device. The terminal will show a few print statements, but you will not be able to see the application in full operation.
35 changes: 28 additions & 7 deletions BLE_LEDBlinker/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,41 @@
DigitalOut alivenessLED(LED1, 1);
static DiscoveredCharacteristic ledCharacteristic;
static bool triggerLedCharacteristic;
static const char PEER_NAME[] = "LED";

void periodicCallback(void) {
alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
}

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
if (params->peerAddr[0] != 0x29) { /* !ALERT! Alter this filter to suit your device. */
return;
// parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME
// The advertising payload is a collection of key/value records where
// byte 0: length of the record excluding this byte
// byte 1: The key, it is the type of the data
// byte [2..N] The value. N is equal to byte0 - 1
for (uint8_t i = 0; i < params->advertisingDataLen; ++i) {

const uint8_t record_length = params->advertisingData[i];
if (record_length == 0) {
continue;
}
const uint8_t type = params->advertisingData[i + 1];
const uint8_t* value = params->advertisingData + i + 2;
const uint8_t value_length = record_length - 1;

if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) {
printf(
"adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %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
);
BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
break;
}
}
i += record_length;
}
printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %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);

BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
}

void serviceDiscoveryCallback(const DiscoveredService *service) {
Expand Down

0 comments on commit 00535f4

Please sign in to comment.