網頁

2019年1月25日 星期五

DIY - ESP32:ESP32 藍牙(Bluetooth)GAP API 函數(八十)

DIY - ESP32:ESP32 藍牙(Bluetooth)GAP API 函數(八十):

在 ESP32 藍牙(Bluetooth)的內置GAP(Generic Access Profile)的 API 函數,GAP 保證在不同的藍牙產品可以互相發現對方並建立連接,並且定義了藍牙設備如何發現和建立與其他設備的安全/不安全連接,它處理一些一般模式的業務(如詢問、命名和搜索)和一些安全性問題(如擔保),同時還處理一些有關連接的業務(如鏈路建立、通道和連接建立),GAP 規定的是一些一般性的運行任務;因此,它具有強制性,並作為所有其它藍牙應用規範的基礎。

藍牙 GAP(Generic Access Profile)
功能 - esp_bt_gap_get_cod_srvc
原型:uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod)
參數:cod: Class of Device
返回:major service bits
說明:get major service field of COD
範例:uint32_t cod = 0;
            esp_bt_gap_get_cod_srvc(cod);

功能 - esp_bt_gap_get_cod_major_dev
原型:uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod)
參數:cod: Class of Device
返回:major device bits
說明:get major device field of COD
範例:

功能 - esp_bt_gap_get_cod_minor_dev
原型:uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod)
參數:cod: Class of Device
返回:minor service bits
說明:get minor service field of COD
範例:

功能 - esp_bt_gap_get_cod_format_type
原型:uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod)
參數:cod: Class of Device
返回:format type
說明:get format type of COD
範例:

功能 - esp_bt_gap_is_valid_cod
原型:bool esp_bt_gap_is_valid_cod(uint32_t cod)
參數:cod: Class of Device
返回:true if cod is valid
            false otherise
說明:decide the integrity of COD
範例:uint32_t cod = 0;
            esp_bt_gap_is_valid_cod(cod)

功能 - esp_bt_gap_register_callback
原型:esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback)
參數:
返回:ESP_OK : Succeed
            ESP_FAIL: others
說明:register callback function. This function should be called after esp_bluedroid_enable() completes successfully
範例:esp_bt_gap_register_callback(bt_app_gap_cb);

功能 - esp_bt_gap_set_scan_mode
原型:esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode)
參數:mode: : one of the enums of bt_scan_mode_t
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_ARG: if argument invalid
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_FAIL: others
說明:Set discoverability and connectability mode for legacy bluetooth. This function should be called after esp_bluedroid_enable() completes successfully.
範例:esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
            esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_NONE);

功能 - esp_bt_gap_start_discovery
原型:esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, uint8_t num_rsps)
參數:mode: - inquiry mode
            inq_len: - inquiry duration in 1.28 sec units, ranging from 0x01 to 0x30
            num_rsps: - number of inquiry responses that can be received, value 0 indicates an unlimited number of responses
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_ERR_INVALID_ARG: if invalid parameters are provided
            ESP_FAIL: others
說明:Start device discovery. This function should be called after esp_bluedroid_enable() completes successfully. esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is started or halted. esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_RES_EVT if discovery result is got.
範例:esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);

功能 - esp_bt_gap_cancel_discovery
原型:esp_err_t esp_bt_gap_cancel_discovery(void)
參數:
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_FAIL: others
說明:Cancel device discovery. This function should be called after esp_bluedroid_enable() completes successfully esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is stopped.
範例:esp_bt_gap_cancel_discovery();

功能 - esp_bt_gap_get_remote_services
原型:esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda)
參數:
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_FAIL: others
說明:Start SDP to get remote services. This function should be called after esp_bluedroid_enable() completes successfully. esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVCS_EVT after service discovery ends.
範例:

功能 - esp_bt_gap_get_remote_service_record
原型:esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_uuid_t *uuid)
參數:
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_FAIL: others
說明:Start SDP to look up the service matching uuid on the remote device. This function should be called after esp_bluedroid_enable() completes successfully.
esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVC_REC_EVT after service discovery ends
範例:

功能 - *esp_bt_gap_resolve_eir_data
原型:uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8_t *length)
參數:eir: - pointer of raw eir data to be resolved
            type: - specific EIR data type
            length: - return the length of EIR data excluding fields of length and data type
返回:pointer of starting position of eir data excluding eir data type, NULL if not found
說明:This function is called to get EIR data for a specific type.
範例:uint8_t *rmt_bdname = NULL;
            uint8_t rmt_bdname_len = 0;
            rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);

功能 - esp_bt_gap_set_cod
原型:esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode)
參數:cod: - class of device
            mode: - setting mode
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_ERR_INVALID_ARG: if param is invalid
            ESP_FAIL: others
說明:This function is called to set class of device. esp_bt_gap_cb_t will is called with ESP_BT_GAP_SET_COD_EVT after set COD ends Some profile have special restrictions on class of device, changes may cause these profile do not work.
範例:

