Reputation: 65
I'm trying to code up a program that allows me to detect nearby bluetooth connections. However, it's been throwing me a null array problem, and it doesn't detect my phone's bluetooth connectivity.
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");
}
}
}
I'm not sure what's going on as I followed a youtube tutorial that outlined the steps to take to set up this code, and I don't see differences in the code.
I'm pretty sure I hooked up the JAR file correctly as the imports work fine.
And here's the error message AND OUTPUT:
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)
Upvotes: 3
Views: 219
Reputation: 543
I am no expert at Java, but I believe I can provide you some lead based on the source code.
To establish a Bluetooth connection first you should discover devices. If we check how such a discovery can be performed in the used library:
There are two ways to discover devices. First, an application may use startInquiry() to start an inquiry to find devices in proximity to the local device. Discovered devices are returned via the deviceDiscovered() method of the interface DiscoveryListener. The second way to discover devices is via the retrieveDevices() method. This method will return devices that have been discovered via a previous inquiry or devices that are classified as pre-known. (Pre-known devices are those devices that are defined in the Bluetooth Control Center as devices this device frequently contacts.) The retrieveDevices() method does not perform an inquiry, but provides a quick way to get a list of devices that may be in the area.
Here this is the key part:
This method will return devices that have been discovered via a previous inquiry or devices that are classified as pre-known.
Hence in the YouTube tutorial you watched, I believe tutor already discovered that device before, this is why s/he has no problems with it.
So, in your case, you should start an inquiry. The method is explained in the source code. I believe, your issue will be fixed if you perform an inquiry, but let's investigate further.
Now, let's check what getRemoteDevice
to understand the error. The source code explains the method thrown in your case as:
NullPointerException - if conn is null
Sadly, the provided link there does not work. But I assume, null
might expected in some cases. Hence you should handle this with try catch
method.
You can find an example code here, also one good example is available in the official library's website as well.
Finally, I suggest reading official documentations and trying basic examples that are provided in GitHub or official website etc. rather than following YouTube tutorials. You will benefit more in the long term I guarantee that.
Upvotes: 1