Mwadime Makokha / Mbed 2 deprecated BlueToothSIM868

Dependencies:   mbed

Revision:
1:940070f92554
Child:
2:83d03d7148a5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BT.cpp	Thu Nov 14 14:02:32 2019 +0000
@@ -0,0 +1,83 @@
+#include "BT.h"
+Serial* BT::modemSerial =0;
+BT::BT(Serial &port){
+    modemSerial = &port;
+}
+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);
+    DEBUG_PRINTLN("%s",modem_response);
+    wait_ms(2000);
+    char* mac_address = get_phone_mac_addr();
+    return get_phone_id(mac_address);  
+}
+void BT::bt_pair_device(char id){
+    modemSerial->printf("AT+BTPAIR=0,%c\n\r",id);
+    get_response(1000,true);
+    DEBUG_PRINTLN("%s",modem_response);
+}
+char* BT::get_phone_mac_addr(){
+    return "74:e6:0f:b1:45:58";
+    }
+char BT::get_phone_id(char* addr){
+    char id=0;
+    bool id_found=false;
+    char* dev_addr;
+    int comma_count =0;
+    int arr_index =0;
+   while(modem_response[arr_index] != 0){
+    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){
+            DEBUG_PRINTLN("id = %c",id);
+            id_found = true;
+            break;
+            }
+        }
+    if(modem_response[arr_index]==',')comma_count++;
+    if(modem_response[arr_index]=='\n')comma_count=0;
+    arr_index++;
+    }
+    return id_found?id:'0';
+}
+
+void BT::send_bt_cmd(char cmd[]){
+    modemSerial->printf(cmd);
+    modemSerial->printf("\n\r"); 
+}
+void BT::get_response(uint16_t timeout,bool is_multiline){
+    int arr_index=0;
+    while(timeout){
+    if(arr_index >= 254) break;
+    while(modemSerial->readable()){
+    char c = modemSerial->getc();
+    if(c == '\r') continue;
+    if(c=='\n' && arr_index==0){
+    continue;
+    }
+    if(c=='\n' && !is_multiline){
+    break;    
+    }
+    modem_response[arr_index] = c;
+    arr_index++;
+    }
+    timeout--;
+    wait_ms(1);
+    }
+    modem_response[arr_index] = 0;    
+}
\ No newline at end of file