diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino index b1624fa4..73acefca 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino @@ -30,7 +30,7 @@ void setup() Serial.println("---------------------------\n"); // Setup the BLE LED to be enabled on CONNECT - // Note: This is actually the default behaviour, but provided + // Note: This is actually the default behavior, but provided // here in case you want to control this LED manually via PIN 19 Bluefruit.autoConnLed(true); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino index d44b267a..3bcabdb8 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino @@ -16,8 +16,6 @@ * This sketch uses the HID Consumer Key API to send the Volume Down * key when pinShutter is grounded. This will cause your mobile device * to capture a photo when you are in the camera app. - * - * For Feather nRF52840 pinShutter is conveniently user switch. */ #include diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/pairing_pin/pairing_pin.ino b/libraries/Bluefruit52Lib/examples/Peripheral/pairing_pin/pairing_pin.ino new file mode 100644 index 00000000..b6c6640e --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/pairing_pin/pairing_pin.ino @@ -0,0 +1,149 @@ +/********************************************************************* + 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 +*********************************************************************/ + +/* This sketch demonstrates Pairing process using static Passkey aka PIN. + * This sketch is essentially the same as bleuart.ino except the BLE Uart + * service requires Security Mode with Man-In-The-Middle protection i.e + * using 6 digits PIN for pairing. + */ + +#include +#include +#include + +// Static PIN is 6 digits from 000000-999999 +#define PAIRING_PIN "123456" + +// BLE Service +BLEUart bleuart; // uart over ble + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLEUART Example"); + Serial.println("---------------------------\n"); + + // Setup the BLE LED to be enabled on CONNECT + // Note: This is actually the default behaviour, but provided + // here in case you want to control this LED manually via PIN 19 + Bluefruit.autoConnLed(true); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setName("Bluefruit52"); + + Serial.println("Setting pairing PIN to: " PAIRING_PIN); + Bluefruit.setPIN(PAIRING_PIN); + + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + + // Configure and Start BLE Uart Service + // Set Permission to access BLE Uart is to require man-in-the-middle protection + // This will cause central to perform pairing with static PIN we set above + Serial.println("Configure BLE Uart to require man-in-the-middle protection for PIN pairing"); + bleuart.setPermission(SECMODE_ENC_WITH_MITM); + bleuart.begin(); + + // Set up and start advertising + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); + Serial.println("Your phone should pop-up PIN input"); + Serial.println("Once connected, enter character(s) that you wish to send"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Forward data from HW Serial to BLEUART + while (Serial.available()) + { + // Delay to wait for enough input, since we have a limited transmission buffer + delay(2); + + uint8_t buf[64]; + int count = Serial.readBytes(buf, sizeof(buf)); + bleuart.write( buf, count ); + } + + // Forward from BLEUART to HW Serial + while ( bleuart.available() ) + { + uint8_t ch; + ch = (uint8_t) bleuart.read(); + Serial.write(ch); + } +} + +// callback invoked when central connects +void connect_callback(uint16_t conn_handle) +{ + // Get the reference to current connection + BLEConnection* connection = Bluefruit.Connection(conn_handle); + + char central_name[32] = { 0 }; + connection->getPeerName(central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @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.println(); + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); +} diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 8ea822dc..3b091ef1 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -656,20 +656,30 @@ void AdafruitBluefruit::setRssiCallback(rssi_callback_t fp) // Use Legacy SC static Passkey bool AdafruitBluefruit::setPIN(const char* pin) { - VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN ); + // back to open mode + if (pin == NULL) + { + _sec_param.lesc = 0; + _sec_param.mitm = 0; + _sec_param.bond = 1; + _sec_param.io_caps = BLE_GAP_IO_CAPS_NONE; + }else + { + VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN ); - // Static Passkey requires using - // - Legacy SC - // - IO cap: Display - // - MITM is on - _sec_param.lesc = 0; - _sec_param.mitm = 1; - _sec_param.bond = 1; - _sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY; + // Static Passkey requires using + // - Legacy SC + // - IO cap: Display + // - MITM is on + _sec_param.lesc = 0; + _sec_param.mitm = 1; + _sec_param.bond = 1; + _sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY; - ble_opt_t opt; - opt.gap_opt.passkey.p_passkey = (const uint8_t*) pin; - VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false); + ble_opt_t opt; + opt.gap_opt.passkey.p_passkey = (const uint8_t*) pin; + VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false); + } return true; }