gpt4 book ai didi

Java 蓝牙检测失败

转载 作者:行者123 更新时间:2023-12-04 17:08:10 27 4
gpt4 key购买 nike

我正在尝试编写一个程序来检测附近的蓝牙连接。但是,它一直给我抛出一个空数组问题,并且它没有检测到我手机的蓝牙连接。

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)

最佳答案

我不是 Java 专家,但我相信我可以根据源代码为您提供一些指导。

要首先建立蓝牙连接,您应该发现设备。如果我们检查这样一个 discovery can be performed在使用的库中:

There are two ways to discover devices. First, an application may usestartInquiry() to start an inquiry to find devices in proximity to thelocal device. Discovered devices are returned via thedeviceDiscovered() method of the interface DiscoveryListener. Thesecond way to discover devices is via the retrieveDevices() method.This method will return devices that have been discovered via aprevious inquiry or devices that are classified as pre-known.(Pre-known devices are those devices that are defined in the BluetoothControl Center as devices this device frequently contacts.) TheretrieveDevices() method does not perform an inquiry, but provides aquick way to get a list of devices that may be in the area.

这是关键部分:

This method will return devices that have been discovered via aprevious inquiry or devices that are classified as pre-known.

因此在您观看的 YouTube 教程中,我相信导师之前已经发现了该设备,这就是他/她使用它没有问题的原因。

因此,对于您的情况,您应该开始调查。 source code 中解释了该方法.我相信,如果您进行查询,您的问题将得到解决,但让我们进一步调查。

现在,让我们检查一下 getRemoteDevice 以了解错误。 source code将您的案例中抛出的方法解释为:

NullPointerException - if conn is null  

遗憾的是,那里提供的链接不起作用。但我认为,在某些情况下可能需要 null。因此,您应该使用 try catch 方法来处理这个问题。

您可以找到示例代码 hereofficial library's website 中也有一个很好的例子。

最后,我建议阅读官方文档并尝试 GitHub 或官方网站等提供的基本示例,而不是遵循 YouTube 教程。从长远来看,您会受益更多,我保证。

关于Java 蓝牙检测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70051465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com