Flying Silicon Sensor
bleInterface.cpp
Go to the documentation of this file.
1
24// BLE Interface for Adafruit BLE
25#include "bleInterface.h"
26
27#include "debug.h"
28
29// Define to select the UUID model to be used
30#define USE_HM10_UUID
31
35void BleInterface::cbConnect(uint16_t conn_handle) {
36
37 // Get reference to current connection
38 BLEConnection *conn = Bluefruit.Connection(conn_handle);
39
40 // see ble_gap.h:
41 // #define BLE_GAP_PHY_AUTO 0x00 /**< Automatic PHY selection. Refer @ref sd_ble_gap_phy_update for more information.*/
42 // #define BLE_GAP_PHY_1MBPS 0x01 /**< 1 Mbps PHY. */
43 // #define BLE_GAP_PHY_2MBPS 0x02 /**< 2 Mbps PHY. */
44 // #define BLE_GAP_PHY_CODED 0x04 /**< Coded PHY. */
45 // #define BLE_GAP_PHY_NOT_SET 0xFF /**< PHY is not configured. */
46
47 // Request PHY 1 Mbit
48 conn->requestPHY(BLE_GAP_PHY_1MBPS);
49 // Request to update data length
50 conn->requestDataLengthUpdate();
51 // Request MTU size
52 conn->requestMtuExchange(MtuSize);
53
54 // Request shorter connection interval (in unit of 1.25 ms)
55 // (needed for constant data flow)
56 // bool requestConnectionParameter(uint16_t conn_interval, uint16_t slave_latency = BLE_GAP_CONN_SLAVE_LATENCY, uint16_t sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS/10);
57 conn->requestConnectionParameter(12, 0);
58
59 // char central_name[32+1] = { 0 };
60 // conn->getPeerName(central_name, sizeof(central_name));
61}
62
66void BleInterface::cbDisconnect(uint16_t conn_handle, uint8_t reason) {
67 (void) conn_handle;
68 (void) reason;
69}
70
76
77 uint8_t ch = 0x00;
78 while (bleuart.read(& ch, 1)) {
79 ; // nothing
80 }
81}
82
88
89 return Bluefruit.connected() > 0 ? 1 : 0;
90}
91
96
97 Bluefruit.Advertising.restartOnDisconnect(false);
98 Bluefruit.Advertising.stop();
99 uint16_t hd = Bluefruit.connHandle();
100 Bluefruit.disconnect(hd);
101}
102
103/* For future extension
104void bleuart_rx_callback(uint16_t conn_hdl) {
105 (void) conn_hdl;
106}
107
108void bleuart_notify_callback(uint16_t conn_hdl, bool enabled) {
109 (void) conn_hdl;
110 (void) enabled;
111}
112*/
113
119void BleInterface::sendData(const char *buf, int len) {
120
121 bleuart.write(buf, len);
122}
123
128
129 Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
130 Bluefruit.Advertising.addTxPower();
131
132 // Add name to BLE 'advertising set'
133 // Maximum data length is 31
134 Bluefruit.Advertising.addName();
135 // Add name also to secondary scan response packet
136 Bluefruit.ScanResponse.addName();
137
138 // Add device information service
139 // Bluefruit.Advertising.addService(bledis);
140
141 // Add UART service
142 Bluefruit.Advertising.addService(bleuart);
143
144 // Enable auto advertising if disconnected
145 Bluefruit.Advertising.restartOnDisconnect(true);
146
147 // Set the advertising interval in units of 0.625 ms
148 // Fast mode = 20 ms, Slow mode = 152.5 ms (units of 0.625 ms)
149 // see https://developer.apple.com/library/content/qa/qa1931/_index.html
150 Bluefruit.Advertising.setInterval(BLE_ADV_INTERVAL_FAST_DFLT, BLE_ADV_INTERVAL_SLOW_DFLT);
151
152 // Timeout for fast mode 30 seconds
153 Bluefruit.Advertising.setFastTimeout(BLE_ADV_FAST_TIMEOUT_DFLT);
154
155 // Never stop advertising
156 Bluefruit.Advertising.start(0);
157
158 // Bluefruit.Advertising.setStopCallback(adv_stop_callback);
159 // Bluefruit.Advertising.start(ADV_TIMEOUT);
160}
161
166void BleInterface::init(const char *deviceName) {
167
168 // Bluefruit must NOT flash the LED
169 Bluefruit.autoConnLed(false);
170
171 // NOTE: All config* function must be called before begin()
172
173 // NOTE: Do not use the bandwith setting function configPrphBandwidth()
174 // It hides only the specific settings for configPrphConn
175
176 // BLE Event length: Time set aside for this connection on every connection interval in 1.25 ms units
177
178 // Best with MTU size 23 (9.5/sec)
179 Bluefruit.configPrphConn(MtuSize, 3, 4, 1);
180 // Best with MTU size 43 (9.5/sec) - fifo can be a bit shorter
181 // Bluefruit.configPrphConn(MtuSize, 3, 3, 1);
182
183 Bluefruit.begin();
184
185 // Set max power for nRF52840
186 // -40dBm, -20dBm, -16dBm, -12dBm, -8dBm, -4dBm, 0dBm, +2dBm, +3dBm, +4dBm, +5dBm, +6dBm, +7dBm, +8dBm
187 Bluefruit.setTxPower(-12);
188
189 // Name used also in advertising if addName() called
190 Bluefruit.setName(deviceName);
191 // Bluefruit.setName(getMcuUniqueID());
192
193 Bluefruit.Periph.setConnectCallback(cbConnect);
194 Bluefruit.Periph.setDisconnectCallback(cbDisconnect);
195
196 // optional: Device Information Service
197 // bledis.setManufacturer("Seeed"); // or use MNA
198 // bledis.setModel("XIAOnRF"); // or use MMO
199 // ...
200 // bledis.begin();
201
202 bleuart.begin();
203}
Provides data transmission over BLE (encapsulating BLE details)
void init(const char *deviceName)
int isConnected(void)
static void cbDisconnect(uint16_t conn_handle, uint8_t reason)
static const int MtuSize
BLE MTU size.
void sendData(const char *buf, int len)
FsUart bleuart
Customized UART service.
void poll(void)
static void cbConnect(uint16_t conn_handle)
void startAdv(void)
void shutdown(void)
Debug log macros.