HID Keyboard Classic

Source Code: hid_keyboard_demo.c

This HID Device example demonstrates how to implement an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent If HAVE_BTSTACK_STDIN is defined, you can type from the terminal

Main Application Setup

Listing here shows main application code. To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. At the end the Bluetooth stack is started.

int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
  (void)argc;
  (void)argv;

  // allow to get found by inquiry
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
  // use Limited Discoverable Mode; Peripheral; Keyboard as CoD
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x2540);
  // set local name to be identified - zeroes will be replaced by actual BD ADDR
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("HID Keyboard Demo 00:00:00:00:00:00");
  // allow for role switch in general and sniff mode
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L439">gap_set_default_link_policy_settings</a>( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
  // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L445">gap_set_allow_role_switch</a>(true);

  // L2CAP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();

#ifdef ENABLE_BLE
  // Initialize LE Security Manager. Needed for cross-transport key derivation
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif

  // SDP Server
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  memset(hid_service_buffer, 0, sizeof(hid_service_buffer));

  uint8_t hid_virtual_cable = 0;
  uint8_t hid_remote_wake = 1;
  uint8_t hid_reconnect_initiate = 1;
  uint8_t hid_normally_connectable = 1;

  hid_sdp_record_t hid_params = {
    // hid sevice subclass 2540 Keyboard, hid counntry code 33 US
    0x2540, 33, 
    hid_virtual_cable, hid_remote_wake, 
    hid_reconnect_initiate, hid_normally_connectable,
    hid_boot_device,
    host_max_latency, host_min_timeout,
    3200,
    hid_descriptor_keyboard,
    sizeof(hid_descriptor_keyboard),
    hid_device_name
  };

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L84">hid_create_sdp_record</a>(hid_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), &amp;hid_params);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( hid_service_buffer) &lt;= sizeof(hid_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(hid_service_buffer);

  // See https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers if you don't have a USB Vendor ID and need a Bluetooth Vendor ID
  // device info: BlueKitchen GmbH, product 1, version 1
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/device_id_server.h#L65">device_id_create_sdp_record</a>(device_id_sdp_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( device_id_sdp_service_buffer) &lt;= sizeof(device_id_sdp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(device_id_sdp_service_buffer);

  // HID Device
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L92">hid_device_init</a>(hid_boot_device, sizeof(hid_descriptor_keyboard), hid_descriptor_keyboard);

  // register for HCI events
  hci_event_callback_registration.callback = &amp;packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&amp;hci_event_callback_registration);

  // register for HID events
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L98">hid_device_register_packet_handler</a>(&amp;packet_handler);

#ifdef HAVE_BTSTACK_STDIN
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L287">sscanf_bd_addr</a>(device_addr_string, device_addr);
  btstack_stdin_setup(stdin_process);
#endif

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_ring_buffer.h#L68">btstack_ring_buffer_init</a>(&amp;send_buffer, send_buffer_storage, sizeof(send_buffer_storage));

  // turn on!
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);
  return 0;
}