|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 based Bluefruit LE modules |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | +
|
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + All text above, and the splash screen below must be included in |
| 12 | + any redistribution |
| 13 | +*********************************************************************/ |
| 14 | + |
| 15 | +/* |
| 16 | + * This sketch demonstrate how to run both Central and Peripheral roles |
| 17 | + * at the same tiem |
| 18 | + */ |
| 19 | +#include <bluefruit.h> |
| 20 | + |
| 21 | +// Peripheral uart service |
| 22 | +BLEUart bleuart; |
| 23 | + |
| 24 | +// Central uart client |
| 25 | +BLEClientUart clientUart; |
| 26 | + |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + Serial.begin(115200); |
| 30 | + |
| 31 | + Serial.println("Bluefruit52 Dual Role BLEUART Example"); |
| 32 | + Serial.println("-------------------------------------\n"); |
| 33 | + |
| 34 | + // Enable both peripheral and central |
| 35 | + Bluefruit.begin(true, true); |
| 36 | + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 |
| 37 | + Bluefruit.setTxPower(4); |
| 38 | + Bluefruit.setName("Bluefruit52"); |
| 39 | + |
| 40 | + // Callbacks for Central |
| 41 | + Bluefruit.Central.setConnectCallback(cent_connect_callback); |
| 42 | + Bluefruit.Central.setDisconnectCallback(cent_disconnect_callback); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + // Init BLE Central Uart Serivce |
| 47 | + clientUart.begin(); |
| 48 | + clientUart.setRxCallback(bleuart_rx_callback); |
| 49 | + |
| 50 | + |
| 51 | + /* Start Central Scanning |
| 52 | + * - Enable auto scan if disconnected |
| 53 | + * - Interval = 100 ms, window = 80 ms |
| 54 | + * - Don't use active scan |
| 55 | + * - Start(timeout) with timeout = 0 will scan forever (until connected) |
| 56 | + */ |
| 57 | + Bluefruit.Scanner.setRxCallback(scan_callback); |
| 58 | + Bluefruit.Scanner.restartOnDisconnect(true); |
| 59 | + Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms |
| 60 | + Bluefruit.Scanner.useActiveScan(false); |
| 61 | + Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Callback invoked when scanner pick up an advertising data |
| 66 | + * @param report Structural advertising data |
| 67 | + */ |
| 68 | +void scan_callback(ble_gap_evt_adv_report_t* report) |
| 69 | +{ |
| 70 | + // Check if advertising contain BleUart service |
| 71 | + if ( Bluefruit.Scanner.checkReportForService(report, clientUart) ) |
| 72 | + { |
| 73 | + Serial.print("BLE UART service detected. Connecting ... "); |
| 74 | + |
| 75 | + // Connect to device with bleuart service in advertising |
| 76 | + Bluefruit.Central.connect(report); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Callback invoked when an connection is established |
| 82 | + * @param conn_handle |
| 83 | + */ |
| 84 | +void cent_connect_callback(uint16_t conn_handle) |
| 85 | +{ |
| 86 | + Serial.println("Connected"); |
| 87 | + |
| 88 | + Serial.print("Dicovering DIS ... "); |
| 89 | + if ( clientDis.discover(conn_handle) ) |
| 90 | + { |
| 91 | + Serial.println("Found it"); |
| 92 | + char buffer[32+1]; |
| 93 | + |
| 94 | + // read and print out Manufacturer |
| 95 | + memset(buffer, 0, sizeof(buffer)); |
| 96 | + if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ) |
| 97 | + { |
| 98 | + Serial.print("Manufacturer: "); |
| 99 | + Serial.println(buffer); |
| 100 | + } |
| 101 | + |
| 102 | + // read and print out Model Number |
| 103 | + memset(buffer, 0, sizeof(buffer)); |
| 104 | + if ( clientDis.getModel(buffer, sizeof(buffer)) ) |
| 105 | + { |
| 106 | + Serial.print("Model: "); |
| 107 | + Serial.println(buffer); |
| 108 | + } |
| 109 | + |
| 110 | + Serial.println(); |
| 111 | + } |
| 112 | + |
| 113 | + Serial.print("Discovering BLE Uart Service ... "); |
| 114 | + |
| 115 | + if ( clientUart.discover(conn_handle) ) |
| 116 | + { |
| 117 | + Serial.println("Found it"); |
| 118 | + |
| 119 | + Serial.println("Enable TXD's notify"); |
| 120 | + clientUart.enableTXD(); |
| 121 | + |
| 122 | + Serial.println("Ready to receive from peripheral"); |
| 123 | + }else |
| 124 | + { |
| 125 | + Serial.println("Found NONE"); |
| 126 | + |
| 127 | + // disconect since we couldn't find bleuart service |
| 128 | + Bluefruit.Central.disconnect(conn_handle); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Callback invoked when a connection is dropped |
| 134 | + * @param conn_handle |
| 135 | + * @param reason |
| 136 | + */ |
| 137 | +void cent_disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 138 | +{ |
| 139 | + (void) conn_handle; |
| 140 | + (void) reason; |
| 141 | + |
| 142 | + Serial.println("Disconnected"); |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Callback invoked when uart received data |
| 147 | + * @param uart_svc Reference object to the service where the data |
| 148 | + * arrived. In this example it is clientUart |
| 149 | + */ |
| 150 | +void bleuart_rx_callback(BLEClientUart& uart_svc) |
| 151 | +{ |
| 152 | + Serial.print("[RX]: "); |
| 153 | + |
| 154 | + while ( uart_svc.available() ) |
| 155 | + { |
| 156 | + Serial.print( (char) uart_svc.read() ); |
| 157 | + } |
| 158 | + |
| 159 | + Serial.println(); |
| 160 | +} |
| 161 | + |
| 162 | +void loop() |
| 163 | +{ |
| 164 | + if ( Bluefruit.Central.connected() ) |
| 165 | + { |
| 166 | + // Not discovered yet |
| 167 | + if ( clientUart.discovered() ) |
| 168 | + { |
| 169 | + // Discovered means in working state |
| 170 | + // Get Serial input and send to Peripheral |
| 171 | + if ( Serial.available() ) |
| 172 | + { |
| 173 | + delay(2); // delay a bit for all characters to arrive |
| 174 | + |
| 175 | + char str[20+1] = { 0 }; |
| 176 | + Serial.readBytes(str, 20); |
| 177 | + |
| 178 | + clientUart.print( str ); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
0 commit comments