bluetooth handfree client test on console

来源:互联网 发布:seo搜索引擎优化 遵义 编辑:程序博客网 时间:2024/06/10 11:29

标签: bluetoothhandfree client and
 456人阅读 评论(2) 收藏 举报
 分类:
 

目录(?)[+]

1 测试环境

  • 硬件环境: wmt8880行车记录仪+mtk6622 bt + iphone手机
  • 软件环境: android4.4.2+ bluedroid5.0 + kernel3.4.5
  • 系统组件的连接框图:

2 测试代码编码

  • step1:通过hw_get_module接口来获取蓝牙HAL层提供的蓝牙接口:bt_interface,并调用他的初始化函数:bt_interface->init(callbacks) 来设置对应的应用层回调函数:callbacks
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bool hal_open(bt_callbacks_t *callbacks) {  
  2.   hw_module_t *module;  
  3.   if (hw_get_module(BT_STACK_MODULE_ID, (hw_module_t const **)&module)) {  
  4.     return false;  
  5.   }  
  6.   
  7.   hw_device_t *device;  
  8.   if (module->methods->open(module, BT_STACK_MODULE_ID, &device)) {  
  9.     return false;  
  10.   }  
  11.   
  12.   bt_device = (bluetooth_device_t *)device;  
  13.   bt_interface = bt_device->get_bluetooth_interface();  
  14.   e3Cspan class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if (!bt_interface) {  
  15.     bt_device->common.close((hw_device_t *)&bt_device->common);  
  16.     bt_device = NULL;  
  17.     return false;  
  18.   }  
  19.   
  20.   bool success = (bt_interface->init(callbacks) == BT_STATUS_SUCCESS);  
  21.   success = success && (bt_interface->set_os_callouts(&callouts) == BT_STATUS_SUCCESS);  
  22.   return success;  

step2:使能蓝牙:bt_interface->enable()
step3:修改蓝牙的名字
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bt_property_t *property_new_name(const char *name) {  
  2.   bt_bdname_t *bdname = calloc(sizeof(bt_bdname_t), 1);  
  3.   bt_property_t *property = calloc(sizeof(bt_property_t), 1);  
  4.   
  5.   property->type = BT_PROPERTY_BDNAME;  
  6.   property->val = bdname;  
  7.   property->len = sizeof(bt_bdname_t);  
  8.   
  9.   strlcpy((char *)bdname->name, name, sizeof(bdname->name));  
  10.   
  11.   return property;  
  12. }  
  13. bool modify_adapter_name() {  
  14.   int error;  
  15.   bt_property_t *name = property_new_name("handfree_sink");  
  16.   
  17.   CALL_AND_WAIT(bt_interface->set_adapter_property(name), adapter_properties);  
  18.   CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);  
  19.   TASSERT(error == BT_STATUS_SUCCESS, "Error getting device name.");  
  20.   TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());  
  21.   TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");  
  22.   TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_extract_name(adapter_get_property(BT_PROPERTY_BDNAME)));  
  23.   property_free(name);  
  24.   return true;  
  25. }  

step4: 是蓝牙adapter对其他设备可见:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int  discoverable()  
  2. {  
  3.     int ret;  
  4.      bt_property_t prop;  
  5.      int val = 120;  
  6.      prop.type = (bt_property_type_t) BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;  
  7.         prop.len = sizeof(int);  
  8.         prop.val = &val;  
  9.     ret = bt_interface->set_adapter_property(&prop);  
  10.     printf("set discoverable,ret:%d\n",ret);  
  11.     return ret;  
  12. }  

step5: 设定蓝牙的scan模式
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int  set_scan_mode(int scan_mode)  
  2. {  
  3.     int ret;  
  4.      bt_property_t prop;  
  5.      int val = scan_mode;//BT_SCAN_MODE_CONNECTABLE;  
  6.      prop.type = (bt_property_type_t) BT_PROPERTY_ADAPTER_SCAN_MODE;  
  7.         prop.len = sizeof(int);  
  8.         prop.val = &val;  
  9.     ret = bt_interface->set_adapter_property(&prop);  
  10.     printf("set scan mod,ret:%d\n",ret);  
  11.     return ret;  
  12. }  

