我正在尝试编写一个程序,让我能够检测附近的蓝牙连接。然而,它一直抛给我一个空阵列的问题,它没有检测到我的手机的蓝牙连接。
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import java.io.IOException;
// import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
public class Greenteeth {
public Greenteeth() throws IOException {
LocalDevice device = LocalDevice.getLocalDevice();
RemoteDevice[] remoteDevice = device.getDiscoveryAgent().retrieveDevices(DiscoveryAgent.PREKNOWN);
if (remoteDevice == null) {
System.out.println("0");
}
for (RemoteDevice d : remoteDevice) {
System.out.print("DEVICE NAME: " + d.getFriendlyName(false));
System.out.println("GREENTEETH ADDRESS: " + d.getBluetoothAddress() + "\n");
}
}
}我不确定发生了什么,因为我遵循了youtube教程,该教程概述了设置此代码的步骤,并且我看不出代码中的差异。
我非常确定我正确地连接了JAR文件,因为导入工作正常。
下面是错误消息和输出:
BlueCove version 2.1.1-SNAPSHOT on winsock
0 // prints because array is null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read the array length because "remoteDevice" is null
at Greenteeth.<init>(Greenteeth.java:20)
at Home$ConnectBT.actionPerformed(Home.java:120)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6379)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)发布于 2021-11-21 11:49:07
我不是Java方面的专家,但我相信我可以根据源代码为您提供一些线索。
要首先建立蓝牙连接,您应该使用发现设备。如果我们检查一下used库中这样的discovery can be performed:
有两种方法可以发现设备。首先,应用程序可以使用startInquiry()来启动查询,以查找与本地设备相邻的设备。发现的设备通过接口DiscoveryListener的deviceDiscovered()方法返回。发现设备的第二种方法是通过retrieveDevices()方法。此方法将返回通过先前查询发现的设备或分类为预知的设备。(预知设备是指在蓝牙控制中心中定义为该设备经常接触的设备。)retrieveDevices()方法不执行查询,但提供了一种快速获取该区域中可能存在的设备列表的方法。
这里是关键部分:
此方法将返回通过以前的查询发现的设备或分类为预知的设备。
因此,在你看过的YouTube教程中,我相信导师之前已经发现了这个设备,这就是为什么他/她对它没有问题。
所以,在你的情况下,你应该开始调查。该方法在source code中进行了说明。我相信,如果您进行查询,您的问题会得到解决,但让我们进一步调查。
现在,让我们检查一下getRemoteDevice来理解这个错误。source code将在您的案例中抛出的方法解释为:
NullPointerException - if conn is null 遗憾的是,这里提供的链接不起作用。但我假设,在某些情况下,null可能会预料到。因此,您应该使用try catch方法来处理此问题。
您可以在official library's website中找到示例代码here,也可以找到一个很好的示例。
最后,我建议阅读官方文档并尝试GitHub或官方网站等提供的基本示例,而不是遵循YouTube教程。我向你保证,从长远来看,你会受益更多。
https://stackoverflow.com/questions/70051465
复制相似问题