ble开发-添加开机蓝牙广播包

客户需求,蓝牙未连接状态电源按键按下后发送开机广播

实现步骤

1.新建蓝牙广播包,格式按照客户要求,创建位置与advdata[]相同

static uint8 adv_power_on[] =
{
 // flags
    0x02,   // length of this data
    GAP_ADTYPE_FLAGS,//0x01
    GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,//0x04
    
      // service UUIDs
    0x03,   // length of this data
        GAP_ADTYPE_16BIT_COMPLETE,
    LO_UINT16(HID_SERV_UUID),
    HI_UINT16(HID_SERV_UUID),

    // appearance
    0x03,   // length of this data
    GAP_ADTYPE_APPEARANCE,
    LO_UINT16(GAP_APPEARE_HID_KEYBOARD),
    HI_UINT16(GAP_APPEARE_HID_KEYBOARD),

        // local name
        0x0D,                               // length of this data
        GAP_ADTYPE_MANUFACTURER_SPECIFIC,     // AD Type = 0xFF
        0X5D,0X00,0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00 // name
};

 

2.创建函数发送蓝牙开机包,在hidkbd.c中加入以下代码并在.h中声明

void key_power_on_advert(void)
{

            GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( adv_power_on ), adv_power_on );
            GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanData ), scanData );
            // set adv type
            uint8_t param=GAP_ADTYPE_ADV_IND;
            GAPRole_SetParameter( GAPROLE_ADV_EVENT_TYPE, sizeof(uint8),&param );
            uint8 adv_param = TRUE;
           GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &adv_param );
        
        ble_LED_display_handle(POWER_ON_EVENT);
            
}

3.修改 ble_LED_display_handle()函数如下

static uint16 led_power_on_count = 0;
void ble_LED_display_handle( uint16 event_id )
{
    switch (event_id)
    {
        case ADVERTISING_DISPLAY_EVENT:
            if( hidDevGapState == GAPROLE_ADVERTISING )
            {
                if(led_flash_count == 0){
                    led_disp_cfg_t cfg;
                    cfg.retention = 1;
                    cfg.flash_loop = ADVERT_TIME/2;
                    cfg.on_period = 200;
                    cfg.off_period = 200;
                    led_disp_start(cfg);
                }

                if( (led_flash_count++) < ADVERT_TIME )
                {
                    osal_start_timerEx(hidKbdTaskId, ADVERTISING_DISPLAY_EVENT, 200);
                }
                else
                {
                    led_flash_count = 0;
                    osal_start_timerEx(hidKbdTaskId, DISABLE_BLE_ADVERT_EVENT, 200);
                }    
            }
            else
            {
                led_flash_count = 0;
                led_disp_stop();
                if( osal_get_timeoutEx( hidKbdTaskId, ADVERTISING_DISPLAY_EVENT) != 0 )
                {
                    osal_stop_timerEx( hidKbdTaskId, ADVERTISING_DISPLAY_EVENT );
                }                
            }    
            break;
            
        case FACTORY_RESET_DISPLAY_EVENT:            
            break;
            
        case IR_DISPLAY_EVENT:
            break;
        case POWER_ON_EVENT:
            LOG("POWER_ON_EVENT\r\n");
            if( hidDevGapState == GAPROLE_ADVERTISING )
            {
                if(led_power_on_count == 0){
                    led_disp_cfg_t cfg;
                    cfg.retention = 1;
                    cfg.flash_loop = ADV_POWER_ON_TIME/2;
                    cfg.on_period = 200;
                    cfg.off_period = 200;
                    led_disp_start(cfg);
                }
                led_power_on_count++;
                if( (led_power_on_count) < ADV_POWER_ON_TIME )
                {
                    LOG("led_power_on_count++\r\n");
                    osal_start_timerEx(hidKbdTaskId, POWER_ON_EVENT, 200);
                }
                else
                {
          led_power_on_count = 0;
                    osal_start_timerEx(hidKbdTaskId, DISABLE_BLE_ADVERT_EVENT, 200);
                }
            }
            else
            {
                //led_power_on_count = 0;
                led_disp_stop();
                if( osal_get_timeoutEx( hidKbdTaskId, POWER_ON_EVENT) != 0 )
                {
                    osal_stop_timerEx( hidKbdTaskId, POWER_ON_EVENT );
                }    
        }    
            break;
            
        default:
            break;
    }
    
}

4.HidKbd_ProcessEvent中添加POWER_ON_EVENT

    if(events & POWER_ON_EVENT)
    {
        ble_LED_display_handle(POWER_ON_EVENT);
        return ( events ^ POWER_ON_EVENT );
    }

5.在按键中断中添加 key_power_on_advert 添加可自己定义,我定义的是在按键松开时发送开机广播包

    case BSP_BTN_UP_TYPE:
            KEY_LOG("[key]:%02d rel\n", key_index);
            start_enable_latency_event();
            key_press_flag = FALSE;
            if( key_index <= 15 )
            {
                if( get_hidDevConnSecure_value() == TRUE )
                {
                    if( get_ir_sending_mode() == true )
                    {
                        ir_sending_stop_handle(0);
                    }
                    if( key_index== KEY_INDEX_AUDIO )
                    {
                        VOICE_Stop();
                    }
                    else
                    {
                        bt_key_code_sending_over_handle( key_index );
                    }
                    Send_BTkey_flag = FALSE;
                }
                else
                {
                        if(key_index==KEY_INDEX_POWER)
                        {
                            LOG("send power on adv");
                            key_power_on_advert();
                        }
                        if(Send_BTkey_flag)
                        {
                            Send_BTkey_flag = FALSE;
                            led_disp_on_off_ctrl(LED_OFF);
                            if( key_index == KEY_INDEX_AUDIO )
                            {
                                LOG("disconnect_voice_stop");
                                disconnect_voice_stop();
                            }                    
                        }
                        if( get_ir_sending_mode() == true )
                        {
                            ir_sending_stop_handle(0);
                        }
                        
                    }
            }

            if(battMeasure()<=20)
            {
                led_low_power_send();
            }
            break;

 

posted on 2023-06-30 17:43  foorun  阅读(436)  评论(0编辑  收藏  举报