step6: 跟远端设备进行配对
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int request_pair()  
  2. {  
  3.     int ret;  
  4.     ret = bt_interface->create_bond(&bt_remote_bdaddr,0);  
  5.     printf("enter %s, ret:%d\n",__func__,ret);  
  6.     return ret;  
  7. }  
  8. void  ssp_request(bt_bdaddr_t *remote_bd_addr,  
  9.                                         bt_bdname_t *bd_name,  
  10.                                         uint32_t cod,  
  11.                                         bt_ssp_variant_t pairing_variant,  
  12.                                      uint32_t pass_key)  
  13.   {  
  14.         printf("enter %s,bd_name:%s,cod:%d,pairing_variant:%d,pass_key:%d\n",__func__,bd_name->name,cod,pairing_variant,pass_key);  
  15.     bt_interface->ssp_reply(remote_bd_addr,pairing_variant,true,pass_key);  
  16.         CALLBACK_RET();  
  17.     //return 0;  
  18.   }  


配对的时序图如下:

  • step7: 获取handfree client对应的接口:handfree_interface,并且调用它的init函数设置对应的回调函数:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bool handfree_init() {  
  2.   handfree_interface = (bthf_client_interface_t *)bt_interface->get_profile_interface(BT_PROFILE_HANDSFREE_CLIENT_ID);  
  3.   return handfree_interface->init(callbacks_get_bthf_struct()) == BT_STATUS_SUCCESS;  
  4. }  
  • step8:跟远端建立rfcomm链路,然后再建立sco链路,前者用于传输at指令,后者用于传输语音数据,如果使用pcm接口传输语音数据,也需要建立sco链路
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bool handfree_connect() {  
  2.   int error;  
  3.   
  4.   // PAN is enabled by default, wait for the first control state change  
  5.   // with default parameters set. We don't want to verify the result since  
  6.   // the implementation could have set any parameters.  
  7.  // WAIT(pan_control_state_changed);  
  8.   
  9.   // Call enable explicitly and verify that the parameters match what we just set.  
  10.   CALL_AND_WAIT(error = handfree_interface->connect(&bt_remote_bdaddr), bthf_client_connection_state);  
  11.   TASSERT(error == BT_STATUS_SUCCESS, "Error handfree connect: %d", error);  
  12.   //TASSERT(pan_get_control_state() == BTPAN_STATE_ENABLED, "Control state is disabled.");  
  13.   //TASSERT(pan_get_local_role() == local_role, "Unexpected local role: %d", pan_get_local_role());  
  14.   //TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error in control callback: %d", pan_get_error());  
  15.   
  16.   return true;  
  17. }  
  • step9: 开始拨打电话:10086:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bt_status_t dialNum(const char * number)  
  2. {  
  3.     int ret;  
  4.     ret = handfree_interface->dial(number);  
  5.     printf("dial,ret:%d\n",ret);  
  6.     return ret;  
  7. }  
  • step10: 建立sco音频通道:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bool handfree_connect_audio() {  
  2.   int error;  
  3.   
  4.   CALL_AND_WAIT(error = handfree_interface->connect_audio(&bt_remote_bdaddr), bthf_client_audio_state);  
  5.   TASSERT(error == BT_STATUS_SUCCESS, "Error handfree connect audio: %d", error);  
  6.   printf("error = %d\n",error);  
  7.   //TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error connecting to BT device: %d", pan_get_error());  
  8.   //TASSERT(pan_get_connection_state() == BTPAN_STATE_CONNECTING, "Invalid PAN state after connect: %d", pan_get_connection_state());  
  9.   //TASSERT(pan_get_local_role() == local_role, "Incorrect local role: %d", pan_get_local_role());  
  10.   //TASSERT(pan_get_remote_role() == remote_role, "Incorrect remote role: %d", pan_get_remote_role());  
  11.   
  12. //  WAIT(pan_connection_state_changed);  
  13.  // TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error connecting to BT device: %d", pan_get_error());  
  14.  // TASSERT(pan_get_connection_state() == BTPAN_STATE_CONNECTED, "Invalid PAN state after connect: %d", pan_get_connection_state());  
  15.  // TASSERT(pan_get_local_role() == local_role, "Incorrect local role: %d", pan_get_local_role());  
  16.  // TASSERT(pan_get_remote_role() == remote_role, "Incorrect remote role: %d", pan_get_remote_role());  
  17.   
  18.   return true;  
  19. }  

