For loop Java for循环不执行
我的蓝牙扫描远程设备发现代码有问题。 如果我取消注释“system.out.print(DevicesDiscovery)”,它会扫描并打印MAC地址 但我希望能够从向量中提取每个MAC地址,并将其放入字符串中 我有两个不同的FOR循环来实现这一点,但它们似乎都没有执行 代码:For loop Java for循环不执行,for-loop,bluetooth,java,For Loop,Bluetooth,Java,我的蓝牙扫描远程设备发现代码有问题。 如果我取消注释“system.out.print(DevicesDiscovery)”,它会扫描并打印MAC地址 但我希望能够从向量中提取每个MAC地址,并将其放入字符串中 我有两个不同的FOR循环来实现这一点,但它们似乎都没有执行 代码: import java.io.IOException; import java.util.List; import java.util.Vector; import javax.bluetooth.*; public
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");
}
}
}
}
import java.io.IOException;
导入java.util.List;
导入java.util.Vector;
导入javax.bluetooth.*;
公共类蓝牙设备发现{
公共静态最终向量/**/DevicesDiscovery=新向量();
public static void main()引发IOException、InterruptedException{
最终对象inquiryCompletedEvent=新对象();
已发现设备。清除();
最终发现侦听器=新发现侦听器(){
发现公共无效设备(远程设备btDevice,设备类cod){
设备发现。添加元素(btDevice);
//
String testingAgain=DevicesDiscoveryd.toString();
System.out.println(“什么?”+testingAgain);
/*
*据我所知,以下两个FOR循环做同样的事情
*但他们两个都没有被处决。。。
*/
//不是执行这个。。。
对于(int i=0;i((列表)btDevice).size();i++){
System.out.println(“测试是否得到输出1”);
字符串测试=(字符串)DevicesDiscoveryd.toString();
系统输出打印(“Test1:+测试”);
}
//打印MAC地址[macaddress、macaddress、macaddress等]
//System.out.println(发现的设备);
/*
*现在需要从发现的设备中提取每个macaddress
*并将向量转换为字符串
*/
}
公共无效查询已完成(int discType){
System.out.println(“设备查询完成!”);
已同步(inquiryCompletedEvent){
inquiryCompletedEvent.notifyAll();
}
}
public void serviceSearchCompleted(int transID,int respCode){
}
发现公共无效服务(int transID,ServiceRecord[]servRecord){
}
};
已同步(inquiryCompletedEvent){
boolean start=LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC,侦听器);
如果(启动){
System.out.println(“等待设备查询完成…”);
inquiryCompletedEvent.wait();
System.out.println(DevicesDiscoveryd.size()+“找到的设备”);
}
}
}
}
有人能找出这两个for循环不工作的原因吗
非常感谢
-Ryan在这行
//Its not executing this....
for(int i=0; i > ((List) btDevice).size(); i++) {
您已将
转到错误的方向…请尝试
for(int i=0; i < ((List) btDevice).size(); i++) {
在循环调试之前。您的代码在我的机器中的执行如下: 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(int i=0;i
我注意到您正在测试哪个
for
循环生成了您想要的输出。我可以说,上面的一个循环有效,但是第二个循环生成了一个异常。您正在尝试将远程设备
对象强制转换到列表
并对其进行迭代(for(int I=0;I<((List)btDevice).size();i++
)。这是不工作的原因,因此是异常情况。您的程序的输出是什么?我想说设备列表是空的,但没有输出,我无法确认。另一方面,请停止使用Vector
,它属于过去的API。输出只是[MACADDRESS]但是For循环中的print语句都没有打印…这告诉我循环不会执行。你建议我尝试使用字符串数组而不是向量吗?对不起,我帮不了你!我在运行代码时遇到了BluetoothStack未初始化的异常!我无法找到解决它的方法。这是一个原因我真的很想设置它。我使用Ubuntu,并且在Eclipse中附加了Bluecove-gpl.jar。很抱歉,我完全忘记了这一点-我改变了它只是为了看看是否有任何不同。它在任何情况下都不起作用,也就是说…我期望的是它打印每个向量索引的MAC地址。但目前,它不起作用当添加打印大小声明时,它告诉我它的大小是1,前1个MAC地址。我目前有3个蓝牙设备正在测试。每次,它都会打印三个MAC地址中不同的一个。我想我应该重新考虑一下
System.out.println(devicesDiscovered.size());
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);
}