2011-05-20 37 views
0

我在使用藍牙掃描的遠程設備發現代碼時遇到問題。 它掃描,如果我取消註釋「是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循環不工作?

非常感謝 - 萊恩

+0

程序的輸出是什麼?我會說設備列表是空的,但沒有輸出我不能肯定這一點。 – bacchus 2011-05-20 21:13:02

+1

在側面說明中,請停止使用'Vector',它屬於yesteryears的API。 – asgs 2011-05-20 21:15:43

+0

輸出只是[MACADDRESS],但For循環內的打印語句都沒有打印......它告訴我循環不執行。你建議我嘗試使用字符串數組而不是矢量然後? – 2011-05-20 21:31:19

回答

1

在我的機器代碼的執行如下:上bluez的

等待

BlueCove版本2.1.0設備查詢完成...

什麼? :[...]

測試,如果這得到輸出

測試:...

設備詢價完成!

1設備(第)發現

BlueCove棧關機完成了

用下面for循環:

for(int i=0; i< devicesDiscovered.size(); i++) 
{ 
    System.out.println("test if this gets output"); 
    String test = (String) devicesDiscovered.elementAt(i).toString(); 
    System.out.println("Test: " + test); 
} 

我注意到,你是測試其for之一循環產生你想要的輸出。我可以說上面的那個有效,但第二個產生了一個異常。您正嘗試將RemoteDevice對象投射到List並遍歷它(for(int i=0; i < ((List) btDevice).size(); i++))。這是不工作的原因,因此也是例外。

6

在這一行

//Its not executing this.... 
for(int i=0; i > ((List) btDevice).size(); i++) { 

您已經打開了>走錯了路...嘗試

for(int i=0; i < ((List) btDevice).size(); i++) { 

代替。

(不重複的原因,是因爲初始值爲0,不大於列表的大小越大!)


在你的第一個循環:

//Its not executing this... 
for(int i=0; i< devicesDiscovered.size(); i++) { 
    System.out.println("test if this gets output"); 

它必須是devicesDiscovered爲空的情況。我建議你做

System.out.println(devicesDiscovered.size()); 

之前的循環調試。

+0

對不起,我完全忘了 - 我已經改變,只是爲了看看是否有任何不同。它不能正常工作,這是... 我期待的是它打印每個向量索引的MAC地址。但目前它不喜歡它 – 2011-05-20 21:21:05

+0

當添加打印大小語句時,它告訴我它的大小爲1,因此有1個MAC地址。我目前有3個藍牙設備正在測試。每次打印出三個不同的MAC地址。 我想我應該有一個完整的重新思考 – 2011-05-20 21:35:39