我在使用藍牙掃描的遠程設備發現代碼時遇到問題。 它掃描,如果我取消註釋「是System.out.print(devicesDiscovered)打印的MAC地址;Java for loop does not execute
但我希望能夠從向量提取每個MAC地址,並將其放置在一個字符串
。我有兩個歧FOR循環要做到這一點,但他們都不似乎執行
代碼:
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import javax.bluetooth.*;
public class BluetoothDeviceDiscovery {
public static final Vector/*<RemoteDevice>*/ devicesDiscovered = new Vector();
public static void main() throws IOException, InterruptedException {
final Object inquiryCompletedEvent = new Object();
devicesDiscovered.clear();
final DiscoveryListener listener = new DiscoveryListener() {
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
devicesDiscovered.addElement(btDevice);
//
String testingAgain = devicesDiscovered.toString();
System.out.println("What?? : " + testingAgain);
/*
* As far as i know, the following two FOR loops do the same thing
* But both of them are not being executed...
*/
//Its not executing this...
for(int i=0; i< devicesDiscovered.size(); i++) {
System.out.println("test if this gets output");
String test = (String) devicesDiscovered.elementAt(i);
System.out.println("Test: " + test);
}
//Its not executing this....
for(int i=0; i> ((List) btDevice).size(); i++){
System.out.println("test if this gets output 1");
String testing = (String) devicesDiscovered.toString();
System.out.print("Test1: " + testing);
}
//Prints the MAC addresses [macaddress, macaddress, macaddress, etc]
// System.out.println(devicesDiscovered);
/*
* Now need to extract each macaddress from devicesDiscovered
* and convert from a Vector to a String
*/
}
public void inquiryCompleted(int discType) {
System.out.println("Device Inquiry completed!");
synchronized(inquiryCompletedEvent){
inquiryCompletedEvent.notifyAll();
}
}
public void serviceSearchCompleted(int transID, int respCode) {
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
};
synchronized(inquiryCompletedEvent) {
boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
if (started) {
System.out.println("wait for device inquiry to complete...");
inquiryCompletedEvent.wait();
System.out.println(devicesDiscovered.size() + " device(s) found");
}
}
}
}
任何人都可以發現任何原因(S),爲什麼這兩個for循環不工作?
非常感謝 - 萊恩
程序的輸出是什麼?我會說設備列表是空的,但沒有輸出我不能肯定這一點。 – bacchus 2011-05-20 21:13:02
在側面說明中,請停止使用'Vector',它屬於yesteryears的API。 – asgs 2011-05-20 21:15:43
輸出只是[MACADDRESS],但For循環內的打印語句都沒有打印......它告訴我循環不執行。你建議我嘗試使用字符串數組而不是矢量然後? – 2011-05-20 21:31:19