BLE
Dependencies: ADT7410 BLE_API mbed nRF51822
See https://developer.mbed.org/users/yasuyuki/notebook/BLE/
Diff: main.cpp
- Revision:
- 2:d5bdd080d451
- Parent:
- 1:6458709fb459
--- a/main.cpp Fri Jul 10 15:19:15 2015 +0000
+++ b/main.cpp Wed Sep 09 17:26:34 2015 +0000
@@ -2,8 +2,9 @@
// BLE demo for mbed_HRM1017
//
// successful combination
-// mbed=rev.97/BLE_API=rev.341/nRF51822=rev.111
-// mbed=rev.101/BLE_API=rev.738/nRF51822=rev.385
+//#define REV1 // mbed=rev.97/BLE_API=rev.341/nRF51822=rev.111
+//#define REV2 // mbed=rev.101/BLE_API=rev.738/nRF51822=rev.385
+#define REV3 // mbed=rev.106/BLE_API=rev.798/nRF51822=rev.428
//
// (C)Copyright 2015 All rights reserved by Y.Onodera
// http://einstlab.web.fc2.com
@@ -11,11 +12,24 @@
#if defined(TARGET_HRM1017)
#include "mbed.h"
#include "ADT7410.h"
-//#include "BLEDevice.h" // BLEライブラリのヘッダ BLE_API=rev.341
-#include "BLE.h" // BLEライブラリのヘッダ BLE_API=rev.738
-#include "BatteryService.h" // BatteryServiceのヘッダ
-#include "HeartRateService.h" // HeartRateServiceのヘッダ
-#include "HealthThermometerService.h" // HealthThermometerServiceのヘッダ
+#ifdef REV1
+#include "BLEDevice.h" // BLEライブラリのヘッダ BLE_API=rev.341
+#include "BatteryService.h" // BatteryServiceのヘッダ
+#include "HeartRateService.h" // HeartRateServiceのヘッダ
+#include "HealthThermometerService.h" // HealthThermometerServiceのヘッダ
+#endif
+#ifdef REV2
+#include "BLE.h" // BLEライブラリのヘッダ BLE_API=rev.738
+#include "BatteryService.h" // BatteryServiceのヘッダ
+#include "HeartRateService.h" // HeartRateServiceのヘッダ
+#include "HealthThermometerService.h" // HealthThermometerServiceのヘッダ
+#endif
+#ifdef REV3
+#include "ble/BLE.h" // BLEライブラリのヘッダ BLE_API=rev.798
+#include "ble/services/BatteryService.h" // BatteryServiceのヘッダ
+#include "ble/services/HeartRateService.h" // HeartRateServiceのヘッダ
+#include "ble/services/HealthThermometerService.h" // HealthThermometerServiceのヘッダ
+#endif
#define THERMO
//#define HEART
@@ -54,13 +68,12 @@
// BLE接続処理
-//#define OLD
-#ifdef OLD
+#ifdef REV1
// BLE_API=rev.341
void connectionCallback(
Gap::Handle_t handle,
Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr,
-const Gap::ConnectionParams_t *params)
+const Gap::ConnectionParams_t *params)
#else
// BLE_API=rev.738
void connectionCallback(
@@ -68,7 +81,11 @@
#endif
{
led2 = 1;
+#ifdef REV3
+ ble.gap().stopAdvertising();
+#else
ble.stopAdvertising();
+#endif
}
@@ -76,12 +93,12 @@
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
led2 = 0;
- ble.startAdvertising();
+ ble.gap().startAdvertising();
}
// BLE接続タイムアウト処理
-#ifdef OLD
+#ifdef REV1
// BLE_API=rev.341
void connectTimeoutCallback(void)
{
@@ -108,42 +125,84 @@
// LE_LIMITED_DISCOVERABLE = 期間限定で検出可能デバイス
// LE_GENERAL_DISCOVERABLE = 検出可能デバイス
// BREDR_NOT_SUPPORTED = Bluetooth Classicは未サポート, BLEオンリー
+#ifdef REV3
+ ble.gap().accumulateAdvertisingPayload(
+ GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
+#else
ble.accumulateAdvertisingPayload(
GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
+#endif
// UUIDリストを設定
+#ifdef REV3
+ ble.gap().accumulateAdvertisingPayload(
+ GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
+ (uint8_t *)uuid16_list,
+ sizeof(uuid16_list));
+#else
ble.accumulateAdvertisingPayload(
GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
(uint8_t *)uuid16_list,
sizeof(uuid16_list));
+#endif
// デバイス名を設定(for GAP), default Device Name="nRF5x"
// ble.setDeviceName(DEVICE_NAME);
// デバイス名を設定(for Advertising)
+#ifdef REV3
+ ble.gap().accumulateAdvertisingPayload(
+ GapAdvertisingData::COMPLETE_LOCAL_NAME,
+ (const uint8_t *)DEVICE_NAME,
+// strlen(DEVICE_NAME));
+ sizeof(DEVICE_NAME));
+#else
ble.accumulateAdvertisingPayload(
GapAdvertisingData::COMPLETE_LOCAL_NAME,
(const uint8_t *)DEVICE_NAME,
// strlen(DEVICE_NAME));
sizeof(DEVICE_NAME));
+#endif
// ADV_CONNECTABLE_UNDIRECTED = 接続可能デバイスに設定
+#ifdef REV3
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+#else
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+#endif
// アドバタイジング間隔の設定=100ms, 0.625ms単位
+#ifdef REV3
+ ble.gap().setAdvertisingInterval(160); // 0.625ms * 160 = 100ms
+// ble.gap().setAdvertisingInterval(1600); // 0.625ms * 1600 = 1000ms
+#else
ble.setAdvertisingInterval(160); // 0.625ms * 160 = 100ms
// ble.setAdvertisingInterval(1600); // 0.625ms * 1600 = 1000ms
+#endif
// Appearance
#ifdef THERMO
+#ifdef REV3
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
+#else
ble.accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
#endif
+#endif
#ifdef HEART
+#ifdef REV3
+// ble.gap().setAppearance(ENERIC_HEART_RATE_SENSOR);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
+#else
// ble.setAppearance(ENERIC_HEART_RATE_SENSOR);
ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
#endif
+#endif
// アドバタイジングタイムアウト=180sec(デフォルトは0でタイムアウトなし)
// ble.setAdvertisingTimeout(180);
+
// アドバタイジング開始
+#ifdef REV3
+ ble.gap().startAdvertising();
+#else
ble.startAdvertising();
-
+#endif
}
@@ -162,9 +221,15 @@
tick.attach(&tickerCallback, 1);
// BLEのイベント関数を設定
+#ifdef REV3
+ ble.gap().onConnection(connectionCallback);
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gap().onTimeout(connectTimeoutCallback);
+#else
ble.onConnection(connectionCallback);
ble.onDisconnection(disconnectionCallback);
ble.onTimeout(connectTimeoutCallback);
+#endif
// BatteryServiceのインスタンス生成=サービスを登録
uint8_t batt=0;