Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: BT.cpp
- Revision:
- 2:83d03d7148a5
- Parent:
- 1:940070f92554
- Child:
- 3:936c6d909891
--- a/BT.cpp Thu Nov 14 14:02:32 2019 +0000 +++ b/BT.cpp Thu Nov 14 22:38:08 2019 +0000 @@ -2,31 +2,53 @@ Serial* BT::modemSerial =0; BT::BT(Serial &port){ modemSerial = &port; + send_bt_cmd("ATE0"); + get_response(1000,false); } void BT::get_bt_status(){ send_bt_cmd("AT+BTSTATUS?"); get_response(1000,true); DEBUG_PRINTLN("%s",modem_response); - } +} + void BT::power_bt_device(bool on){ if(on)send_bt_cmd("AT+BTPOWER=1"); else send_bt_cmd("AT+BTPOWER=0"); get_response(1000,false); DEBUG_PRINTLN("%s",modem_response); } -char BT::bt_scan(){ - send_bt_cmd("AT+BTSCAN=1,10"); - get_response(10000,true); + +char BT::bt_scan(uint32_t timeout){ + char id; + if(timeout>60000){ + int cmd_rounds = timeout/10000; + for(int k=0;k<cmd_rounds;k++){ + send_bt_cmd("AT+BTSCAN=1,10"); + get_response(10000,true); DEBUG_PRINTLN("%s",modem_response); - wait_ms(2000); char* mac_address = get_phone_mac_addr(); - return get_phone_id(mac_address); + id = get_phone_id(mac_address); + if(id != '0') break ; + } + } + return id; } + void BT::bt_pair_device(char id){ + DEBUG_PRINTLN("iD returned"); modemSerial->printf("AT+BTPAIR=0,%c\n\r",id); - get_response(1000,true); + get_response(5000,true); DEBUG_PRINTLN("%s",modem_response); + char* passkey = get_phone_passkey(); + if(passkey != "error"){ + send_bt_cmd("AT+BTPAIR=1,1"); + get_response(3000,true); + } + else{ + DEBUG_PRINTLN("Error"); + } } + char* BT::get_phone_mac_addr(){ return "74:e6:0f:b1:45:58"; } @@ -40,7 +62,6 @@ if(comma_count==1)id = modem_response[arr_index++]; if(comma_count==3){ for(int k=0;k<17;k++){ - //pcSerial.printf("%c",modem_response[arr_index+k]); dev_addr[k] = modem_response[arr_index+k]; } if(strcmp(dev_addr,addr) == 0){ @@ -56,11 +77,71 @@ return id_found?id:'0'; } -void BT::send_bt_cmd(char cmd[]){ +char* BT::get_phone_passkey(){ + int arr_index = 0; + char* passkey=new char[7]; + bool passkey_found=false; + int comma_count = 0; + while(modem_response[arr_index] !=0){ + if(comma_count == 2){ + int i =0; + while(modem_response[arr_index+i] != 0){ + passkey[i] = modem_response[arr_index+i]; + i++; + } + passkey[i] = 0; + passkey_found = true; + break; + } + if(modem_response[arr_index]==',')comma_count++; + arr_index++; + } + if(passkey_found){ + return passkey; + } + DEBUG_PRINTLN("Error"); + return "error"; +} + +void BT::bt_unpair_device(){ + send_bt_cmd("AT+BTUNPAIR=0"); + get_response(1000,false); +} + +void BT::bt_connect_device(char id,char profile_id){ + DEBUG_PRINTLN("%c",id); + DEBUG_PRINTLN("%c",profile_id); + // modemSerial->printf("AT+BTCONNECT=%c,%c\n\r",id,profile_id); + send_bt_cmd("AT+BTCONNECT=1,10"); + get_response(3000,true); + DEBUG_PRINTLN("%s",modem_response); + } + +char BT::bt_get_profile_id(char id){ + char profile_id=0; + modemSerial->printf("AT+BTGETPROF=%c\n\r",id); + get_response(2000,true); + int arr_index=0; + DEBUG_PRINTLN("%s",modem_response); + while(modem_response[arr_index]!='\n'){ + if(modem_response[arr_index] == ':'){ + if(modem_response[arr_index+1] == ' '){ + profile_id = modem_response[arr_index+2]; + break; + } + profile_id = modem_response[arr_index+1]; + break; + } + arr_index++; + } + return profile_id; +} + +void BT::send_bt_cmd(char* cmd){ modemSerial->printf(cmd); modemSerial->printf("\n\r"); } -void BT::get_response(uint16_t timeout,bool is_multiline){ +void BT::get_response(uint32_t timeout,bool is_multiline){ int arr_index=0; while(timeout){ if(arr_index >= 254) break;