2011-05-20 65 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