功能 - esp_bt_gap_get_cod
原型:esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod)
參數:cod: - class of device
返回:ESP_OK : Succeed
            ESP_FAIL: others
說明:This function is called to get class of device.
範例:

功能 - esp_bt_gap_read_rssi_delta
原型:esp_err_t esp_bt_gap_read_rssi_delta(esp_bd_addr_t remote_addr)
參數:remote_addr: - remote device address, corresponding to a certain connection handle.
返回:ESP_OK : Succeed
            ESP_FAIL: others
說明:This function is called to read RSSI delta by address after connected. The RSSI value returned by ESP_BT_GAP_READ_RSSI_DELTA_EVT.
範例:

功能 - esp_bt_gap_remove_bond_device
原型:esp_err_t esp_bt_gap_remove_bond_device(esp_bd_addr_t bd_addr)
參數:bd_addr: : BD address of the peer device
返回:ESP_OK : success
   ESP_FAIL : failed
說明:Removes a device from the security database list of peer device.
範例:

功能 - pcnt_isr_service_uninstall
原型:int esp_bt_gap_get_bond_device_num(void)
參數:
返回:- >= 0 : bonded devices number.
            ESP_FAIL : failed
說明:Get the device number from the security database list of peer device. It will return the device bonded number immediately.
範例:

功能 - esp_bt_gap_get_bond_device_list
原型:esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list)
參數:dev_num: Indicate the dev_list array(buffer) size as input. If dev_num is large enough, it means the actual number as output. Suggest that dev_num value equal to esp_ble_get_bond_device_num().
            dev_list: an array(buffer) of esp_bd_addr_t type. Use for storing the bonded devices address. The dev_list should be allocated by who call this API.
返回:ESP_OK : Succeed
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            ESP_FAIL: others
說明:Get the device from the security database list of peer device. It will return the device bonded information immediately.
範例:

功能 - esp_bt_gap_set_pin
原型:esp_err_t esp_bt_gap_set_pin(esp_bt_pin_type_t pin_type, uint8_t pin_code_len, esp_bt_pin_code_t pin_code)
參數:pin_type: Use variable or fixed pin. If pin_type is ESP_BT_PIN_TYPE_VARIABLE, pin_code and pin_code_len will be ignored, and ESP_BT_GAP_PIN_REQ_EVT will come when control requests for pin code. Else, will use fixed pin code and not callback to users.
            pin_code_len: Length of pin_code
            pin_code: Pin_code
返回:ESP_OK : success
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
   other : failed
說明:Set pin type and default pin code for legacy pairing.
範例:esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
            esp_bt_pin_code_t pin_code;
            esp_bt_gap_set_pin(pin_type, 0, pin_code);

功能 - esp_bt_gap_pin_reply
原型:esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_code_len, esp_bt_pin_code_t pin_code)
參數:bd_addr: BD address of the peer
            accept: Pin_code reply successful or declined.
            pin_code_len: Length of pin_code
            pin_code: Pin_code
返回:ESP_OK : success
  ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            other : failed
說明:Reply the pin_code to the peer device for legacy pairing when ESP_BT_GAP_PIN_REQ_EVT is coming.
範例:esp_bt_pin_code_t pin_code;
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);

功能 - esp_bt_gap_set_security_param
原型:esp_err_t esp_bt_gap_set_security_param(esp_bt_sp_param_t param_type, void *value, uint8_t len)
參數:param_type: : the type of the param which is to be set
            value: : the param value
            len: : the length of the param value
返回:ESP_OK : success
  ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            other : failed
說明:Set a GAP security parameter value. Overrides the default value.
範例:esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));

功能 - esp_bt_gap_ssp_passkey_reply
原型:esp_err_t esp_bt_gap_ssp_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint32_t passkey)
參數:bd_addr: : BD address of the peer
            accept: : passkey entry successful or declined.
            passkey: : passkey value, must be a 6 digit number, can be lead by 0.
返回:ESP_OK : success
            ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            other : failed
說明:Reply the key value to the peer device in the legacy connection stage.
範例:

功能 - esp_bt_gap_ssp_confirm_reply
原型:esp_err_t esp_bt_gap_ssp_confirm_reply(esp_bd_addr_t bd_addr, bool accept)
參數:bd_addr: : BD address of the peer device
            accept: : numbers to compare are the same or different.
返回:ESP_OK : success
    ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
            other : failed
說明:Reply the confirm value to the peer device in the legacy connection stage.   
範例:esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);

Header File:bt/bluedroid/api/include/api/esp_gap_bt_api.h
參考網址:https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/bluetooth/esp_gap_bt.html

2019年 1月 25日 天氣報告
氣溫:16.9@ 20:40
相對濕度:百分之 74%
天氣:天色良好

沒有留言:

張貼留言