Skip to main content

Rockchip Blutooth DeviceIo Introduction

:::info 文档信息

文档标识:RK-SM-YF-343 · 发布版本:V2.0.1 · 发布日期:2020-07-12 · 页数:33 · 语言:英文

原始文档:Rockchip_Developer_Guide_DeviceIo_Bluetooth_EN.pdf

:::

章节大纲​

  • Rockchip Blutooth DeviceIo Introduction(P1)
    • Bluetooth Basic Interface (RkBtBase.h)(P8)
    • BLE Interface Introduction (RkBle.h)(P12)
    • BLE CLIENT Interface Introduction (RkBtSpp.h)(P13)
    • SPP Interface Introduction (RkBtSpp.h)(P15)
    • A2DP SINK Interface Introduction (RkBtSink.h)(P16)
    • A2DP SOURCE Interface Introduction (RkBtSource.h)(P19)
    • HFP-HF Interface Introduction (RkBtHfp.h)(P21)
    • OBEX Interface Introduction (RkBtObex.h BLUEZ only)(P23)
    • Demo Program Introduction(P24)
      • Build(P24)
      • Basic Interface Demo Program(P24)
        • Interface Introduction(P25)
          • Basic Interface Test Introduction to Bluetooth Service(P25)
          • BLE Interface Testing Introduction(P26)
          • BLE CLIENT Interface Test Introduction(P26)
          • A2DP SINK Interface Test Introduction(P27)
          • A2DP SOURCE Interface Test Introduction(P28)
          • SPP Interface Testing Introduction(P29)
          • HFP Interface Test Introduction(P29)
          • OBEX Interface Test Introduction(P30)
        • Test Steps(P31)
      • BLE Network Configuration Demo Program(P33)

在线预览​

若预览无法显示,请直接下载 PDF 后用本地阅读器打开。


1. Bluetooth Basic Interface (RkBtBase.h)​

RkBtContent structure

