Scan for nearby Bluetooth device using JAVAJune 17, 2022
This article is a guide for creating Java applications that can scan for nearby Bluetooth devices. This example project will be helpful to create BLE application easily. Source file is available.
- BleuIO. (Bluetooth Low Energy USB dongle)
About the Project
The script has a COM port settings section. This section shows connected devices to the COM port. Using jSerialComm we get the list of COM ports and show it on a dropdown menu to select.
The connect and disconnect buttons manages the connection of the dongle.
Once we are connected to the BleuIO dongle, we will be able to write AT commands to the dongle using serial port write command.
By clicking on the SCAN button , it will put the dongle into CENTRAL mode, wait for one second and write another AT command (AT+GAPSCAN=3) to scan for nearby Bluetooth devices for three seconds.
The response will be available on the output panel.
The script can be updated as required. For example. If you want to scan for more than three seconds, just update the MainJFrame.java file 271 line.
List of AT commands are available at https://www.bleuio.com/getting_started/docs/commands/
Step 1: Clone the project
- Get the source code from https://github.com/smart-sensor-devices-ab/scan_nearby_bluetooth_java.git
- Now open the project using NetBean.
- Select jSerialComm library to resolve the issue. jSerialComm is available at root folder. You can also download it from https://fazecast.github.io/jSerialComm/
Step 2 : Run the project
Connect BleuIO dongle into the computer.
Run the project using NetBean play button.
Alternatively we can open the project using command line interface by going to the root folder of the project and type
java -jar "dist/JavaBleuIO.jar"
The output will look like this.
Lets select a COM port where the BleuIO dongle is connected and click connect. After successful connection, we should be able to click on scan button. The response will be available on output screen.
If we click on scan button again , it will look for nearby devices for three seconds and show the output.