-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.ino
259 lines (221 loc) · 7.14 KB
/
temp.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
/* For a list of EIR data types see:
* https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
* Matching enum: cores/nRF5/SDK/components/softdevice/s132/headers/ble_gap.h */
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluefruit52 Central ADV Scan Example");
Serial.println("------------------------------------\n");
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
// SRAM usage required by SoftDevice will increase dramatically with number of connections
Bluefruit.begin(0, 1);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
/* Set the device name */
Bluefruit.setName("Bluefruit52 Central");
/* Set the LED interval for blinky pattern on BLUE LED */
Bluefruit.setConnLedInterval(250);
// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
/* Start Central Scanning
* - Enable auto scan if disconnected
* - Filter out packet with a min rssi
* - Interval = 100 ms, window = 50 ms
* - Use active scan (used to retrieve the optional scan response adv packet)
* - Start(0) = will scan forever since no timeout is given
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.filterRssi(-80);
//Bluefruit.Scanner.filterUuid(BLEUART_UUID_SERVICE); // only invoke callback if detect bleuart service
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
Bluefruit.Scanner.useActiveScan(true); // Request scan response data
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
Serial.println("Scanning ...");
}
void scan_callback(ble_gap_evt_adv_report_t* report)
{
PRINT_LOCATION();
uint8_t len = 0;
uint8_t buffer[32];
memset(buffer, 0, sizeof(buffer));
/* Display the timestamp and device address */
if (report->type.scan_response)
{
Serial.printf("[SR%10d] Packet received from ", millis());
}
else
{
Serial.printf("[ADV%9d] Packet received from ", millis());
}
// MAC is in little endian --> print reverse
Serial.printBufferReverse(report->peer_addr.addr, 6, ':');
Serial.print("\n");
/* Complete Local Name */
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer)))
{
//Serial.printf("%14s %s\n", "COMPLETE NAME", buffer);
if(memcmp(buffer, "Bluefruit52 Adv", sizeof("Bluefruit52 Adv")) == 0) { // same name
Serial.println("Name Matched!");
/* TX Power Level */
if (Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, buffer, sizeof(buffer)))
{
Serial.printf("%i, ", buffer[0]);
memset(buffer, 0, sizeof(buffer));
}
/* RSSI value */
Serial.printf("%d\n", "RSSI", report->rssi);
Serial.print("Target detected. Connecting ... ");
// Connect to device with bleuart service in advertising
Bluefruit.Central.connect(report);
}else{
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner resume() to continue scanning
Bluefruit.Scanner.resume();
}
memset(buffer, 0, sizeof(buffer));
}
/* Raw buffer contents *//*
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->data.len);
if (report->data.len)
{
Serial.printf("%15s", " ");
Serial.printBuffer(report->data.p_data, report->data.len, '-');
Serial.println();
}
*/
/* Adv Type *//*
Serial.printf("%14s ", "ADV TYPE");
if ( report->type.connectable )
{
Serial.print("Connectable ");
}else
{
Serial.print("Non-connectable ");
}
if ( report->type.directed )
{
Serial.println("directed");
}else
{
Serial.println("undirected");
}
*/
/* Shortened Local Name *//*
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, buffer, sizeof(buffer)))
{
Serial.printf("%14s %s\n", "SHORT NAME", buffer);
memset(buffer, 0, sizeof(buffer));
}
*/
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner resume() to continue scanning
//Bluefruit.Scanner.resume();
}
void connect_callback(uint16_t conn_handle)
{
Serial.println("Connected");
Bluefruit.disconnect(conn_handle);
/*
Serial.print("Dicovering Device Information ... ");
if ( clientDis.discover(conn_handle) )
{
Serial.println("Found it");
char buffer[32+1];
// read and print out Manufacturer
memset(buffer, 0, sizeof(buffer));
if ( clientDis.getManufacturer(buffer, sizeof(buffer)) )
{
Serial.print("Manufacturer: ");
Serial.println(buffer);
}
// read and print out Model Number
memset(buffer, 0, sizeof(buffer));
if ( clientDis.getModel(buffer, sizeof(buffer)) )
{
Serial.print("Model: ");
Serial.println(buffer);
}
Serial.println();
}else
{
Serial.println("Found NONE");
}
Serial.print("Dicovering Battery ... ");
if ( clientBas.discover(conn_handle) )
{
Serial.println("Found it");
Serial.print("Battery level: ");
Serial.print(clientBas.read());
Serial.println("%");
}else
{
Serial.println("Found NONE");
}
Serial.print("Discovering BLE Uart Service ... ");
if ( clientUart.discover(conn_handle) )
{
Serial.println("Found it");
Serial.println("Enable TXD's notify");
clientUart.enableTXD();
Serial.println("Ready to receive from peripheral");
}else
{
Serial.println("Found NONE");
// disconnect since we couldn't find bleuart service
Bluefruit.disconnect(conn_handle);
}
*/
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}
/*
void printUuid16List(uint8_t* buffer, uint8_t len)
{
Serial.printf("%14s %s", "16-Bit UUID");
for(int i=0; i<len; i+=2)
{
uint16_t uuid16;
memcpy(&uuid16, buffer+i, 2);
Serial.printf("%04X ", uuid16);
}
Serial.println();
}
void printUuid128List(uint8_t* buffer, uint8_t len)
{
(void) len;
Serial.printf("%14s %s", "128-Bit UUID");
// Print reversed order
for(int i=0; i<16; i++)
{
const char* fm = (i==4 || i==6 || i==8 || i==10) ? "-%02X" : "%02X";
Serial.printf(fm, buffer[15-i]);
}
Serial.println();
}
*/
void loop()
{
// nothing to do
}