1 typedef struct { 2 Ble Uuid Type t server uuid; //BLE server uuid _ _ _ _ 3 Ble Uuid Type t chr uuid[12]; //BLE CHR uuid, 12 at most _ _ _ _ 4 uint8 t chr cnt; //the number of CHR _ _ 5 const char *ble name; //the name of BLE, which may be different from the _ name of bt name _ 6 uint8 t ble addr[DEVICE ADDR LEN]; //BLE address, random address is _ _ _ _ used by default(BLUEZ Only) 7 uint8 t advData[256]; //Broadcast data _ 8 uint8 t advDataLen; //the length of broadcast data _ 9 uint8 t respData[256]; //Broadcast response data _ 10 uint8 t respDataLen; //the length of broadcast response data _ 11 /* Ways to generate broadcast data with the value of BLE ADVDATA TYPE USER/BLE ADVDATA TYPE SYSTEM _ _ _ _ _ _ 12 * BLE ADVDATA TYPE USER: use data from advData and respData as BLE _ _ _ broadcast 13 * BLE ADVDATA TYPE SYSTEM: system's broadcast data by default. _ _ _ 14 * Broadcast packages: flag(0x1a),128bit Server UUID; 15 * Broadcast response packages: bluetooth's name 16 */ 17 uint8 t advDataType; _ 18 //AdvDataKgContent adv kg; _ 19 char le random addr[6]; //random address, generated by system by _ _ default, users do not need to fill in. 20 /* BLE data receiving callback function, uuid represents the current CHR UUID, data: data pointer, len: data's length */ 21 void (*cb ble recv fun)(const char *uuid, unsigned char *data, int len); _ _ _ 22 /* BLE data request callback function. When this function is used on the receiver side, it will trigger the function to fill data */ 23 void (*cb ble request data)(const char *uuid); _ _ _ 24 } RkBleContent;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24typedef struct { Ble Uuid Type t server uuid; //BLE server uuid _ _ _ _ Ble Uuid Type t chr uuid[12]; //BLE CHR uuid, 12 at most _ _ _ _ uint8 t chr cnt; //the number of CHR _ _ const char *ble name; //the name of BLE, which may be different from the _ name of bt name _ uint8 t ble addr[DEVICE ADDR LEN]; //BLE address, random address is _ _ _ _ used by default(BLUEZ Only) uint8 t advData[256]; //Broadcast data _ uint8 t advDataLen; //the length of broadcast data _ uint8 t respData[256]; //Broadcast response data _ uint8 t respDataLen; //the length of broadcast response data _ /* Ways to generate broadcast data with the value of BLE ADVDATA TYPE USER/BLE ADVDATA TYPE SYSTEM _ _ _ _ _ _ * BLE ADVDATA TYPE USER: use data from advData and respData as BLE _ _ _ broadcast * BLE ADVDATA TYPE SYSTEM: system's broadcast data by default. _ _ _ * Broadcast packages: flag(0x1a),128bit Server UUID; * Broadcast response packages: bluetooth's name */ uint8 t advDataType; _ //AdvDataKgContent adv kg; _ char le random addr[6]; //random address, generated by system by _ _ default, users do not need to fill in. /* BLE data receiving callback function, uuid represents the current CHR UUID, data: data pointer, len: data's length */ void (*cb ble recv fun)(const char *uuid, unsigned char *data, int len); _ _ _ /* BLE data request callback function. When this function is used on the receiver side, it will trigger the function to fill data */ void (*cb ble request data)(const char *uuid); _ _ _ } RkBleContent;
R
1 2 3 4
5
1 typedef struct scaned dev { _ 2 char *remote address; //remote device address _ 3 char *remote name; //remote device name _ 4 unsigned int cod; //class of device 5 bool is connected; //whether the remote device is connected _ currently(sink, source, hfp) 6 truct paired dev *next; //point to next device _ 7 } RkBtScanedDevice;
1 2 3 4 5 6 7typedef struct scaned dev { _ char *remote address; //remote device address _ char *remote name; //remote device name _ unsigned int cod; //class of device bool is connected; //whether the remote device is connected _ currently(sink, source, hfp) truct paired dev *next; //point to next device _ } RkBtScanedDevice;
RK BT STATE _ _
1 typedef enum { 2 RK BT STATE OFF, //closed _ _ _ 3 RK BT STATE ON, //turned off _ _ _ 4 RK BT STATE TURNING ON, //is turnning on _ _ _ _ 5 RK BT STATE TURNING OFF, //is trunning off _ _ _ _ 6 } RK BT STATE; _ _
1 2 3 4 5 6typedef enum { RK BT STATE OFF, //closed _ _ _ RK BT STATE ON, //turned off _ _ _ RK BT STATE TURNING ON, //is turnning on _ _ _ _ RK BT STATE TURNING OFF, //is trunning off _ _ _ _ } RK BT STATE; _ _
1 typedef enum { 2 RK BT BOND STATE NONE, //pairing failed or unpaired _ _ _ _ 3 RK BT BOND STATE BONDING, //is pairing _ _ _ _ 4 RK BT BOND STATE BONDED, //paired successfully _ _ _ _ 5 } RK BT BOND STATE; _ _ _
1 2 3 4 5typedef enum { RK BT BOND STATE NONE, //pairing failed or unpaired _ _ _ _ RK BT BOND STATE BONDING, //is pairing _ _ _ _ RK BT BOND STATE BONDED, //paired successfully _ _ _ _ } RK BT BOND STATE; _ _ _
RK BT SCAN TYPE _ _ _
1 typedef enum { 2 SCAN TYPE AUTO, //LE and BR/EDR, scan all types of devices _ _ 3 SCAN TYPE BREDR, //scan BR/EDR type devices only _ _ 4 SCAN TYPE LE //scan LE type devices only _ _ 5 } RK BT SCAN TYPE; _ _ _
1 2 3 4 5typedef enum { SCAN TYPE AUTO, //LE and BR/EDR, scan all types of devices _ _ SCAN TYPE BREDR, //scan BR/EDR type devices only _ _ SCAN TYPE LE //scan LE type devices only _ _ } RK BT SCAN TYPE; _ _ _
RK BT DISCOVERY STATE _ _ _
1 typedef enum { 2 RK BT DISC STARTED, //start scanning successfully _ _ _ 3 RK BT DISC STOPPED AUTO, //scan completed, automatically stop scanning _ _ _ _ 4 RK BT DISC START FAILED, //start scanning failed _ _ _ _ 5 RK BT DISC STOPPED BY USER, //interrupt scanning by _ _ _ _ _ rk bt cancel discovery, _ _ _ 6 } RK BT DISCOVERY STATE; _ _ _
1 2 3 4 5 6typedef enum { RK BT DISC STARTED, //start scanning successfully _ _ _ RK BT DISC STOPPED AUTO, //scan completed, automatically stop scanning _ _ _ _ RK BT DISC START FAILED, //start scanning failed _ _ _ _ RK BT DISC STOPPED BY USER, //interrupt scanning by _ _ _ _ _ rk bt cancel discovery, _ _ _ } RK BT DISCOVERY STATE; _ _ _

RK_BT_PLAYROLE_TYPE introduction

1 typedef enum { 2 PLAYROLE TYPE UNKNOWN, //unknown device _ _ 3 PLAYROLE TYPE SOURCE, //a2dp Source device _ _ 4 PLAYROLE TYPE SINK, //a2dp Sink device _ _ 5 } RK BT PLAYROLE TYPE; _ _ _
1 2 3 4 5typedef enum { PLAYROLE TYPE UNKNOWN, //unknown device _ _ PLAYROLE TYPE SOURCE, //a2dp Source device _ _ PLAYROLE TYPE SINK, //a2dp Sink device _ _ } RK BT PLAYROLE TYPE; _ _ _
typedef void (*RK_BT_STATE_CALLBACK)(RK_BT_STATE state)``typedef void
(*RK_BT_STATE_CALLBACK)(RK_BT_STATE state)

Bluetooth status callback

typedef void (*RK_BT_BOND_CALLBACK)(const char *bd_addr, const char *name,
RK_BT_BOND_STATE state)

Bluetooth pairing status callback, bd_addr: address of current bound device, name: name of current paired device

typedef void (*RK_BT_DISCOVERY_CALLBACK)(RK_BT_DISCOVERY_STATE state)

Bluetooth scanning status callback, if rk_bt_start_discovery is used to scan the surrounding Bluetooth devices, you need to register this callback

typedef void (*RK_BT_DEV_FOUND_CALLBACK)(const char *address, const char *name,
unsigned int bt_class, int rssi)

Bluetooth device scan callback. If you use rk_bt_start_discovery to scan the surrounding Bluetooth devices, you need to register this callback. Bluez triggers this callback every time it scans a device; after bsa scan, it will trigger the callback in turn according to the number of devices scanned.

typedef void (*RK_BT_NAME_CHANGE_CALLBACK)(const char *bd_addr, const char
*name)

Remote device name update callback

typedef void (*RK_BT_MTU_CALLBACK)(const char *bd_addr, unsigned int mtu)

ble MTU callback, shared with ble and ble client, after successful MTU negotiation, the callback is triggered

void rk_bt_register_state_callback(RK_BT_STATE_CALLBACK cb)

Register the callback function to get the Bluetooth start status

void rk_bt_register_bond_callback(RK_BT_BOND_CALLBACK cb)

Register callback function to get Bluetooth pairing status

void rk_bt_register_discovery_callback(RK_BT_DISCOVERY_CALLBACK cb)

Register the callback function to get the Bluetooth scanning status

void rk_bt_register_dev_found_callback(RK_BT_DEV_FOUND_CALLBACK cb)

Register the callback function of the discovered device

void rk_bt_register_name_change_callback(RK_BT_NAME_CHANGE_CALLBACK cb)

Registered device name update callback function

int rk_bt_init(RkBtContent *p_bt_content)

To initialize Bluetooth service, this interface should be called to initialize Bluetooth basic services before calling other Bluetooth interfaces.

int rk_bt_deinit(void)

To de-initialize Bluetooth service.

int rk_bt_is_connected(void)

To get whether there is a service connected to Bluetooth currently. Any one of SPP/BLE/SINK/SOURCE services is connected, it will return 1; otherwise return 0.

int rk_bt_set_class(int value)

Set the type of Bluetooth device. value: the type's value. For example, 0x240404 means:

Major Device Class: Audio/Video Minor Device Class: Wearable headset device

Service Class: Audio (Speaker, Microphone, Headset service), Rendering (Printing, Speaker)

int rk_bt_enable_reconnect(int value)

To enables/disables the auto reconnect function of HFP/A2DP SINK. value: 0 means disable the auto reconnect function, 1 means enable the auto reconnect function.

void rk_bt_start_discovery(unsigned int mseconds, RK_BT_SCAN_TYPE scan_type)

Start Bluetooth scanning, mseconds: scan duration, in milliseconds; scan_type: scan type, see the description of RK_BT_SCAN_TYPE for details, only bluez supports scan type filtering, bsa only supports full type scan.

void rk_bt_cancel_discovery()

Stop Bluetooth scanning and cancel the scanning operation initiated by rk_bt_start_discovery

bool rk_bt_is_discovering()

Whether Bluetooth is in the state of scanning the surrounding devices, returning true if the device is being scanned, otherwise false

void rk_bt_display_devices()

Print a list of currently scanned devices

int rk_bt_pair_by_addr(char *addr)

Pair with the device specified by addr actively; addr: device address, such as: 94:87:E0:B6:6D:AE

int rk_bt_unpair_by_addr(char *addr)

Cancel pairing with the device specified by addr. After canceling the pairing, all records of the device will be deleted; addr: device address

int rk_bt_set_device_name(char *name)

Set the local device name, name: the device name you want to set

int rk_bt_get_device_name(char *name, int len)

Get the local device name, name: used to store the obtained device name, len: the length of the device name

int rk_bt_get_device_addr(char *addr, int len)

Get the local device's Bluetooth mac address, addr: used to store the obtained mac address, len: the length of mac address

void rk_bt_display_paired_devices()

Print the currently paired device list

int rk_bt_get_paired_devices(RkBtScanedDevice**dev_list,int *count)

Get the currently paired device list, dev_list: used to store the paired device list, count: the number of paired devices

int rk_bt_free_paired_devices(RkBtScanedDevice*dev_list)

Free the memory allocated by rk_bt_get_paired_devices to store the device list

int rk_bt_get_scaned_devices(RkBtScanedDevice**dev_list,int *count)

Get the currently scanned device list, dev_list: used to store the scanned device list, count: the number of scanned devices

int rk_bt_free_scaned_devices(RkBtScanedDevice*dev_list)

Free the memory allocated by rk_bt_get_scaned_devices to store the device list

void rk_bt_set_bsa_server_path(char *path)

Set the bsa_server.sh path, is /usr/bin/bsa_server.sh (BSA only) by default

bool rk_bt_get_connected_properties(char *addr)

Get the connection status of the device specified by addr, addr: device address, return true if connected, otherwise true false (BLUEZ only)

int rk_bt_set_visibility(const int visiable, const int connectal)

Set visible/connectable properties. visiable: 0 means invisible, 1 means visible. connectal: 0 means not connectable, 1 means connectable. Only applicable to BR/EDR devices

RK_BT_PLAYROLE_TYPE rk_bt_get_playrole_by_addr(char *addr)

Get the playrole of the device specified by addr, see the description of RK_BT_PLAYROLE_TYPE for details.

int rk_bt_read_remote_device_name(char *addr, int transport)

Read the name of the device specified by addr, transport specifies the device type, unknown device: RK_BT_TRANSPORT_UNKNOWN, BR/EDR device: RK_BT_TRANSPORT_BR_EDR, LE device: RK_BT_TRANSPORT_LE. This interface needs to be used matched with rk_bt_register_name_change_callback. Reading successfully will trigger RK_BT_NAME_CHANGE_CALLBACK callback (BSA only)

2. BLE Interface Introduction (RkBle.h)​

RK_BLE_STATE introduction

1 typedef enum { 2 RK BLE STATE IDLE = 0, //idle state _ _ _ 3 RK BLE STATE CONNECT, //successful connection _ _ _ 4 RK BLE STATE DISCONNECT //disconnected _ _ _ 5 } RK BLE STATE; _ _
1 2 3 4 5typedef enum { RK BLE STATE IDLE = 0, //idle state _ _ _ RK BLE STATE CONNECT, //successful connection _ _ _ RK BLE STATE DISCONNECT //disconnected _ _ _ } RK BLE STATE; _ _
typedef void (*RK BLE STATE CALLBACK)(const char *bd addr, const char *name, _ _ _ _
RK_BLE_STATE state)

BLE state callback function. bd_addr: remote device address, name: remote device name.

typedef void (*RK_BLE_RECV_CALLBACK)(const char *uuid, char *data, int len)

BLE receiving callback function. uuid: CHR UUID, data: data pointer, len: data's length

int rk_ble_register_status_callback(RK_BLE_STATE_CALLBACK cb)

This interface is used to register a callback function to get BLE connection status.

int rk_ble_register_recv_callback(RK_BLE_RECV_CALLBACK cb)

This interface is used to register a callback function to receive BLE data. There are two ways to register the receiving callback function: one is specified by the RkBtContent parameter of the rk_bt_init () interface; the other is to call this interface for registration. For BLUEZ DEVICEIO, both of the two methods are available, but for BSA DEVICEIO, you can only use this interface to register the receiving callback function.

void rk_ble_register_mtu_callback(RK_BT_MTU_CALLBACK cb)

This interface is used to register mtu callback. After mtu negotiation is successful, RK_BT_MTU_CALLBACK callback is triggered to report the negotiated mtu value

int rk_ble_start(RkBleContent *ble_content)

To enable BLE broadcast. ble_content: should be consistent with the p_bt_content->ble_content in the rk_bt_init(RkBtContent *p_bt_content).

int rk_ble_stop(void)

Stop BLE broadcast. After this function is executed, BLE becomes invisible and disconnected.

int rk_ble_get_state(RK_BLE_STATE *p_state)

Get the current connection status of BLE actively.

rk_ble_write(const char *uuid, char *data, int len)

Send data to the other side.

uuid: the CHR object of the written data

data: the pointer of the written data

len: the length of the written data. You should pay attention to that: the length is limited by the MTU connected to BLE, and it will be cut off when over the MTU.

The current MTU's value is set to 134 Bytes by default to maintain a good compatibility

int rk_bt_ble_set_visibility(const int visiable, const int connect)

Set ble visible/connectable characteristics. visible: 0 means invisible, 1 means visible. connect: 0 means not connectable, 1 means connectable. This interface is only applicable to bsa (BSA only)

int rk_ble_disconnect(void)

Disconnect the current ble connection actively

int rk_ble_set_address(char *address)

Set the ble address, you can also use ble_addr parameter setting in rk_bt_init, the default random address is not set (BLUEZ Only)

int rk_ble_set_adv_interval(unsigned short adv_int_min, unsigned short
adv_int_max)

Set the ble broadcast interval, adv_int_min minimum broadcast interval, adv_int_max maximum broadcast interval, minimum value is 32 (32 * 0.625ms = 20ms), is 30ms when not set bsa default interval, bluez is 100ms by default.

3. BLE CLIENT Interface Introduction (RkBtSpp.h)​

RK_BT_SPP_STATE introduction

1 typedef enum { 2 RK BT SPP STATE IDLE = 0, //idle state _ _ _ _ 3 RK BT SPP STATE CONNECT, //successful connection _ _ _ _ 4 RK BT SPP STATE DISCONNECT //disconnected state _ _ _ _ 5 } RK BT SPP STATE; _ _ _
1 2 3 4 5typedef enum { RK BT SPP STATE IDLE = 0, //idle state _ _ _ _ RK BT SPP STATE CONNECT, //successful connection _ _ _ _ RK BT SPP STATE DISCONNECT //disconnected state _ _ _ _ } RK BT SPP STATE; _ _ _
RK BLE CLIENT SERVICE INFO _ _ _ _
typedef struct {
int service_cnt; //number of services
included in the connected remote device
RK_BLE_CLIENT_SERVICE service[SERVICE_COUNT_MAX]; //detailed information
for each service
} RK_BLE_CLIENT_SERVICE_INFO;
typedef struct {
char describe[DESCRIBE_BUG_LEN]; //uuid description
char path[PATH_BUF_LEN];
char uuid[UUID_BUF_LEN]; //service uuid
int chrc_cnt; //the number of characteristics
included in the service
RK_BLE_CLIENT_CHRC chrc[CHRC_COUNT_MAX]; //detailed information for each
characteristic
} RK_BLE_CLIENT_SERVICE;
typedef struct {
char describe[DESCRIBE_BUG_LEN]; //uuid description
char path[PATH_BUF_LEN];
char uuid[UUID_BUF_LEN]; //characteristic uuid
unsigned int props; //characteristic attributes
unsigned int ext_props; //characteristic extended
attributes
unsigned int perm; //characteristic permission
bool notifying; //whether characteristic open
notification(BLUEZ only)
int desc_cnt; //the number of descriptors
contained in this characteristic
RK_BLE_CLIENT_DESC desc[DESC_COUNT_MAX]; //detailed information for each
descriptor
} RK_BLE_CLIENT_CHRC;
typedef struct {
char describe[DESCRIBE_BUG_LEN]; //uuid description
char path[PATH_BUF_LEN];
char uuid[UUID_BUF_LEN]; //descriptor uuid
} RK_BLE_CLIENT_DESC;
Note: t search, t
*name, RK BLE CLIENT STATE state) _ _ _
b

Note: the path indicates the relationship between service, characteristic, and descriptor. It is used to traversal

typedef void (*RK_BLE_CLIENT_STATE_CALLBACK)(const char *bd_addr, const char

ble client status callback function, bd_addr: remote device address, name: remote device name.

typedef void (*RK_BLE_CLIENT_RECV_CALLBACK)(const char *uuid, char *data, int
len)

ble client data reception callback function. uuid: CHR UUID, data: data pointer, len: data length.

void rk_ble_client_register_state_callback(RK_BLE_CLIENT_STATE_CALLBACK cb)

Register ble client status callback function

int rk_ble_client_register_recv_callback(RK_BLE_CLIENT_RECV_CALLBACK cb)

Register ble client data reception callback function

int rk_ble_client_open(void)

Initialize ble client

void rk_ble_client_close(void)

Deinitialize ble client

RK_BLE_CLIENT_STATE rk_ble_client_get_state()

Get ble client status actively

int rk_ble_client_get_service_info(char *address, RK_BLE_CLIENT_SERVICE_INFO
*info)

Get the specified information of the device by address, including service uuid, characteristic uuid, permission, Properties, descriptor uuid, etc. Please refer to the RK_BLE_CLIENT_SERVICE_INFO structure for details.

int rk_ble_client_write(const char *uuid, char *data, int data_len)

Send data to the specify uuid of the opposite side, data: data pointer, len: data length.

int rk_ble_client_read(const char *uuid)

Read the specified uuid data from the opposite side, and it will trigger the RK_BLE_CLIENT_RECV_CALLBACK callback when read successfully.

int rk_ble_client_connect(char *address)

Connect to the device with the specified address

int rk_ble_client_disconnect(char *address)

Disconnect from the device with the specified address

bool rk_ble_client_is_notifying(const char *uuid)

Search whether the specified uuid has enabled notification, and returns true (BLUEZ only) when it is enable.

int rk_ble_client_notify(const char *uuid, bool enable)

Set the notification with specified uuid. The uuid must support notifications or indications. It is turned on when enable = true and turned off when enable = false. When the remote device (server) writes the uuid, it will trigger the RK_BLE_CLIENT_RECV_CALLBACK callback to report the modified value automatically.

int rk_ble_client_get_eir_data(char *address, char *eir_data, int len)

Get the broadcast data of the remote device specified by address, eir_data: the obtained broadcast data, len: the length of the broadcast data

int rk_ble_client_default_data_length()

Force to specify the length of hci writing data to be 27 bytes, which is customized for specific customers. Generally, this API is not used (BSA only)

4. SPP Interface Introduction (RkBtSpp.h)​

RK_BT_SPP_STATE introduction

1 typedef enum { 2 RK BT SPP STATE IDLE = 0, //idle state _ _ _ _ 3 RK BT SPP STATE CONNECT, //successful connection _ _ _ _ 4 RK BT SPP STATE DISCONNECT //disconnected state _ _ _ _ 5 } RK BT SPP STATE; _ _ _
1 2 3 4 5typedef enum { RK BT SPP STATE IDLE = 0, //idle state _ _ _ _ RK BT SPP STATE CONNECT, //successful connection _ _ _ _ RK BT SPP STATE DISCONNECT //disconnected state _ _ _ _ } RK BT SPP STATE; _ _ _
typedef void (*RK BT SPP STATUS CALLBACK)(RK BT SPP STATE status) _ _ _ _ _ _ _

State callback function.

typedef void (*RK_BT_SPP_RECV_CALLBACK)(char *data, int len)

Reception callback function. data: data pointer, len: data length.

int rk_bt_spp_register_status_cb(RK_BT_SPP_STATUS_CALLBACK cb)

Registration status callback function.

int rk_bt_spp_register_recv_cb(RK_BT_SPP_RECV_CALLBACK cb)

Registration reception callback function.

int rk_bt_spp_open(void)

Turn on SPP, the device is in the connectable state.

int rk_bt_spp_close(void)

Close SPP。

int rk_bt_spp_get_state(RK_BT_SPP_STATE *pState)

Get the current SPP connection status actively

int rk_bt_spp_write(char *data, int len)

Send data. data: data pointer, len: data length.

5. A2DP SINK Interface Introduction (RkBtSink.h)​

BtTrackInfo structure

1 typedef struct btmg track info t { _ _ _ 2 char title[256]; //title 3 char artist[256]; //artist 4 char album[256]; //album 5 char track num[64]; //track the number of the song in the _ album 6 char num tracks[64]; //total number of the album _ 7 char genre[256]; //genres 8 char playing time[256]; //total time of playing _ 9 } btmg track info t; _ _ _ 10 11 typedef struct btmg track info t BtTrackInfo; _ _ _
1 2 3 4 5 6 7 8 9 10 11typedef struct btmg track info t { _ _ _ char title[256]; //title char artist[256]; //artist char album[256]; //album char track num[64]; //track the number of the song in the _ album char num tracks[64]; //total number of the album _ char genre[256]; //genres char playing time[256]; //total time of playing _ } btmg track info t; _ _ _ typedef struct btmg track info t BtTrackInfo; _ _ _
R
RK BT SINK STATE _ _ _
1 typedef enum { 2 RK BT SINK STATE IDLE = 0, //idle sate _ _ _ _ 3 RK BT SINK STATE CONNECT, //connected state _ _ _ _ 4 RK BT SINK STATE DISCONNECT //disconnected _ _ _ _ 5 RK BT SINK STATE PLAY , //avrcp playing state _ _ _ _ 6 RK BT SINK STATE PAUSE, //avrcp pause state _ _ _ _ 7 RK BT SINK STATE STOP, //avrcp stop state _ _ _ _ 8 RK BT A2DP SINK STARTED, //avdtp playing state _ _ _ _ 9 RK BT A2DP SINK SUSPENDED, //avdtp pause state _ _ _ _ 10 RK BT A2DP SINK STOPPED, //avdtp stop state _ _ _ _ 11 } RK BT SINK STATE; _ _ _
1 2 3 4 5 6 7 8 9 10 11typedef enum { RK BT SINK STATE IDLE = 0, //idle sate _ _ _ _ RK BT SINK STATE CONNECT, //connected state _ _ _ _ RK BT SINK STATE DISCONNECT //disconnected _ _ _ _ RK BT SINK STATE PLAY , //avrcp playing state _ _ _ _ RK BT SINK STATE PAUSE, //avrcp pause state _ _ _ _ RK BT SINK STATE STOP, //avrcp stop state _ _ _ _ RK BT A2DP SINK STARTED, //avdtp playing state _ _ _ _ RK BT A2DP SINK SUSPENDED, //avdtp pause state _ _ _ _ RK BT A2DP SINK STOPPED, //avdtp stop state _ _ _ _ } RK BT SINK STATE; _ _ _

The avdtp state is mainly used for reporting a2dp sink state during WeChat calls and WeChat voices, because the avrcp status change will not be triggered at this time.

typedef int (*RK_BT_SINK_CALLBACK)(RK_BT_SINK_STATE state)

Status callback function.

typedef void (*RK_BT_SINK_VOLUME_CALLBACK)(int volume)

Volume change callback function. Which is called when the volume of the mobile phone changes. volume: the new volume value. Note: Due to the different implementations of AVRCP version and different mobile phone manufacturers, some mobile phones are not compatible with this function, iPhone series phones support this interface well.

typedef void (*RK_BT_AVRCP_TRACK_CHANGE_CB)(const char *bd_addr, BtTrackInfo
track_info)

Song information callback function, which will be triggered when the playing song changes. bd_addr: remote device address, track_info: song information

typedef void (*RK_BT_AVRCP_PLAY_POSITION_CB)(const char *bd_addr, int song_len,
int song_pos)

Song playback progress callback, when the remote device supports position change, it will automatically report the playback progress and trigger this function. bd_addr: remote device address, song_len: total song length, song_pos: current playback progress

typedef void (*RK_BT_SINK_UNDERRUN_CB)(void)

Playback underrun status callback, which will be triggered automatically when playing underrun, this interface is only applicable to bluez (Bluez only).

int rk_bt_sink_register_callback(RK_BT_SINK_CALLBACK cb)

Register a status callback function.

int rk_bt_sink_register_volume_callback(RK_BT_SINK_VOLUME_CALLBACK cb)

Register the volume change callback function.

int rk_bt_sink_register_track_callback(RK_BT_AVRCP_TRACK_CHANGE_CB cb)

Register the song information callback function

int rk_bt_sink_register_position_callback(RK_BT_AVRCP_PLAY_POSITION_CB cb)

Register the song playback progress callback

void rk_bt_sink_register_underurn_callback(RK_BT_SINK_UNDERRUN_CB cb)

Register the underrun callback function, which is only applicable to bluez (Bluez only)

int rk_bt_sink_open()

To enable A2DP SINK service. If A2DP SINK is required to coexist with HFP, please refer to rk_bt_hfp_sink_open interface in the chapter of "HFP-HF Interface Introduction"

int rk_bt_sink_close(void)

Close A2DP Sink function.

int rk_bt_sink_get_state(RK_BT_SINK_STATE *p_state)

To get A2DP Sink connection status actively.

int rk_bt_sink_play(void)

Reverse control: play.

int rk_bt_sink_pause(void)

Reverse control: pause

int rk_bt_sink_prev(void)

Reverse control: previous

int rk_bt_sink_next(void)

Reverse control: next

int rk_bt_sink_stop(void)

Reverse control: stop playing

i nt rk_bt_sink_volume_up(void)

Reverse control: increase the volume. Volume range [0, 127], each time the interface is called, the volume increases by 8.

Note: Due to the different implementations of AVRCP version and different mobile phone manufacturers, some mobile phones are not compatible with this function. iPhone series phones support this interface well.

i nt rk_bt_sink_volume_down(void)

Reverse control: reduce the volume . Volume range [0, 127], each time the interface is called, the volume reduce by 8.

Note: Due to the different implementations of AVRCP version and different mobile phone manufacturers, some mobile phones are not compatible with this function. iPhone series phones support this interface well.

int rk_bt_sink_set_volume(int volume)

Reverse control: Set the volume of A2DP SOURCE. The volume range [0, 127]. If it exceeds the value range, the interface will correct automatically .

Note: Due to the different implementations of AVRCP version and different mobile phone manufacturers, some mobile phones are not compatible with this function. iPhone series phones support this interface well.

int rk_bt_sink_disconnect()

Disconnect A2DP Sink.

int rk_bt_sink_connect_by_addr(char *addr)

Connect to the device specified by addr actively; addr: device address, like "94:87:E0:B6:6D:AE"

int rk_bt_sink_disconnect_by_addr(char *addr)

Disconnect the device specified by addr actively; addr: device address, like "94:87:E0:B6:6D:AE"

int rk_bt_sink_get_default_dev_addr(char *addr, int len)

Get the address of the currently connected remote device (BLUEZ only)

int rk_bt_sink_get_play_status()

Get the playback status of the currently connected remote device. When the remote device does not support reporting the playback progress actively, you can get the playback progress through this interface. Calling this interface will trigger the RK_BT_AVRCP_PLAY_POSITION_CB callback.

bool rk_bt_sink_get_poschange()

Whether the currently connected remote device supports reporting the progress of the playback actively; if it does, returns true, otherwise returns false.

void rk_bt_sink_set_alsa_device(char *alsa_dev)

To set the Bluetooth playback device node, it must be called after rk_bt_sink_open. Use "default" by default, this interface is only applicable to bsa (BSA only)

The bluez playback device node is located in external/bluez-alsa/utils/aplay.c, which can be modified by yourselves.

6. A2DP SOURCE Interface Introduction (RkBtSource.h)​

BtDeviceInfo introduction

1 typedef struct bt device info { _ _ _ 2 char name[128]; // bt name 3 char address[17]; // bt address 4 bool rssi valid; _ 5 int rssi; 6 char playrole[12]; // audio Sink? audio Source? unknown? 7 } BtDeviceInfo;
1 2 3 4 5 6 7typedef struct bt device info { _ _ _ char name[128]; // bt name char address[17]; // bt address bool rssi valid; _ int rssi; char playrole[12]; // audio Sink? audio Source? unknown? } BtDeviceInfo;
The a

address. rssi_valid: indicates whether rssi is valid. rssi: signal strength. playrole: device role, values: "Audio Sink", "Audio Source", "Unknown".

BtScanParam introduction

1 typedef struct bt scan parameter { _ _ _ 2 unsigned short mseconds; 3 unsigned char item cnt; _ 4 BtDeviceInfo devices[BT SOURCE SCAN DEVICES CNT]; _ _ _ _ 5 } BtScanParam;
1 2 3 4 5typedef struct bt scan parameter { _ _ _ unsigned short mseconds; unsigned char item cnt; _ BtDeviceInfo devices[BT SOURCE SCAN DEVICES CNT]; _ _ _ _ } BtScanParam;

This structure is used to save the list of devices scanned in the rk_bt_source_scan (BtScanParam * data) interface. mseconds: scan time. item_cnt: the number of scanned devices. devices: device's information. BT_SOURCE_SCAN_DEVICES_CNT value is 30, which means that the interface scans up to 30 devices.

RK_BT_SOURCE_EVENT introduction

1 typedef enum { 2 BT SOURCE EVENT CONNECT FAILED, //fail to connect A2DP Sink device _ _ _ _ 3 BT SOURCE EVENT CONNECTED, //connect to A2DP Sink device _ _ _ successfully 4 BT SOURCE EVENT DISCONNECT FAILED, //fail to diconnect(BLUEZ only) _ _ _ _ 5 BT SOURCE EVENT DISCONNECTED, //disconnect _ _ _ 6 /* reverse control event on the Sink side*/ 7 BT SOURCE EVENT RC PLAY, //play _ _ _ _ 8 BT SOURCE EVENT RC STOP, //stop _ _ _ _ 9 BT SOURCE EVENT RC PAUSE, //pause _ _ _ _ 10 BT SOURCE EVENT RC FORWARD, //Previous _ _ _ _ 11 BT SOURCE EVENT RC BACKWARD, //next _ _ _ _ 12 BT SOURCE EVENT RC VOL UP, //volume+ _ _ _ _ _ 13 BT SOURCE EVENT RC VOL DOWN, //volume- _ _ _ _ _ 14 BT SOURCE EVENT AUTO RECONNECTING, //is reconnecting(BLUEZ only) _ _ _ _ 15 } RK BT SOURCE EVENT; _ _ _
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15typedef enum { BT SOURCE EVENT CONNECT FAILED, //fail to connect A2DP Sink device _ _ _ _ BT SOURCE EVENT CONNECTED, //connect to A2DP Sink device _ _ _ successfully BT SOURCE EVENT DISCONNECT FAILED, //fail to diconnect(BLUEZ only) _ _ _ _ BT SOURCE EVENT DISCONNECTED, //disconnect _ _ _ /* reverse control event on the Sink side*/ BT SOURCE EVENT RC PLAY, //play _ _ _ _ BT SOURCE EVENT RC STOP, //stop _ _ _ _ BT SOURCE EVENT RC PAUSE, //pause _ _ _ _ BT SOURCE EVENT RC FORWARD, //Previous _ _ _ _ BT SOURCE EVENT RC BACKWARD, //next _ _ _ _ BT SOURCE EVENT RC VOL UP, //volume+ _ _ _ _ _ BT SOURCE EVENT RC VOL DOWN, //volume- _ _ _ _ _ BT SOURCE EVENT AUTO RECONNECTING, //is reconnecting(BLUEZ only) _ _ _ _ } RK BT SOURCE EVENT; _ _ _
R
1 typedef enum { 2 BT SOURCE STATUS CONNECTED, //connected state _ _ _ 3 BT SOURCE STATUS DISCONNECTED, //disconnected state _ _ _ 4 } RK BT SOURCE STATUS; _ _ _
1 2 3 4typedef enum { BT SOURCE STATUS CONNECTED, //connected state _ _ _ BT SOURCE STATUS DISCONNECTED, //disconnected state _ _ _ } RK BT SOURCE STATUS; _ _ _
typedef void (*RK_BT_SOURCE_CALLBACK)(void *userdata, const char *bd_addr,
const char *name, const RK_BT_SOURCE_EVENT event)

Status callback function. userdata: user pointer, bd_addr: address of the connected remote device, name: name of the connected remote device, event: connection event. It is recommended to register the status callback function before the rk_bt_source_open interface to avoid state events losing.

int rk_bt_source_register_status_cb(void *userdata, RK_BT_SOURCE_CALLBACK cb)

Registration status callback function.

int rk_bt_source_auto_connect_start(void *userdata, RK_BT_SOURCE_CALLBACK cb)

Scans nearby Audio Sink devices, and connects to the device with strongest rssi automatically. userdata: user pointer, cb: status callback function. The time for the interface automatically scans is 10 seconds. If no Audio Sink device is scanned within 10 seconds, the interface will not do any operation. If an Audio Sink device is scanned, the basic information of the device will be printed. If the Audio Sink device cannot be scanned, it will print "=== Cannot find audio Sink devices. ==="; if the signal strength of the scanned device is too low, the connection will fail and print “=== BT SOURCE RSSI is too weak !!! ===”.

int rk_bt_source_auto_connect_stop(void)

Turn off automatic scan.

int rk_bt_source_open(void)

Open A2DP Source function。

int rk_bt_source_close(void)

Close A2DP Source function。

int rk_bt_source_get_device_name(char *name, int len)

Get local device name. name: the buffer to store the name, len: size of the name space

int rk_bt_source_get_device_addr(char *addr, int len)

Get the local device address. addr: the buffer to store the address, len: the size of the addr space.

int rk_bt_source_get_status(RK_BT_SOURCE_STATUS *pstatus, char *name, int
name_len, char *addr, int addr_len)

Get A2DP source connection status. pstatus: a pointer to store the current status value. If it is in the connected status, name stores the name of the device on the other side(A2DP Sink), name_len: is the name's length, addr: stores the address of the device on the other side(A2DP Sink), and addr_len is the length of addr. Both the name and addr parameters can be empty.

int rk_bt_source_scan(BtScanParam *data)

To scan device. The scanning parameters are specified by data , and the scanned results are also stored in data . For details, please see the introduction of BtScanParam.

int rk_bt_source_connect_by_addr(char *address)

Connect to the device specified by address automatically.

int rk_bt_source_disconnect_by_addr(char *address)

Disconnect to the device specified by address .

int rk_bt_source_disconnect()

Disconnect.

int rk_bt_source_remove(char *address)

Delete the connected device. It will not connect automatically after deletion.

int rk_bt_source_resume(void)

Go on playing (BSA only)

int rk_bt_source_stop(void)

Stop playing (BSA only)

int rk_bt_source_pause(void)

Pause to play (BSA only)

int rk_bt_source_vol_up(void)

increase volume (BSA only)

int rk_bt_source_vol_down(void)

Decrease volume (BSA only)

7. HFP-HF Interface Introduction (RkBtHfp.h)​

RK_BT_HFP_EVENT Introduction

1 typedef enum { 2 RK BT HFP CONNECT EVT, // HFP connected successfully _ _ _ _ 3 RK BT HFP DISCONNECT EVT, // HFP disconnected _ _ _ _ 4 RK BT HFP RING EVT, // received ringing signal from AG (mobile _ _ _ _ phone) 5 RK BT HFP AUDIO OPEN EVT, // connected _ _ _ _ _ 6 RK BT HFP PICKUP EVT, // answer the phone actively _ _ _ _ 7 RK BT HFP HANGUP EVT, // hangup the phone actively _ _ _ _ 8 RK BT HFP VOLUME EVT, // AG (Mobile phone) Volume Change _ _ _ _ 9 } RK BT HFP EVENT; _ _ _
1 2 3 4 5 6 7 8 9typedef enum { RK BT HFP CONNECT EVT, // HFP connected successfully _ _ _ _ RK BT HFP DISCONNECT EVT, // HFP disconnected _ _ _ _ RK BT HFP RING EVT, // received ringing signal from AG (mobile _ _ _ _ phone) RK BT HFP AUDIO OPEN EVT, // connected _ _ _ _ _ RK BT HFP PICKUP EVT, // answer the phone actively _ _ _ _ RK BT HFP HANGUP EVT, // hangup the phone actively _ _ _ _ RK BT HFP VOLUME EVT, // AG (Mobile phone) Volume Change _ _ _ _ } RK BT HFP EVENT; _ _ _
RK BT SCO CODEC TYPE _ _ _ _
1 typedef enum { 2 BT SCO CODEC CVSD, // CVSD(8K sampling), Bluetooth _ _ _ required to support 3 BT SCO CODEC MSBC, // mSBC(16K sampling), Optional _ _ _ support 4 } RK BT SCO CODEC TYPE; _ _ _ _
1 2 3 4typedef enum { BT SCO CODEC CVSD, // CVSD(8K sampling), Bluetooth _ _ _ required to support BT SCO CODEC MSBC, // mSBC(16K sampling), Optional _ _ _ support } RK BT SCO CODEC TYPE; _ _ _ _
typedef int (*RK_BT_HFP_CALLBACK)(RK_BT_HFP_EVENT event, void *data)

HFP status callback function. event: refer to the introduction of RK_BT_HFP_EVENT above. data: when event is RK_BT_HFP_VOLUME_EVT , *((int *)data) is the volume value displayed on the current AG (mobile phone).Note: the actual call volume still needs to be handled accordingly on the board.

void rk_bt_hfp_register_callback(RK_BT_HFP_CALLBACK cb)

Register a HFP callback function, which is recommended to be called before rk_bt_hfp_sink_open to avoid losing state events.

int rk_bt_hfp_sink_open(void)

Turn on HFP-HF and A2DP SINK functions at the same time. BSA DEVICEIO can call this interface, or call the A2DP Sink open and HFP open interfaces separately to realize the coexistence of HFP-HF and A2DP SINK. But BLUEZ DEVICEIO can only realize the coexistence of HFP-HF and A2DP SINK through this interface. For A2DP SINK and HFP-HF, the registration of callback functions and the functional interface are still separate. It is best to call rk_bt_hfp_register_callback and rk_bt_sink_register_callback before rk_bt_hfp_sink_open to avoid losing events. For BLUEZ DEVICEIO, before calling rk_bt_hfp_sink_open interface, you cannot call rk_bt_hfp_open and rk_bt_sink_open functions, otherwise the interface returns -1. The reference code is as follows:

1 /*opens A2DP SINK and HFP HF functions in coexistence mode */ 2 rk bt sink register callback(bt sink callback); _ _ _ _ _ _ 3 rk bt hfp register callback(bt hfp hp callback); _ _ _ _ _ _ _ 4 rk bt hfp sink open(); _ _ _ _
1 2 3 4/*opens A2DP SINK and HFP HF functions in coexistence mode */ rk bt sink register callback(bt sink callback); _ _ _ _ _ _ rk bt hfp register callback(bt hfp hp callback); _ _ _ _ _ _ _ rk bt hfp sink open(); _ _ _ _
1 /* close the operation */ 2 rk bt hfp close(); //close HFP HF _ _ _ 3 rk bt sink close(); //close A2DP SINK _ _ _
1 2 3/* close the operation */ rk bt hfp close(); //close HFP HF _ _ _ rk bt sink close(); //close A2DP SINK _ _ _
int rk_bt_hfp_open(void)

Turn on HFP service.

BLUEZ DEVICEIO: this interface is mutually exclusive with rk_bt_sink_open . Calling this interface will automatically exit A2DP protocol related services, and then start HFP service. If A2DP SINK and HFP need to coexist, please refer to rk_bt_hfp_sink_open .

BSA DEVICEIO: there is no mutual exclusion between this interface and rk_bt_sink_open

int rk_bt_hfp_close(void)

Turn off HFP service。

int rk_bt_hfp_pickup(void)

Answer the phone actively

int rk_bt_hfp_hangup(void)

Hang up actively.

int rk_bt_hfp_redial(void)

Recall the last dialed phone number in the call list. Note: it is "call out" phone number, not the most recent phone number in the call list. For example, in the following case, calling rk_bt_hfp_redial interface will call back rockchip-003.

<1> rockchip-001 [Call in]

<2> rockchip-002 [Call in]

<3> rockchip-003 [Call out]

int rk_bt_hfp_dial_number(char *number)

Dial the phone number specified by "number"

int rk_bt_hfp_report_battery(int value)

Report the battery level. value: battery power value, the value range is [0, 9].

int rk_bt_hfp_set_volume(int volume)

Set the speaker volume of AG (mobile phone). volume: volume value, range is [0, 15]. When AG device is a mobile phone, after calling this interface, the volume progress bar of the Bluetooth call on the mobile phone will change accordingly. However, the actual call volume still needs to be set on the board.

void rk_bt_hfp_enable_cvsd(void)

hfp codec is forced to use CVSD (8K sampling rate), AG (mobile phone) and HF (headphone) will no longer negotiate SCO codec type, at this time the SCO codec type must be forced to BT_SCO_CODEC_CVSD. This interface is only applicable to bsa (BSA only).

Bluez supports 8K and 16K sample rate adaptation. SCO codec type is negotiated and determined by AG (mobile phone) and HF (headphone). It does not support forcing to use of CVSD.

void rk_bt_hfp_disable_cvsd(void)

It is forbidden to force the use of CVSD (8K sampling rate) by hfp codec. The type of SCO codec is determined through negotiation between AG (mobile phone) and HF (headphone). The result of the negotiation is notified to the application layer through the callback event RK_BT_HFP_BCS_EVT. This interface is only applicable to bsa (BSA only).

int rk_bt_hfp_disconnect(void)

Disconnect current connection

8. OBEX Interface Introduction (RkBtObex.h BLUEZ only)​

RK_BT_OBEX_STATE introduction

1 typedef enum { 2 RK BT OBEX CONNECT FAILED, //connection failed _ _ _ _ 3 RK BT OBEX CONNECTED, //connection succeeded _ _ _ 4 RK BT OBEX DISCONNECT FAILED, //disconnection failed _ _ _ _ 5 RK BT OBEX DISCONNECTED, //disconnection succeeded _ _ _ 6 RK BT OBEX TRANSFER ACTIVE, //start transferring _ _ _ _ 7 RK BT OBEX TRANSFER COMPLETE, //complete transfer _ _ _ _ 8 } RK BT OBEX STATE; _ _ _
1 2 3 4 5 6 7 8typedef enum { RK BT OBEX CONNECT FAILED, //connection failed _ _ _ _ RK BT OBEX CONNECTED, //connection succeeded _ _ _ RK BT OBEX DISCONNECT FAILED, //disconnection failed _ _ _ _ RK BT OBEX DISCONNECTED, //disconnection succeeded _ _ _ RK BT OBEX TRANSFER ACTIVE, //start transferring _ _ _ _ RK BT OBEX TRANSFER COMPLETE, //complete transfer _ _ _ _ } RK BT OBEX STATE; _ _ _
typedef void (*RK BT OBEX STATE CALLBACK)(const char *bd addr, RK BT OBEX STATE _ _ _ _ _ _ _ _
state);

obex status callback, bd_addr: address of the connected remote device

void rk_bt_obex_register_status_cb(RK_BT_OBEX_STATE_CALLBACK cb)

Register obex status callback

int rk_bt_obex_init(char *path)

Start obexd process, only needs to call this interface to realize Bluetooth file transfer function, path: file storage path

int rk_bt_obex_deinit()

Close the obexd process and use it with rk_bt_obex_init

int rk_bt_obex_pbap_init()

To initialize the Bluetooth phone book, you must call rk_bt_obex_init to start obexd before calling this interface

int rk_bt_obex_pbap_deinit()

To de-initialize the Bluetooth phone book, after calling this interface, you must call rk_bt_obex_deinit to close obexd

int rk_bt_obex_pbap_connect(char *btaddr)

Open the pbap service, and connect with the device specified by btaddr actively.

int rk_bt_obex_pbap_get_vcf(char *dir_name, char *dir_file)

Obtain information about the object type specified by dir_name and store it in the file specified by dir_file

pbab defines six object types:

"pb": contact phone book

"ich": call history

"och": dial history

"mch": history of missed calls

"cch": combined history records, that is, all calls, outgoing and missed records

"spd": speed dial, for example, you can specify button 1 as a contact's speed dial button

"fav": favorites

int rk_bt_obex_pbap_disconnect(char *btaddr)

Disconnect with device specified by btaddr actively

9. Demo Program Introduction​

The sample program is stored in: external/deviceio /test. The bluetooth-related test cases are implemented in bt_test.cpp, which cover all the above interfaces. The function call is in DeviceIOTest.cpp.

9.1 Build​

  1. Execute make deviceio-dirclean && make deviceio -j4 in the SDK root directory, and the following log will be displayed when building is successful (note: only part of log is showed below, rk- xxxx corresponds to the specific project root directory)
-- Installing: /home/rk-
xxxx/buildroot/output/target/usr/lib/librkmediaplayer.so
-- Installing: /home/rk-
xxxx/buildroot/output/target/usr/lib/libDeviceIo.so
-- Installing: /home/rk-
xxxx/buildroot/output/target/usr/include/DeviceIo/Rk_battery.h
-- Installing: /home/rk-
xxxx/buildroot/output/target/usr/include/DeviceIo/RK_timer.h
-- Installing: /home/rk-
xxxx/buildroot/output/target/usr/include/DeviceIo/Rk_wake_lock.h
-- Installing: /home/rk-xxxx/buildroot/output/target/usr/bin/deviceio_test
  1. Run ./build.sh to generate new firmware, and then flash the new firmware to device.

9.2 Basic Interface Demo Program​

9.2.1 Interface Introduction​

9.2.1.1 Basic Interface Test Introduction to Bluetooth Service

void bt_test_bluetooth_init(void *data)

To initialize Bluetooth test. This interface is called before execute Bluetooth test. To register BLE receiving and data request callback functions, please refer to bt_server_open in the DeviceIOTest.cpp test menu.

Note: BLE reading data is achieved by registering callback functions. When BLE connection receives data, it will call the receiving callback function actively. For details, please refer to introduction of RkBtContent structure and rk_ble_register_recv_callback function.

void bt_test_bluetooth_deinit(char *data)

Bluetooth de-initialization test, de-initialize all Bluetooth profiles.

bt_test_set_class(void *data)

Set the type of Bluetooth device. The current test value is 0x240404.

bt_test_enable_reconnect(void *data)

Enable A2DP SINK and HFP auto reconnect function. It is recommended to call immediately after

bt_test_bluetooth_init .

bt_test_disable_reconnect(void *data)

Disable the A2DP SINK and HFP auto-reconnect function. It is recommended to call immediately after

bt_test_bluetooth_init .

On the phone side:

void bt_test_get_device_name(char *data)

Get local device name

void bt_test_get_device_addr(char *data)

Get local device address

void bt_test_set_device_name(char *data)

Set local device name

void bt_test_pair_by_addr(char *data)

Pair with the device at the specified address, data: " 94:87:E0:B6:6D:AE "

void bt_test_unpair_by_addr(char *data)

Unpair with the device at the specified address, data: " 94:87:E0:B6:6D:AE "

void bt_test_get_paired_devices(char *data)

Get a list of currently paired devices

void bt_test_free_paired_devices(char *data)

Release the memory requested in bt_test_get_paired_devices to store paired device information

void bt_test_get_scaned_devices(char *data)

Get a list of scanning devices

void bt_test_start_discovery(char *data)

Scan surrounding devices, including BR/EDR and LE devices

void bt_test_start_discovery_bredr(char *data) Scan the surrounding BR/EDR devices

void bt_test_start_discovery_le(char *data)

Scan the surrounding LE devices

void bt_test_cancel_discovery(char *data)

Cancel scan operation

void bt_test_is_discovering(char *data)

Whether is scanning the surrounding devices

void bt_test_display_devices(char *data)

Print the scanned information of surrounding devices

void bt_test_display_paired_devices(char *data)

Print the currently paired device information

9.2.1.2 BLE Interface Testing Introduction

  1. Install a third-party BLE test APK on your phone, such as nrfconnnect. 2. Choose the bt_test_ble_start function. 3. Scans Bluetooth and connects to "ROCKCHIP_AUDIO BLE" on the phone. 4. After the connection is successful, the device will call back the ble_status_callback_test function in bt_test.cpp and print "+++++ RK_BLE_STATE_CONNECT +++++". 5. Execute the following functions to do specific functional tests.

void bt_test_ble_start(void *data)

To enable BLE. After the device is connected passively, it will receive "Hello RockChip" and responds with "My name is rockchip".

void bt_test_ble_write(void *data)

Test BLE write function and send 134 strings with '0'-'9'.

void bt_test_ble_get_status(void *data)

Test BLE status interface.

void bt_test_ble_stop(void *data)

Disabled BLE.

void bt_test_ble_disconnect(char *data)

Disconnect.

9.2.1.3 BLE CLIENT Interface Test Introduction

  1. Select bt_test_sink_open function, start ble client 2. Select bt_test_start_discovery or bt_test_start_discovery_le to start scanning the device 3. Enter "60 input xx:xx:xx:xx:xx:xx" and call bt_test_ble_client_connect to connect to the ble server device at the specified address 4. After the connection is successful, the callback ble_client_test_state_callback will be triggered, printing "+++++ RK_BLE_CLIENT_STATE_IDLE +++++" 5. Enter "61 input xx:xx:xx:xx:xx:xx", call bt_test_ble_client_disconnect to disconnect the ble server device at the specified address, and successfully disconnect, will print "+++++ RK_BLE_CLIENT_STATE_DISCONNECT ++++ +" 6. Enter "63 input xx:xx:xx:xx:xx:xx" and call bt_test_ble_client_get_service_info to get the service uuid, characteristic uuid, permission, properties, descriptor uuid and other information of the connected device 7. Enter "64 input uuid", such as "56 input 00009999-0000-1000-8000-00805F9B34FB" to read the data of 9999 uuid through bt_test_ble_client_read. Successful reading will trigger bt_test_ble_client_recv_data_callback to print the read value 8. Enter "65 input uuid", such as "57 input 00009999-0000-1000-8000-00805F9B34FB" and write 9999 uuid via bt_test_ble_client_write 9. Select 59, 68 to turn on or off the notification of the specified uuid

9.2.1.4 A2DP SINK Interface Test Introduction

  1. Select the bt_test_sink_open function. 2. Use the mobile phone Bluetooth to scan and connect to "ROCKCHIP_AUDIO". 3. After the connection is successful, the device will call back the bt_sink_callback function in bt_test.cpp and print "++++++++++++ BT SINK EVENT: connect success ++++++++++". 4. Turn on music player of the phone, and make sure it is ready to play songs. 5. Execute the following functions to test specific functions:

void bt_test_sink_open(void *data)

Turn on A2DP Sink mode.

void bt_test_sink_visibility00(void *data)

Set A2DP Sink to be invisible and unreachable.

void bt_test_sink_visibility01(void *data)

Set the A2DP Sink to be invisible and connectable.

void bt_test_sink_visibility10(void *data)

Set the A2DP Sink to be visible and disconnectable.

void bt_test_sink_visibility11(void *data)

Set A2DP Sink visible and connectable.

void bt_test_sink_music_play(void *data)

Control the device to play in reverse.

void bt_test_sink_music_pause(void *data)

Control the device to pause in reverse.

void bt_test_sink_music_next(void *data)

Control the device to play the next song in reverse

void bt_test_sink_music_previous(void *data)

Control the device to play the previous song in reverse.

void bt_test_sink_music_stop(void *data)

Control the device to stop playing in reverse.

void bt_test_sink_reconnect_enable(void *data)

Enable A2DP Sink auto-connect function.

void bt_test_sink_reconnect_disenable(void *data)

Disable the A2DP Sink auto-connect function.

void bt_test_sink_disconnect(void *data) Disconnected A2DP Sink。

void bt_test_sink_close(void *data)

Close A2DP Sink service。

void bt_test_sink_status(void *data)

Query A2DP Sink connection status.

void bt_test_sink_set_volume(char *data)

Set volume test

void bt_test_sink_connect_by_addr(char *data)

Connect to the device with the specified address, data: " 94:87:E0:B6:6D:AE "

void bt_test_sink_disconnect_by_addr(char *data)

Disconnect the device with the specified address, data: " 94:87:E0:B6:6D:AE "

void bt_test_sink_get_play_status(char *data)

Get the playback status, it will trigger the "play position change" callback

void bt_test_sink_get_poschange(char *data)

Whether the currently connected device supports reporting of playback progress

9.2.1.5 A2DP SOURCE Interface Test Introduction

  1. Select bt_test_source_open function, start source function 2. Select bt_test_start_discovery or bt_test_start_discovery_bredr to start scanning the surrounding Bluetooth devices 3. Select bt_test_source_connect_by_addr to connect to the addr specified Bluetooth device (27 input xx:xx:xx:xx:xx:xx). After the connection is successful, the device will call back the bt_test_source_status_callback function in bt_test.cpp and print "+++++++++" +++ BT
SOURCE EVENT: connect sucess ++++++++++".
  1. At this time, music will be broadcast from the connected A2DP Sink device. 5. Execute the following functions to do detailed functional tests.

void bt_test_source_open(char *data)

Open source function

void bt_test_source_close(char *data)

Close source function

void bt_test_source_connect_status(char *data)

Get A2DP Source connection status.

void bt_test_source_connect_by_addr(char *data)

Connect to the device with specified addr.

bt_test_source_disconnect

Disconnect.

bt_test_source_disconnect_by_addr

Disconnect the device specified by addr 9.2.1.6 SPP Interface Testing Introduction

  1. Install a third-party SPP test APK on the phone, such as "Serial Bluetooth Terminal". 2. Select the bt_test_spp_open function. 3. Scan Bluetooth and connects to "ROCKCHIP_AUDIO" on the phone. 4. Open the third-party SPP test APK and connect the device by SPP. After the device is connected successfully, the device will call back the _btspp_status_callback function in bt_test.cpp and print "+++++++ RK_BT_SPP_EVENT_CONNECT +++++". 5. Execute the following functions for detailed functional tests.

void bt_test_spp_open(void *data)

Open SPP

void bt_test_spp_write(void *data)

Test SPP writing function, send “This is a message from rockchip board!” string to the other side

void bt_test_spp_close(void *data)

Close SPP

void bt_test_spp_status(void *data)

Query SPP connection status

9.2.1.7 HFP Interface Test Introduction

  1. Select bt_test_hfp_sink_open or bt_test_hfp_hp_open function. 2. Scans Bluetooth and connects to "ROCKCHIP_AUDIO" on the mobile phone. Note: If you have already connected the mobile phone before testing SINK function, you should ignore the device at the mobile phone, then scan and connect again. 3. After the device is successfully connected, the device will call back the bt_test_hfp_hp_cb function in bt_test.cpp and print "+++++ BT HFP HP CONNECT +++++". If the phone is called, it will print "+++++ BT HFP HP RING +++++" , and "+++++ BT HFP AUDIO OPEN +++++" when the phone is connected. For other status printing, please read the source code of bt_test_hfp_hp_cb function in bt_test.cpp directly. Note: If the bt_test_hfp_sink_open interface is called, when the device is successfully connected, the connection status of A2DP SINK will also be printed, such as "++++++++++++ BT SINK EVENT: connect success +++++++ +++ ". 4. Execute the following functions for detailed functional tests.

bt_test_hfp_sink_open

Open HFP HF and A2DP SINK in coexist mode.

bt_test_hfp_hp_open

Open HFP HF function only.

bt_test_hfp_hp_accept

Answer the phone actively.

bt_test_hfp_hp_hungup

Hang up actively。

bt_test_hfp_hp_redail

To re-dial.

void bt_test_hfp_hp_dial_number(char *data)

Dial the specified phone number bt_test_hfp_hp_report_battery

Battery power status is reported per second from 0 to 9, At this time, you will see the icon change from empty to full on the phone. Note: Some phones do not support Bluetooth power icon display.

bt_test_hfp_hp_set_volume

Set the Bluetooth call volume per second from 1 to 15, . At this time, you will see the Bluetooth call volume progress bar change process on the mobile phone.

Note: Some mobile phones do not support display the progress bar change dynamically. Actively increasing or decreasing volume to trigger progress bar display. At this time, you will see that the device has set the volume of mobile phone successfully . For example, if the original volume is 0. After running the interface, press the mobile phone volume '+' button and you will find that the volume is full.

bt_test_hfp_hp_close

Close HFP service.

bt_test_hfp_open_audio_diplex

Open the hfp audio channel, which is called in the callback event RK_BT_HFP_AUDIO_OPEN_EVT.

bt_test_hfp_close_audio_diplex

Close the hfp audio channel and which is called in the callback event RK_BT_HFP_AUDIO_CLOSE_EVT.

9.2.1.8 OBEX Interface Test Introduction

Execute the following functions for detailed functional tests:

bt_test_obex_init

Open obexd process and execute this function to test the file transfer

bt_test_obex_deinit

Close the obexd process

bt_test_obex_pbap_init

Execute bt_test_obex_init fore test the Bluetooth phone book

bt_test_obex_pbap_deinit

Deinitialize the Bluetooth phone book, and then execute bt_test_obex_deinit

bt_test_obex_pbap_connect

Open the pbap service and connect to the specified device

bt_test_obex_pbap_get_pb_vcf

Get the contact phone book, the result is stored in /data/pb.vcf

bt_test_obex_pbap_get_ich_vcf

Get call history, the results are stored in /data/ich.vcf

bt_test_obex_pbap_get_och_vcf

Get outgoing history record, the result is stored in /data/och.vcf

bt_test_obex_pbap_get_mch_vcf

Get the history of missed calls, the results are stored in /data/mch.vcf

bt_test_obex_pbap_disconnect

Turn off the pbap service, and disconnect bt_test_obex_close

Close obex service

9.2.2 Test Steps​

  1. Execute the test program command: DeviceIOTest bluetooth to display the following interface:
# deviceio_test bluetooth
version:V1.3.5
#### Please Input Your Test Command Index ####
01. bt_server_open
02. bt_test_set_class
03. bt_test_get_device_name
04. bt_test_get_device_addr
05. bt_test_set_device_name
06. bt_test_enable_reconnect
07. bt_test_disable_reconnect
08. bt_test_start_discovery
09. bt_test_start_discovery_le
10. bt_test_start_discovery_bredr
11. bt_test_cancel_discovery
12. bt_test_is_discovering
13. bt_test_display_devices
14. bt_test_read_remote_device_name
15. bt_test_get_scaned_devices
16. bt_test_display_paired_devices
17. bt_test_get_paired_devices
18. bt_test_free_paired_devices
19. bt_test_pair_by_addr
20. bt_test_unpair_by_addr
21. bt_test_get_connected_properties
22. bt_test_source_auto_start
23. bt_test_source_connect_status
24. bt_test_source_auto_stop
25. bt_test_source_open
26. bt_test_source_close
27. bt_test_source_connect_by_addr
28. bt_test_source_disconnect
29. bt_test_source_disconnect_by_addr
30. bt_test_source_remove_by_addr
31. bt_test_sink_open
32. bt_test_sink_visibility00
33. bt_test_sink_visibility01
34. bt_test_sink_visibility10
35. bt_test_sink_visibility11
36. bt_test_ble_visibility00
37. bt_test_ble_visibility11
38. bt_test_sink_status
39. bt_test_sink_music_play
40. bt_test_sink_music_pause
41. bt_test_sink_music_next
42. bt_test_sink_music_previous
43. bt_test_sink_music_stop
44. bt_test_sink_set_volume
45. bt_test_sink_connect_by_addr
46. bt_test_sink_disconnect_by_addr
47. bt_test_sink_get_play_status
48. bt_test_sink_get_poschange
49. bt_test_sink_disconnect
50. bt_test_sink_close
51. bt_test_ble_start
52. bt_test_ble_set_address
53. bt_test_ble_set_adv_interval
54. bt_test_ble_write
55. bt_test_ble_disconnect
56. bt_test_ble_stop
57. bt_test_ble_get_status
58. bt_test_ble_client_open
59. bt_test_ble_client_close
60. bt_test_ble_client_connect
61. bt_test_ble_client_disconnect
62. bt_test_ble_client_get_status
63. bt_test_ble_client_get_service_info
64. bt_test_ble_client_read
65. bt_test_ble_client_write
66. bt_test_ble_client_is_notify
67. bt_test_ble_client_notify_on
68. bt_test_ble_client_notify_off
69. bt_test_ble_client_get_eir_data
70. bt_test_spp_open
71. bt_test_spp_write
72. bt_test_spp_close
73. bt_test_spp_status
74. bt_test_hfp_sink_open
75. bt_test_hfp_hp_open
76. bt_test_hfp_hp_accept
77. bt_test_hfp_hp_hungup
78. bt_test_hfp_hp_redail
79. bt_test_hfp_hp_dial_number
80. bt_test_hfp_hp_report_battery
81. bt_test_hfp_hp_set_volume
82. bt_test_hfp_hp_close
83. bt_test_hfp_hp_disconnect
84. bt_test_obex_init
85. bt_test_obex_pbap_init
86. bt_test_obex_pbap_connect
87. bt_test_obex_pbap_get_pb_vcf
88. bt_test_obex_pbap_get_ich_vcf
89. bt_test_obex_pbap_get_och_vcf
90. bt_test_obex_pbap_get_mch_vcf
91. bt_test_obex_pbap_disconnect
92. bt_test_obex_pbap_deinit
93. bt_test_obex_deinit
94. bt_server_close
Which would you like:
  1. Select the corresponding test program number. Firstly, select 01 to initialize the Bluetooth basic service. Such as testing BT Source function。
1 Which would you like:01 2 #Note: enter the next round of selection interface until finish execution 3 Which would you like:25 4 #Note: open source function 5 Which would you like:8 input 15000 6 #Note: Start to scan the surrounding Bluetooth devices, scan time is 15s 7 Which would you like:27 input xx:xx:xx:xx:xx:xx 8 #Note: Start to connect with the device wtih the address xx:xx:xx:xx:xx:xx
1 2 3 4 5 6 7 8Which would you like:01 #Note: enter the next round of selection interface until finish execution Which would you like:25 #Note: open source function Which would you like:8 input 15000 #Note: Start to scan the surrounding Bluetooth devices, scan time is 15s Which would you like:27 input xx:xx:xx:xx:xx:xx #Note: Start to connect with the device wtih the address xx:xx:xx:xx:xx:xx
3.

parameters, such as pairing with the specified address device

Which would you like:19 input 94:87:E0:B6:6D:AE
#Note: start pairing with the device with the address of 94:87:E0:B6:6D:AE

9.3 BLE Network Configuration Demo Program​

Please refer to "Rockchip_Developer_Guide_Network_Config_CN".