Skip to content

Commit bde21e5

Browse files
Add BLE support to BluetoothHIDMaster (#2208)
Support Bluetooth BLE keyboard and mice using the same HID master infrastructure as the BT Classic.
1 parent c104c67 commit bde21e5

File tree

10 files changed

+740
-22
lines changed

10 files changed

+740
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
8787
# Features
8888
* Adafruit TinyUSB Arduino (USB mouse, keyboard, flash drive, generic HID, CDC Serial, MIDI, WebUSB, others)
8989
* Bluetooth on the PicoW (Classic and BLE) with Keyboard, Mouse, Joystick, and Virtual Serial
90-
* Bluetooth Classic HID master mode (connect to BT keyboard or mouse)
90+
* Bluetooth Classic and BLE HID master mode (connect to BT keyboard or mouse)
9191
* Generic Arduino USB Serial, Keyboard, Joystick, and Mouse emulation
9292
* WiFi (Pico W, ESP32-based ESPHost, Atmel WINC1500)
9393
* Ethernet (Wired W5500, W5100, ENC28J60)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <BluetoothHCI.h>
2+
3+
BluetoothHCI hci;
4+
5+
void BTBasicSetup() {
6+
l2cap_init();
7+
gatt_client_init();
8+
sm_init();
9+
sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
10+
gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
11+
hci_set_master_slave_policy(HCI_ROLE_MASTER);
12+
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
13+
14+
hci.setBLEName("Pico BLE Scanner");
15+
hci.install();
16+
hci.begin();
17+
}
18+
19+
void setup() {
20+
delay(5000);
21+
BTBasicSetup();
22+
}
23+
24+
void loop() {
25+
Serial.printf("BEGIN BLE SCAN @%lu ...", millis());
26+
auto l = hci.scanBLE(BluetoothHCI::any_cod);
27+
Serial.printf("END BLE SCAN @%lu\n\n", millis());
28+
Serial.printf("%-8s | %-17s | %-4s | %s\n", "Class", "Address", "RSSI", "Name");
29+
Serial.printf("%-8s | %-17s | %-4s | %s\n", "--------", "-----------------", "----", "----------------");
30+
for (auto e : l) {
31+
Serial.printf("%08lx | %17s | %4d | %s\n", e.deviceClass(), e.addressString(), e.rssi(), e.name());
32+
}
33+
Serial.printf("\n\n\n");
34+
}

libraries/BluetoothHCI/keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ scan KEYWORD2
2020
scanAsyncDone KEYWORD2
2121
scanAsyncResult KEYWORD2
2222

23+
setName KEYWORD2
24+
scanBLE KEYWORD2
25+
2326
# BTDeviceInfo
2427
deviceClass KEYWORD2
2528
address KEYWORD2
29+
addressType KEYWORD2
2630
addressString KEYWORD2
2731
rssi KEYWORD2
2832
name KEYWORD2

libraries/BluetoothHCI/src/BluetoothDevice.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,26 @@ class BTDeviceInfo {
2727
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int rssi, const char *name) {
2828
_deviceClass = dc;
2929
memcpy(_address, addr, sizeof(_address));
30+
_addressType = -1;
3031
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
3132
_rssi = rssi;
3233
_name = strdup(name);
3334
}
35+
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int addressType, int rssi, const char *name, size_t nameLen) {
36+
_deviceClass = dc;
37+
memcpy(_address, addr, sizeof(_address));
38+
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
39+
_addressType = addressType;
40+
_rssi = rssi;
41+
_name = (char *)malloc(nameLen + 1);
42+
memcpy(_name, name, nameLen);
43+
_name[nameLen] = 0;
44+
}
3445
// Copy constructor to ensure we deep-copy the string
3546
BTDeviceInfo(const BTDeviceInfo &b) {
3647
_deviceClass = b._deviceClass;
3748
memcpy(_address, b._address, sizeof(_address));
49+
_addressType = b._addressType;
3850
memcpy(_addressString, b._addressString, sizeof(_addressString));
3951
_rssi = b._rssi;
4052
_name = strdup(b._name);
@@ -57,9 +69,13 @@ class BTDeviceInfo {
5769
const char *name() {
5870
return _name;
5971
}
72+
int addressType() {
73+
return _addressType;
74+
}
6075
private:
6176
uint32_t _deviceClass;
6277
uint8_t _address[6];
78+
int _addressType;
6379
char _addressString[18];
6480
int8_t _rssi;
6581
char *_name;

0 commit comments

Comments
 (0)