- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHC05.java
More file actions
Latest commit
165 lines (141 loc) · 5.66 KB
/
Copy pathHC05.java
File metadata and controls
165 lines (141 loc) · 5.66 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
importjavax.swing.*;
importjava.awt.*;
importjava.io.*;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.bluetooth.DeviceClass;
importjavax.bluetooth.DiscoveryAgent;
importjavax.bluetooth.DiscoveryListener;
importjavax.bluetooth.LocalDevice;
importjavax.bluetooth.RemoteDevice;
importjavax.bluetooth.ServiceRecord;
importjavax.bluetooth.UUID;
importjavax.microedition.io.Connector;
importjavax.microedition.io.StreamConnection;
publicclassHC05 {
booleanscanFinished = false;
RemoteDevicehc05device;
Stringhc05Url;
publicstaticvoidmain(String[] args) {
try {
newHC05().go();
} catch (Exceptionex) {
Logger.getLogger(HC05.class.getName()).log(Level.SEVERE, null, ex);
}
}
privatevoidgo() throwsException {
//scan for all devices:
scanFinished = false;
LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, newDiscoveryListener() {
@Override
publicvoiddeviceDiscovered(RemoteDevicebtDevice, DeviceClasscod) {
try {
Stringname = btDevice.getFriendlyName(false);
System.out.format("%s (%s)\n", name, btDevice.getBluetoothAddress());
if (name.matches("RNBT-1884")) {
hc05device = btDevice;
System.out.println("got it!");
}
} catch (IOExceptione) {
e.printStackTrace();
}
}
@Override
publicvoidinquiryCompleted(intdiscType) {
scanFinished = true;
}
@Override
publicvoidserviceSearchCompleted(inttransID, intrespCode) {
}
@Override
publicvoidservicesDiscovered(inttransID, ServiceRecord[] servRecord) {
}
});
while (!scanFinished) {
//this is easier to understand (for me) as the thread stuff examples from bluecove
Thread.sleep(500);
}
//search for services:
UUIDuuid = newUUID(0x1101); //scan for btspp://... services (as HC-05 offers it)
UUID[] searchUuidSet = newUUID[]{uuid};
int[] attrIDs = newint[]{
0x0100// service name
};
scanFinished = false;
LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet,
hc05device, newDiscoveryListener() {
@Override
publicvoiddeviceDiscovered(RemoteDevicebtDevice, DeviceClasscod) {
}
@Override
publicvoidinquiryCompleted(intdiscType) {
}
@Override
publicvoidserviceSearchCompleted(inttransID, intrespCode) {
scanFinished = true;
}
@Override
publicvoidservicesDiscovered(inttransID, ServiceRecord[] servRecord) {
for (inti = 0; i < servRecord.length; i++) {
hc05Url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (hc05Url != null) {
break; //take the first one
}
}
}
});
while (!scanFinished) {
Thread.sleep(500);
}
System.out.println(hc05device.getBluetoothAddress());
System.out.println(hc05Url);
//if you know your hc05Url this is all you need:
StreamConnectionstreamConnection = (StreamConnection) Connector.open(hc05Url);
OutputStreamos = streamConnection.openOutputStream();
InputStreamis = streamConnection.openInputStream();
inti;
StringBuildersb = newStringBuilder();
Filefile = newFile("test1.csv");
FileWriterfileWriter = newFileWriter(file);
Stringdelims = "[,]+";
os.write("1".getBytes()); //just send '1' to the device
while((i = is.read())!=-1) {
if(i != 32){
sb.append((char)i);
}
else{
System.out.println(sb);
try {
Stringsb1 = sb.toString();
String[] tokens = sb1.split(delims);
fileWriter.append(tokens[0]); //t
fileWriter.append(',');
fileWriter.append(tokens[1]); //x
fileWriter.append(',');
fileWriter.append(tokens[2]); //y
fileWriter.append(',');
fileWriter.append(tokens[3]); //z
fileWriter.append(',');
fileWriter.append(tokens[4]); //f
fileWriter.append(',');
fileWriter.append(tokens[5]); //ft
fileWriter.append(',');
fileWriter.append(tokens[6]); //h
fileWriter.append(',');
fileWriter.append(tokens[7]); //tt
fileWriter.append(',');
fileWriter.append(tokens[8]); //h
fileWriter.append(',');
fileWriter.append('\n');
fileWriter.flush();
} catch (IOExceptione) {
e.printStackTrace();
}
sb.setLength(0);
}
}//End while loop
os.close();
is.close();
streamConnection.close();
}
}