Skip to content

Commit

Permalink
Add BLE support to BluetoothHIDMaster (#2208)
Browse files Browse the repository at this point in the history
Support Bluetooth BLE keyboard and mice using the same HID master
infrastructure as the BT Classic.
  • Loading branch information
earlephilhower authored Jun 7, 2024
1 parent c104c67 commit bde21e5
Show file tree
Hide file tree
Showing 10 changed files with 740 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
# Features
* Adafruit TinyUSB Arduino (USB mouse, keyboard, flash drive, generic HID, CDC Serial, MIDI, WebUSB, others)
* Bluetooth on the PicoW (Classic and BLE) with Keyboard, Mouse, Joystick, and Virtual Serial
* Bluetooth Classic HID master mode (connect to BT keyboard or mouse)
* Bluetooth Classic and BLE HID master mode (connect to BT keyboard or mouse)
* Generic Arduino USB Serial, Keyboard, Joystick, and Mouse emulation
* WiFi (Pico W, ESP32-based ESPHost, Atmel WINC1500)
* Ethernet (Wired W5500, W5100, ENC28J60)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <BluetoothHCI.h>

BluetoothHCI hci;

void BTBasicSetup() {
l2cap_init();
gatt_client_init();
sm_init();
sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
hci_set_master_slave_policy(HCI_ROLE_MASTER);
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);

hci.setBLEName("Pico BLE Scanner");
hci.install();
hci.begin();
}

void setup() {
delay(5000);
BTBasicSetup();
}

void loop() {
Serial.printf("BEGIN BLE SCAN @%lu ...", millis());
auto l = hci.scanBLE(BluetoothHCI::any_cod);
Serial.printf("END BLE SCAN @%lu\n\n", millis());
Serial.printf("%-8s | %-17s | %-4s | %s\n", "Class", "Address", "RSSI", "Name");
Serial.printf("%-8s | %-17s | %-4s | %s\n", "--------", "-----------------", "----", "----------------");
for (auto e : l) {
Serial.printf("%08lx | %17s | %4d | %s\n", e.deviceClass(), e.addressString(), e.rssi(), e.name());
}
Serial.printf("\n\n\n");
}
4 changes: 4 additions & 0 deletions libraries/BluetoothHCI/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ scan KEYWORD2
scanAsyncDone KEYWORD2
scanAsyncResult KEYWORD2

setName KEYWORD2
scanBLE KEYWORD2

# BTDeviceInfo
deviceClass KEYWORD2
address KEYWORD2
addressType KEYWORD2
addressString KEYWORD2
rssi KEYWORD2
name KEYWORD2
Expand Down
16 changes: 16 additions & 0 deletions libraries/BluetoothHCI/src/BluetoothDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,26 @@ class BTDeviceInfo {
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int rssi, const char *name) {
_deviceClass = dc;
memcpy(_address, addr, sizeof(_address));
_addressType = -1;
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
_rssi = rssi;
_name = strdup(name);
}
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int addressType, int rssi, const char *name, size_t nameLen) {
_deviceClass = dc;
memcpy(_address, addr, sizeof(_address));
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
_addressType = addressType;
_rssi = rssi;
_name = (char *)malloc(nameLen + 1);
memcpy(_name, name, nameLen);
_name[nameLen] = 0;
}
// Copy constructor to ensure we deep-copy the string
BTDeviceInfo(const BTDeviceInfo &b) {
_deviceClass = b._deviceClass;
memcpy(_address, b._address, sizeof(_address));
_addressType = b._addressType;
memcpy(_addressString, b._addressString, sizeof(_addressString));
_rssi = b._rssi;
_name = strdup(b._name);
Expand All @@ -57,9 +69,13 @@ class BTDeviceInfo {
const char *name() {
return _name;
}
int addressType() {
return _addressType;
}
private:
uint32_t _deviceClass;
uint8_t _address[6];
int _addressType;
char _addressString[18];
int8_t _rssi;
char *_name;
Expand Down
Loading

0 comments on commit bde21e5

Please sign in to comment.