-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBluetooth.cpp
90 lines (67 loc) · 1.62 KB
/
Bluetooth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/************************************************
Written by Cruz Monrreal II
Created on 06-26-2012
Modified on 11-11-2012
Updates can be found here:
https://github.com/cmonr/Arduino-Bluetooth-Library
************************************************/
#include <stdint.h>
#include <Arduino.h>
#include <Bluetooth.h>
Bluetooth::Bluetooth(){
Serial1.begin(115200);
}
Bluetooth::~Bluetooth(){}
char Bluetooth::beginCMD(){
Serial1.print("$$$");
return readUntilLine() == "CMD";
}
void Bluetooth::endCMD(){
Serial1.println("---");
readUntilStr("END", 5000);
}
void Bluetooth::connect(){
do{
Serial1.println("C");
} while(readUntilStr("failed", 10000).indexOf("failed") != -1);
}
char Bluetooth::connectToAddr(String addr){
}
char Bluetooth::isConnected(){
Serial1.println("GK");
return readUntilDelay(1000) != "0";
}
String Bluetooth::readUntilStr(String str, unsigned long timeout){
String resp = "";
char c = 0;
start = millis();
Serial.flush();
while (resp.indexOf(str) == -1 && start + timeout > millis()){
while(Serial1.available()){
c = char(Serial1.read());
resp.concat(c);
}
}
return resp;
}
String Bluetooth::readUntilLine(){
String resp = "";
char c = 0;
while (c != '\n'){
while(Serial1.available()){
c = char(Serial1.read());
resp.concat(c);
}
}
return resp;
}
String Bluetooth::readUntilDelay(unsigned long delay){
String resp = "";
start = millis();
Serial.flush();
while(start + delay > millis()){
while(Serial1.available())
resp.concat(char(Serial1.read()));
}
return resp;
}