|
| 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 the central API(). A additional bluefruit |
| 17 | + * that has bleuart as peripheral is required for the demo. |
| 18 | + */ |
| 19 | +#include <bluefruit.h> |
| 20 | + |
| 21 | +BLEClientUart clientUart; // bleuart client |
| 22 | + |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + Serial.begin(115200); |
| 26 | + while ( !Serial ) delay(10); // for nrf52840 with native usb |
| 27 | + |
| 28 | + Serial.println("Bluefruit52 Central BLEUART Example"); |
| 29 | + Serial.println("-----------------------------------\n"); |
| 30 | + |
| 31 | + // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 |
| 32 | + // SRAM usage required by SoftDevice will increase dramatically with number of connections |
| 33 | + Bluefruit.begin(0, 1); |
| 34 | + |
| 35 | + Bluefruit.setName("Bluefruit52 Central"); |
| 36 | + |
| 37 | + // Init BLE Central Uart Serivce |
| 38 | + clientUart.begin(); |
| 39 | + clientUart.setRxCallback(bleuart_rx_callback); |
| 40 | + |
| 41 | + // Increase Blink rate to different from PrPh advertising mode |
| 42 | + Bluefruit.setConnLedInterval(250); |
| 43 | + |
| 44 | + // Callbacks for Central |
| 45 | + Bluefruit.Central.setConnectCallback(connect_callback); |
| 46 | + Bluefruit.Central.setDisconnectCallback(disconnect_callback); |
| 47 | + |
| 48 | + /* Start Central Scanning |
| 49 | + * - Enable auto scan if disconnected |
| 50 | + * - Interval = 100 ms, window = 80 ms |
| 51 | + * - Don't use active scan |
| 52 | + * - Start(timeout) with timeout = 0 will scan forever (until connected) |
| 53 | + */ |
| 54 | + Bluefruit.Scanner.setRxCallback(scan_callback); |
| 55 | + Bluefruit.Scanner.restartOnDisconnect(true); |
| 56 | + Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms |
| 57 | + Bluefruit.Scanner.useActiveScan(false); |
| 58 | + Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Callback invoked when scanner pick up an advertising data |
| 63 | + * @param report Structural advertising data |
| 64 | + */ |
| 65 | +void scan_callback(ble_gap_evt_adv_report_t* report) |
| 66 | +{ |
| 67 | + // Check if advertising contain BleUart service |
| 68 | + if ( Bluefruit.Scanner.checkReportForService(report, clientUart) ) |
| 69 | + { |
| 70 | + Serial.print("BLE UART service detected. Connecting ... "); |
| 71 | + |
| 72 | + // Connect to device with bleuart service in advertising |
| 73 | + Bluefruit.Central.connect(report); |
| 74 | + }else |
| 75 | + { |
| 76 | + // For Softdevice v6: after received a report, scanner will be paused |
| 77 | + // We need to call Scanner resume() to continue scanning |
| 78 | + Bluefruit.Scanner.resume(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Callback invoked when an connection is established |
| 84 | + * @param conn_handle |
| 85 | + */ |
| 86 | +void connect_callback(uint16_t conn_handle) |
| 87 | +{ |
| 88 | + Serial.println("Connected"); |
| 89 | + |
| 90 | + Serial.print("Discovering BLE Uart Service ... "); |
| 91 | + if ( clientUart.discover(conn_handle) ) |
| 92 | + { |
| 93 | + Serial.println("Found it"); |
| 94 | + |
| 95 | + Serial.println("Enable TXD's notify"); |
| 96 | + clientUart.enableTXD(); |
| 97 | + |
| 98 | + Serial.println("Ready to receive from peripheral"); |
| 99 | + }else |
| 100 | + { |
| 101 | + Serial.println("Found NONE"); |
| 102 | + |
| 103 | + // disconnect since we couldn't find bleuart service |
| 104 | + Bluefruit.disconnect(conn_handle); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Callback invoked when a connection is dropped |
| 110 | + * @param conn_handle |
| 111 | + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h |
| 112 | + */ |
| 113 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 114 | +{ |
| 115 | + (void) conn_handle; |
| 116 | + (void) reason; |
| 117 | + |
| 118 | + Serial.println("Disconnected"); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Callback invoked when uart received data |
| 123 | + * @param uart_svc Reference object to the service where the data |
| 124 | + * arrived. In this example it is clientUart |
| 125 | + */ |
| 126 | +void bleuart_rx_callback(BLEClientUart& uart_svc) |
| 127 | +{ |
| 128 | + Serial.print("[RX]: "); |
| 129 | + |
| 130 | + while ( uart_svc.available() ) |
| 131 | + { |
| 132 | + Serial.print( (char) uart_svc.read() ); |
| 133 | + } |
| 134 | + |
| 135 | + Serial.println(); |
| 136 | +} |
| 137 | + |
| 138 | +void loop() |
| 139 | +{ |
| 140 | + if ( Bluefruit.Central.connected() ) |
| 141 | + { |
| 142 | + // Not discovered yet |
| 143 | + if ( clientUart.discovered() ) |
| 144 | + { |
| 145 | + // Discovered means in working state |
| 146 | + // Get Serial input and send to Peripheral |
| 147 | + if ( Serial.available() ) |
| 148 | + { |
| 149 | + delay(2); // delay a bit for all characters to arrive |
| 150 | + |
| 151 | + char str[20+1] = { 0 }; |
| 152 | + Serial.readBytes(str, 20); |
| 153 | + |
| 154 | + clientUart.print( str ); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments