📄 discovery.java
字号:
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class Discovery extends Thread implements DiscoveryListener {
private Object waitMutex = new Object();
private DeviceConnection protocol;
private Vector foundDevices = new Vector();
private RemoteDevice currentDevice;
private boolean serviceSearchInError;
public Discovery(DeviceConnection protocol) {
this.protocol = protocol;
}
public void run() {
try {
while (true) { // 设备连接监视器
foundDevices.removeAllElements();
DiscoveryAgent agent = LocalDevice.getLocalDevice()
.getDiscoveryAgent();
synchronized (waitMutex) {
Log.log("Start inquiry method to found devices.");
agent.startInquiry(Setting.DISCOVERY_MODE, this);
waitMutex.wait();
}
UUID uuids[] = new UUID[] { Setting.UUID };
for (int i = 0; i < foundDevices.size(); i++) {
serviceSearchInError = false;
for (int t = 0; t < Setting.MAX_TRY_COUNT_TO_SEARCH_SERVICE; t++) {
synchronized (waitMutex) {
currentDevice = (RemoteDevice) foundDevices.elementAt(i);
Log.log("Start to search the Serial Port Profile(SPP) service from "+ deviceString(currentDevice));
agent.searchServices(null, uuids, currentDevice,this);
waitMutex.wait();
}
if (!serviceSearchInError)
break;
else
Thread.sleep(2 * 1000);
}
}
Thread.sleep(Setting.SLEEP_TIME_BEFORE_NEW_DISCOVERY * 1000);
}
} catch (Exception e) {
Log.log("DiscoveryThread-Exception", e);
}
}
public void inquiryCompleted(int arg0) {
synchronized (waitMutex) {
waitMutex.notify();
}
}
public void deviceDiscovered(RemoteDevice dev, DeviceClass clazz) {
Log.log("设备已发现: " + deviceString(dev));
if (!protocol.hasConnection(dev.getBluetoothAddress()))
foundDevices.addElement(dev);
}
public void servicesDiscovered(int transId, ServiceRecord[] records) {
Log.log("Service discovered.");
for (int i = 0; i < records.length; i++) {
if (records[i] == null)
continue;
String url = records[i].getConnectionURL(
Setting.SECURITY_OPTIONS,
Setting.MUST_BE_MASTER);
try {
StreamConnection stream = (StreamConnection) Connector
.open(url);
stream = new ConnectionBuffer(stream);
protocol.handleClientConnection(stream, currentDevice
.getBluetoothAddress());
break;
} catch (IOException e) {
Log.log("DiscoveryThread-Exception", e);
}
}
}
//java.bluetooth API
public void serviceSearchCompleted(int transId, int respCode) {
serviceSearchInError = false;
String msg = null;
switch (respCode) {
case SERVICE_SEARCH_COMPLETED:
msg = "the service search completed normally";
break;
case SERVICE_SEARCH_TERMINATED:
msg = "the service search request was cancelled by a call to DiscoveryAgent.cancelServiceSearch()";
break;
case SERVICE_SEARCH_ERROR:
msg = "an error occurred while processing the request";
serviceSearchInError = true;
break;
case SERVICE_SEARCH_NO_RECORDS:
msg = "no records were found during the service search";
break;
case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
msg = "the device specified in the search request could not be reached or the local device could not establish a connection to the remote device";
break;
}
Log.log("Service search completed - " + msg);
synchronized (waitMutex) {
waitMutex.notify();
}
}
private String deviceString(RemoteDevice dev) {
String ret = null;
try {
if (Setting.WORKAROUND_BLUECOVE)
ret = "";
else
ret = dev.getFriendlyName(false);
} catch (IOException e) {
ret = "none";
}
ret += " - " + dev.getBluetoothAddress();
return ret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -