Есть ли у кого-нибудь полный рабочий пример того, как программно соединяться с устройством BLE (не Bluetooth Classic), которое использует запись ключа доступа (то есть 6-значный PIN-код) или числовое сравнение на Android 4.4 или новее? "Программно" я имею в виду, что я говорю Android PIN-код - пользователь не запрашивается.
Есть много подобных вопросов об этом на SO, но они либо a) о Bluetooth Classic, b) старые (до setPin()
и createBond()
были общедоступными) или c) без ответа.
Мое понимание таково:
- Вы подключаетесь к устройству и обнаруживаете его службы.
- Вы пытаетесь прочитать "защищенную" характеристику.
- Устройство возвращает ошибку проверки подлинности.
- Android как-то инициирует спаривание, и вы сообщаете ему PIN-код.
- Теперь вы можете прочитать характеристику.
Я создал устройство, использующее mBed, работающий на nRF51-DK и дал ему одну характеристику.
Я установил параметры безопасности следующим образом:
ble.securityManager().init(
true, // Enable bonding (though I don't really need this)
true, // Require MitM protection. I assume you don't get a PIN prompt without this, though I'm not 100% sure.
SecurityManager::IO_CAPS_DISPLAY_ONLY, // This makes it us the Passkey Entry (PIN) pairing method.
"123456"); // Static PIN
И затем в характеристике я использовал
requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM);
Теперь, когда я пытаюсь прочитать его с Nordic Master Control Panel, я получаю уведомление с запросом на соединение следующим образом:
И я могу вставить этот PIN-код, а затем MCP говорит, что я связан и могу прочитать характеристику.
Однако в моем приложении я бы хотел, чтобы пользователь не вводил ПИН-код, так как я его уже знаю. Кто-нибудь имеет полный недавний пример того, как это сделать?
Изменить: Кстати этот является наиболее актуальным вопросом, который я нашел на SO, но ответ там, похоже, не работает.