蓝牙学习-HSP

HSP - Headset Profile

HSP提供蓝牙耳机和蓝牙设备的通讯机制。HSP依赖SCO(Synchronous Connection Oriented)实现同步话音传送。前面章节讲的如SPP依赖ACL(Asynchronous Connection less)实现分组数据传送。HSP的音频解码为 64kbit/s CVSD 并且支持 GSM07.07 AT命令的子集控制命令,如响铃,应答,挂机和调整音量大小。

HSP的服务能力

  • Audio Gateway (AG):蓝牙主机提供音源供蓝牙外设连接;
  • Headset (HS):蓝牙外设连接主机并提供对主机的控制;

限制:

1. 在HS和AG之间只能建立一个音频通道

2. HSP只提供基础的交互能力,如处理多方通话是不支持的

3. HS只支持用户的按键事件

HS解析

  1. 创建流程

    sco_demo_init();
    sco_demo_set_codec(HFP_CODEC_CVSD);
    l2cap_init();
    sm_init();
    sdp_init();
    hsp_hs_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_hs_service_name, 0);
    sdp_register_service(hsp_service_buffer);
    rfcomm_init();
    hsp_hs_init(rfcomm_channel_nr);
    hci_add_event_handler(&hci_event_callback_registration);
    hci_register_sco_packet_handler(&packet_handler)
    hsp_hs_register_packet_handler(packet_handler);
    
  1. 数据处理流程

    graph LR
    packet_handler --> packet_type:HCI_SCO_DATA_PACKET --> sco_demo_receive
    packet_handler --> packet_type:HCI_EVENT_PACKET --> BTSTACK_EVENT_STATE
    packet_type:HCI_EVENT_PACKET --> HCI_EVENT_SCO_CAN_SEND_NOW
    packet_type:HCI_EVENT_PACKET --> HCI_EVENT_HSP_META
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_SPEAKER_GAIN_CHANGED
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_RING
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_AG_INDICATION
    
    

    主要流程解释:

    耳机开机后可以选择命令:设备连接,音频连接,按键控制,调整声音输入(mircrophone,麦克风)和输出(speaker,喇叭)
    连接建立完毕后收到HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE,音频建立完毕后收到HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE
    如果有拨号事件来会收到HSP_SUBEVENT_RING,如果是提醒会收到HSP_SUBEVENT_AG_INDICATION
    

AG解析

  1. 创建流程

    sco_demo_init()
    sco_demo_set_codec(HFP_CODEC_CVSD)
    l2cap_init()
    sm_init()
    sdp_init()
    hsp_ag_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_ag_service_name)
    sdp_register_service(hsp_service_buffer)
    rfcomm_init();
    hsp_ag_init(rfcomm_channel_nr);
    hsp_ag_register_packet_handler(&packet_handler);
    hci_register_sco_packet_handler(&packet_handler);
    sscanf_bd_addr(device_addr_string, device_addr);
    gap_set_local_name(device_name);
    gap_discoverable_control(1)
    gap_ssp_set_io_capabilitygap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
    gap_set_class_of_device(0x400204);
    hci_power_control(HCI_POWER_ON);
    
  1. 数据处理流程

    graph LR
    packet_handler --> packet_type:HCI_SCO_DATA_PACKET --> sco_demo_receive
    packet_handler --> packet_type:HCI_EVENT_PACKET --> BTSTACK_EVENT_STATE --> hsp_ag_connect
    packet_type:HCI_EVENT_PACKET --> HCI_EVENT_SCO_CAN_SEND_NOW --> sco_demo_send
    packet_type:HCI_EVENT_PACKET --> HCI_EVENT_HSP_META
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE --> hsp_ag_establish_audio_connection
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE --> hci_request_sco_can_send_now_event
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_SPEAKER_GAIN_CHANGED
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_HS_COMMAND
    HCI_EVENT_HSP_META --> HSP_SUBEVENT_BUTTON_PRESSED
    

    主要流程解释:

    启动HCI后,建立ag服务;
    等待hs进行连接,收到HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE表明建立rfcomm连接完毕,然后建立音频连接
    等待音频建立完毕,收到HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE,然后获取sco_handle后进行hci_request_sco_can_send_now_event发送请求,处理HCI_EVENT_SCO_CAN_SEND_NOW以便发送数据;
    同时可以接收控制命令:HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED,HSP_SUBEVENT_SPEAKER_GAIN_CHANGED,HSP_SUBEVENT_HS_COMMAND,HSP_SUBEVENT_BUTTON_PRESSED(默认动作就是断开和建立连接)
    

你可能感兴趣的:(蓝牙学习-HSP)