|
| 1 | +/* |
| 2 | + Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <CurieBLE.h> |
| 20 | + |
| 21 | +/* |
| 22 | + This sketch example partially implements the standard Bluetooth Low-Energy Battery service. |
| 23 | + For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx |
| 24 | +*/ |
| 25 | + |
| 26 | +#define MAX_IMU_RECORD 1 |
| 27 | + |
| 28 | +struct bt_le_conn_param conn_param = {0x18, 0x28, 0, 400}; |
| 29 | +typedef struct { |
| 30 | + int index; |
| 31 | + unsigned int slot[3]; |
| 32 | +} imuFrameType; |
| 33 | + |
| 34 | +imuFrameType imuBuf[MAX_IMU_RECORD]; |
| 35 | +BLECentral bleCentral; // BLE Central Device (the board you're programming) |
| 36 | + |
| 37 | +BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); |
| 38 | +BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID |
| 39 | + BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to |
| 40 | + // get notifications if this characteristic changes |
| 41 | + |
| 42 | +void ble_connected(BLEHelper &role) |
| 43 | +{ |
| 44 | + BLEPeripheralHelper&peripheral = *(BLEPeripheralHelper*)(&role); |
| 45 | + Serial.println("Connected"); |
| 46 | + |
| 47 | + // Start discovery the profiles in peripheral device |
| 48 | + peripheral.discover(); |
| 49 | +} |
| 50 | + |
| 51 | +void bleImuCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) |
| 52 | +{ |
| 53 | + // Peripheral wrote new value to characteristic by Notification/Indication |
| 54 | + const unsigned char *cvalue = characteristic.value(); |
| 55 | + const imuFrameType *value = (const imuFrameType *)cvalue; |
| 56 | + Serial.print("\r\nCharacteristic event, written: "); |
| 57 | + Serial.print(value->index); |
| 58 | + Serial.print("\t"); |
| 59 | + Serial.print(value->slot[0]); |
| 60 | + Serial.print("\t"); |
| 61 | + Serial.print(value->slot[1]); |
| 62 | + Serial.print("\t"); |
| 63 | + Serial.println(value->slot[2]); |
| 64 | +} |
| 65 | + |
| 66 | +bool adv_found(uint8_t type, |
| 67 | + const uint8_t *data, |
| 68 | + uint8_t data_len, |
| 69 | + void *user_data) |
| 70 | +{ |
| 71 | + bt_addr_le_t *addr = (bt_addr_le_t *)user_data; |
| 72 | + int i; |
| 73 | + |
| 74 | + Serial.print("[AD]:"); |
| 75 | + Serial.print(type); |
| 76 | + Serial.print(" data_len "); |
| 77 | + Serial.println(data_len); |
| 78 | + |
| 79 | + switch (type) |
| 80 | + { |
| 81 | + case BT_DATA_UUID128_SOME: |
| 82 | + case BT_DATA_UUID128_ALL: |
| 83 | + { |
| 84 | + if (data_len % MAX_UUID_SIZE != 0) |
| 85 | + { |
| 86 | + Serial.println("AD malformed"); |
| 87 | + return true; |
| 88 | + } |
| 89 | + struct bt_uuid * serviceuuid = bleImuService.uuid(); |
| 90 | + for (i = 0; i < data_len; i += MAX_UUID_SIZE) |
| 91 | + { |
| 92 | + if (memcmp (((struct bt_uuid_128*)serviceuuid)->val, &data[i], MAX_UUID_SIZE) != 0) |
| 93 | + { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + // Accept the advertisement |
| 98 | + if (!bleCentral.stopScan()) |
| 99 | + { |
| 100 | + Serial.println("Stop LE scan failed"); |
| 101 | + continue; |
| 102 | + } |
| 103 | + Serial.println("Connecting"); |
| 104 | + // Connect to peripheral |
| 105 | + bleCentral.connect(addr, &conn_param); |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +void setup() { |
| 115 | + Serial.begin(115200); // initialize serial communication |
| 116 | + pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected |
| 117 | + |
| 118 | + bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten); |
| 119 | + |
| 120 | + /* Set a local name for the BLE device |
| 121 | + This name will appear in advertising packets |
| 122 | + and can be used by remote devices to identify this BLE device |
| 123 | + The name can be changed but maybe be truncated based on space |
| 124 | + left in advertisement packet */ |
| 125 | + bleCentral.addAttribute(bleImuService); // Add the BLE IMU service |
| 126 | + bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic |
| 127 | + |
| 128 | + /* Setup callback */ |
| 129 | + bleCentral.setAdvertiseHandler(adv_found); |
| 130 | + bleCentral.setEventHandler(BLEConnected, ble_connected); |
| 131 | + |
| 132 | + /* Now activate the BLE device. It will start continuously transmitting BLE |
| 133 | + advertising packets and will be visible to remote BLE central devices |
| 134 | + until it receives a new connection */ |
| 135 | + bleCentral.begin(); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +void loop() |
| 140 | +{ |
| 141 | + delay(2000); |
| 142 | +} |
| 143 | + |
0 commit comments