for-loop - Java for 循环不执行

标签 for-loop bluetooth java

我的蓝牙扫描远程设备发现代码出现问题。 如果我取消注释“system.out.print(devicesDiscovered);”,它会扫描并打印 MAC 地址;

但我希望能够从 vector 中提取每个 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");
            }
        }

    }
}

任何人都可以找出这两个 for 循环不起作用的任何原因吗?

非常感谢 - 瑞安

最佳答案

在这一行

//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());

在要调试的循环之前。

关于for-loop - Java for 循环不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6077624/

相关文章:

excel - VBA 循环永远存在 - 我在哪里可以优化?

c++ - 基于范围的切片 for 循环?

java - Android蓝牙编程的代码示例

iOS - CoreBluetooth didDiscoverPeripheral 仅调用一次

ios - CLBeaconRegion 没有从终止调用 didEnterRegion

java - JSCH Java 小程序

python列表从for循环到一个列表

Java代码优化

java - 如何制作匹配带定界符和分隔符的标记的正则表达式?

c++ - 如何在完整图的每条边上精确迭代一次?