完整的测试代码如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int main(int argc, char **argv) {  
  2.   if (argc < 2 || !parse_bdaddr(argv[1], &bt_remote_bdaddr)) {  
  3.     printf("Usage: %s <bdaddr>\n", argv[0]);  
  4.     return -1;  
  5.   }  
  6.   
  7.   if (!hal_open(callbacks_get_adapter_struct())) {  
  8.     printf("Unable to open Bluetooth HAL.\n");  
  9.     return 1;  
  10.   }  
  11.   printf("begin callbacks_init\n");  
  12.    callbacks_init();  
  13.    //enable bt  
  14.    printf("begin enable bt...\n");  
  15.    CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);  
  16.    //set bt device name  
  17.    printf("modify bt name to :handfree test\n");  
  18.    modify_adapter_name();  
  19.    //set bt discovable  
  20.    printf("set bt device discoverable\n");  
  21.   //CALL_AND_WAIT(discoverable(), discovery_state_changed);  
  22.   CALL_AND_WAIT(discoverable(), adapter_properties);  
  23.   //set scan mode  
  24.   printf("set bt device scan mode\n");  
  25.   CALL_AND_WAIT(set_scan_mode(BT_SCAN_MODE_CONNECTABLE), adapter_properties);  
  26.   // paire with remote device  
  27.   printf("begin to pair with iphone\n");  
  28.   //request_pair();  
  29.   CALL_AND_WAIT(request_pair(), ssp_request);  
  30.   
  31.    WAIT(bond_state_chang);  
  32.    WAIT(bond_state_chang);  
  33.   
  34.    //test handfree client profile  
  35.    printf("begint to handfree client profile test...\n");  
  36.    handfree_init();  
  37.    printf("begin to rfcomm connect\n");  
  38.    handfree_connect();  
  39.    WAIT(bthf_client_connection_state);  
  40.   
  41.   // dialNum("10086");  
  42.   //CALL_AND_WAIT(dialNum("10086"),cmd_complete_cb);  
  43.   CALL_AND_WAIT(dialNum("13424419597"),cmd_complete_cb);  
  44.   
  45.    audio_config();  
  46.      
  47.    printf("begin to audio connect\n");  
  48.    handfree_connect_audio();  
  49.    WAIT(bthf_client_audio_state);  
  50.    WAIT(bthf_client_audio_state);  
  51.    WAIT(bthf_client_audio_state);  
  52.    WAIT(bthf_client_audio_state);  
  53.      
  54.   callbacks_cleanup();  
  55. }  
编译输出:bdtest
以上代码是基于android5.0中的bluedroid中的test/suite中的代码框架进行的扩展。

3 控制台的测试步骤

前提条件:需要wmt8880支持pcm驱动,使用支持handfree client和a2dp sink的bluedroid,uart driver可以正常工作。
  1. bdtest 1c:1a:c0:39:e8:d2 &(iphone手机的蓝牙mac地址)
  2. tinycap bt.wav -d 1 -c 1 -r 8000 (录下行downlink 语音)
  3. tinyplay bt.wav -d 1 -r 8000(播放上行uplink语音)
0 0
原创粉丝点击