📄 echoclient.java
字号:
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;
class EchoClient implements DiscoveryListener {
private DiscoveryAgent discoveryAgent;
private RemoteDevice[] remoteDevices;
private UUID[] uuidSet;
private String serviceUrl;
public EchoClient() {
try {
LocalDevice localDevice = LocalDevice.getLocalDevice(); discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (Exception e) {
System.out.println(e);
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));
// Search for Services
uuidSet = new UUID[1];
uuidSet[0] = BluetoothEchoDemo.RFCOMM_UUID;
int searchID = discoveryAgent.searchServices(null,uuidSet,btDevice,this);
} catch (Exception e) {
System.out.println("Device Discovered Error: " + e);
}
}
public void inquiryCompleted(int discType) {
System.out.println("InquiryCompleted");
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
System.out.println("ServicesDiscovered");
// in this example there is only one service
for(int i=0;i<servRecord.length;i++) {
serviceUrl = servRecord[i].getConnectionURL(0,false);
}
}
public void serviceSearchCompleted(int transID, int responseCode) {
if(responseCode == SERVICE_SEARCH_ERROR)
System.out.println("SERVICE_SEARCH_ERROR\n");
if(responseCode == SERVICE_SEARCH_COMPLETED) {
System.out.println("SERVICE_SEARCH_COMPLETED\n");
System.out.println("Service URL: " + serviceUrl);
StreamConnection conn = null;
try {
String msg = "Say Hello World";
conn = (StreamConnection)Connector.open(serviceUrl);
OutputStream output = conn.openOutputStream();
output.write(msg.length()); output.write(msg.getBytes()); output.close();
System.out.println(BluetoothEchoDemo.readData(conn));
} catch (Exception ex) {
System.out.println(ex);
} finally {
try {
conn.close();
} catch (IOException ioe) {
System.out.println("Error Closing connection " + ioe);
}
}
}
if(responseCode == SERVICE_SEARCH_TERMINATED)
System.out.println("SERVICE_SEARCH_TERMINATED\n");
if(responseCode == SERVICE_SEARCH_NO_RECORDS)
System.out.println("SERVICE_SEARCH_NO_RECORDS\n");
if(responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -