HSP HS - Headset

Source Code: hsp_hs_demo.c

This example implements a HSP Headset device that sends and receives audio signal over HCI SCO. It demonstrates how to receive an output from a remote audio gateway (AG), and, if HAVE_BTSTACK_STDIN is defined, how to control the AG.

Audio Transfer Setup

A pre-computed sine wave (160Hz) is used as the input audio signal. 160 Hz. To send and receive an audio signal, ENABLE_SCO_OVER_HCI has to be defined.

Tested working setups:

Main Application Setup

Listing here shows main application code. To run a HSP Headset service you need to initialize the SDP, and to create and register HSP HS record with it. In this example, the SCO over HCI is used to receive and send an audio signal.

Two packet handlers are registered:

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

  sco_demo_init();
  sco_demo_set_codec(HFP_CODEC_CVSD);

  <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

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  memset(hsp_service_buffer, 0, sizeof(hsp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hsp_hs.h#L69">hsp_hs_create_sdp_record</a>(hsp_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), rfcomm_channel_nr, hsp_hs_service_name, 0);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( hsp_service_buffer) &lt;= sizeof(hsp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(hsp_service_buffer);

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/rfcomm.h#L278">rfcomm_init</a>();

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hsp_hs.h#L60">hsp_hs_init</a>(rfcomm_channel_nr);

  // register for HCI events and SCO packets
  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);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1596">hci_register_sco_packet_handler</a>(&amp;packet_handler);

  // register for HSP events
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hsp_hs.h#L86">hsp_hs_register_packet_handler</a>(packet_handler);

#ifdef HAVE_BTSTACK_STDIN
  btstack_stdin_setup(stdin_process);
#endif

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("HSP HS Demo 00:00:00:00:00:00");
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L549">gap_ssp_set_io_capability</a>(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x240404);

  // Parse human readable Bluetooth address.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L287">sscanf_bd_addr</a>(device_addr_string, device_addr);

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