HFP HF - Hands-Free
Source Code: hfp_hs_demo.c
This HFP Hands-Free example demonstrates how to receive an output from a remote HFP audio gateway (AG), and, if HAVE_BTSTACK_STDIN is defined, how to control the HFP AG.
Main Application Setup
Listing here shows main application code. To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it. The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers. The stdin_process callback allows for sending commands to the HFP AG. 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;
// Init protocols
// init L2CAP
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();
<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/sdp_server.h#L72">sdp_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
// Init profiles
uint16_t hf_supported_features =
(1<<HFP_HFSF_ESCO_S4) |
(1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) |
(1<<HFP_HFSF_HF_INDICATORS) |
(1<<HFP_HFSF_CODEC_NEGOTIATION) |
(1<<HFP_HFSF_ENHANCED_CALL_STATUS) |
(1<<HFP_HFSF_VOICE_RECOGNITION_FUNCTION) |
(1<<HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
(1<<HFP_HFSF_VOICE_RECOGNITION_TEXT) |
(1<<HFP_HFSF_EC_NR_FUNCTION) |
(1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L92">hfp_hf_init</a>(rfcomm_channel_nr);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L92">hfp_hf_init</a>_supported_features(hf_supported_features);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L92">hfp_hf_init</a>_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L92">hfp_hf_init</a>_codecs(sizeof(codecs), codecs);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L119">hfp_hf_register_packet_handler</a>(hfp_hf_packet_handler);
// Set Apple Accessory Information: features = battery reporting & docked/powered, battery=9, docked=1
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L594">hfp_hf_apple_set_identification</a>(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 0x0001, "0001", 3);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L601">hfp_hf_apple_set_battery_level</a>(9);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L608">hfp_hf_apple_set_docked_state</a>(1);
// Configure SDP
// - Create and register HFP HF service record
memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L81"><a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_hf.h#L643">hfp_hf_create_sdp_record</a>_with_codecs</a>(hfp_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, hfp_hf_service_name, hf_supported_features, sizeof(codecs), codecs);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( hfp_service_buffer) <= sizeof(hfp_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(hfp_service_buffer);
// Configure GAP - discovery / connection
// - Set local name with a template Bluetooth address, that will be automatically
// replaced with an actual address once it is available, i.e. when BTstack boots
// up and starts talking to a Bluetooth module.
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("HFP HF Demo 00:00:00:00:00:00");
// - Allow to show up in Bluetooth inquiry
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
// - Set Class of Device - Service Class: Audio, Major Device Class: Audio, Minor: Hands-Free device
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x200408);
// - 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 allows HFP AG, e.g. smartphone, 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);
// Register for HCI events and SCO packets
hci_event_callback_registration.callback = &hci_packet_handler;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&hci_event_callback_registration);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1596">hci_register_sco_packet_handler</a>(&hci_packet_handler);
// Init SCO / HFP audio processing
sco_demo_init();
#ifdef HAVE_BTSTACK_STDIN
// 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);
btstack_stdin_setup(stdin_process);
#endif
// turn on!
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);
return 0;
}