-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlueCoveExample.java
140 lines (123 loc) · 5.19 KB
/
BlueCoveExample.java
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
package com.itheima;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class BlueCoveExample {
public static StreamConnection connection = null;
private static boolean isConnected = false;
public static void main(String[] args) {
try {
// 搜索附近的蓝牙设备
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
RemoteDevice[] remoteDevices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACHED);
// 打印设备名称
for (RemoteDevice remoteDevice : remoteDevices) {
String deviceName = remoteDevice.getFriendlyName(false);
System.out.println("Device Address: " + remoteDevice.getBluetoothAddress());
System.out.println("Device Name: " + deviceName);
if (deviceName.equals("BOHB-WAX9")) {
// 连接名为 "apple" 的设备
isConnected = connectToDevice(remoteDevice);
if (isConnected) {
System.out.println("Connected to device: " + deviceName);
// 在这里可以进行进一步的操作
String filePath = "C:\\Users\\ASUS\\IdeaProjects\\StudyJava\\cqu_projects\\bluetoothTest\\src\\main\\java\\com\\itheima/example.txt";
sendFile(filePath);
//sendMessages();
receiveMessages();
//sleep for 100s
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("Failed to connect to device: " + deviceName);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean connectToDevice(RemoteDevice remoteDevice) {
try {
// 创建一个连接
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false); // SPP UUID
String deviceAddress = remoteDevice.getBluetoothAddress();
String connectionString = "btspp://" + deviceAddress + ":1;authenticate=false;encrypt=false;master=false";
connection = (StreamConnection) Connector.open(connectionString);
// 连接成功
// 在这里可以进行进一步的操作
return true;
} catch (Exception e) {
e.printStackTrace();
return false; // 连接失败
}
}
private static void receiveMessages() {
if (!isConnected) {
System.out.println("Not connected to any device.");
return;
}
try {
// 在新线程中持续读取输入流的数据
Thread thread = new Thread(() -> {
try {
InputStream inputStream = connection.openInputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
String message = new String(buffer, 0, bytesRead);
System.out.println("Received message: " + message);
}
} catch (IOException e) {
e.printStackTrace();
}
});
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sendMessages() {
if (!isConnected) {
System.out.println("Not connected to any device.");
return;
}
try {
OutputStream outputStream = connection.openOutputStream();
String message = "Hello, World!";
outputStream.write(message.getBytes());
outputStream.flush();
System.out.println("Message sent: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendFile(String filePath) {
if (!isConnected) {
System.out.println("Not connected to any device.");
return;
}
try {
OutputStream outputStream = connection.openOutputStream();
FileInputStream fileInputStream = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
outputStream.flush();
System.out.println("File sent: " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}