nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.37k stars 710 forks source link

Cross-transport Key Derivation #986

Open caleedubya opened 4 years ago

caleedubya commented 4 years ago

Might anyone know how to go about implementing this? The idea is that if you connect to BLE and have BT Classic running you only have to connect to one and then the other will be auto connected. Would really help to have when using Dual Mode BT.

Thanks!

caleedubya commented 4 years ago

Here's the code for anyone who might be able to help. From the reading I've done it looks like you need to bond to the BLE Device an then in your iOS app you make the following call:

WWDC Reference - https://developer.apple.com/videos/play/wwdc2019/901/ --> From reading the PDF found here about Dual Mode BT, it looks of it you should be able to bond via BLE and then magically exchange keys for BT Classic.

iOS Bluetooth call func connectToPeripheral(_ peripheral: CBPeripheral) { pendingPeripheral = peripheral centralManager.connect(peripheral, options: [CBConnectPeripheralOptionEnableTransportBridgingKey: true]) }

ESP Code

class MySecurity : public BLESecurityCallbacks {

bool onConfirmPIN(uint32_t pin){ return false; }

uint32_t onPassKeyRequest(){ ESP_LOGI(LOG_TAG, "PassKeyRequest"); return 123456; }

void onPassKeyNotify(uint32_t pass_key){ ESP_LOGI(LOG_TAG, "On passkey Notify number:%d", pass_key); }

bool onSecurityRequest(){ ESP_LOGI(LOG_TAG, "On Security Request"); return true; }

void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){ ESP_LOGI(LOG_TAG, "Starting BLE work!"); if(cmpl.success){ uint16_t length; esp_ble_gap_get_whitelist_size(&length); ESP_LOGD(LOG_TAG, "size: %d", length); } } };

class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; };

void onDisconnect(BLEServer* pServer) {
  deviceConnected = false;
}

};

class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string rxValue = pCharacteristic->getValue();

  if (rxValue.length() > 0 && newData == false) {

    for (int i = 0; i < rxValue.length(); i++) {
      inData[i] = rxValue[i];
      newData = true;
    }
  }
}

};

void setup() { BLEDevice::init("Data");

a2d_sink.start("Music");

// Create the BLE Server BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT); BLEDevice::setSecurityCallbacks(new MySecurity());

pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks());

// Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID);

// Create a BLE Characteristic pTxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY );

pTxCharacteristic->addDescriptor(new BLE2902());

BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE );

pRxCharacteristic->setCallbacks(new MyCallbacks());

// Start the service pService->start();

// Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->start();

BLESecurity *pSecurity = new BLESecurity(); pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND); pSecurity->setCapability(ESP_IO_CAP_OUT); pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); pSecurity->setRespEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising();