BLE-Interact is a powerful native DLL library that provides seamless integration between Java applications and low-level Bluetooth Low Energy (BLE) functionality on Windows systems. This library leverages Windows Runtime (WinRT) APIs to interact with BLE devices and exposes this functionality to Java through JNI (Java Native Interface).
- 🔍 Scan for nearby BLE devices
- 🔌 Connect to specific BLE devices by address
- 🚀 Initialize UART service for communication
- 📤 Send data to BLE devices
- 📥 Receive notifications from BLE devices
- 🔄 Handle automatic device connection status changes
- 🛑 Manage BLE device connections
- Windows operating system
- JDK 8 or higher
- BLE-capable hardware
The library exposes the following JNI functions that can be called from Java:
Initializes the WinRT apartment for BLE operations.
public native void initialize();
Starts a scan for nearby BLE devices. Returns an ArrayList of BLEDevice objects.
public native ArrayList<BLEDevice> searchBLEDevices();
Connects to a BLE device using its address. Returns true if connection is successful.
public native boolean connectDevice(String deviceAddress);
Initializes UART service with the specified UUIDs for communication. Returns true if successful.
public native boolean initializeUARTCharacteristics(String uartServiceUuid, String rxUuid, String txUuid);
Writes data to the RX characteristic of the connected device. Returns true if successful.
public native boolean writeToRX(String data);
Disconnects from the currently connected device. Returns true if successful.
public native boolean disconnectDevice();
Releases all resources used by the library.
public native void cleanup();
Your Java class should load the DLL and declare the native methods:
public class BluetoothBLE {
static {
System.loadLibrary("BleInteract");
}
// Declare native methods here
public native void initialize();
public native ArrayList<BLEDevice> searchBLEDevices();
public native boolean connectDevice(String deviceAddress);
public native boolean initializeUARTCharacteristics(String uartServiceUuid, String rxUuid, String txUuid);
public native boolean writeToRX(String data);
public native boolean disconnectDevice();
public native void cleanup();
// Callback methods that will be called from C++
public void onDeviceConnected(String message) {
// Handle connection event
}
public void onDeviceDisconnected(String message) {
// Handle disconnection event
}
public void onDeviceNotificationReceived(String data) {
// Handle received data
}
}
BluetoothBLE ble = new BluetoothBLE();
// Initialize the BLE service
ble.initialize();
// Search for devices
ArrayList<BLEDevice> devices = ble.searchBLEDevices();
// Connect to a device
if (devices != null && !devices.isEmpty()) {
boolean connected = ble.connectDevice(devices.get(0).getAddress());
if (connected) {
// Initialize UART service
boolean initialized = ble.initializeUARTCharacteristics(
"6E400001-B5A3-F393-E0A9-E50E24DCCA9E", // UART service UUID
"6E400002-B5A3-F393-E0A9-E50E24DCCA9E", // RX UUID
"6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // TX UUID
);
if (initialized) {
// Send data to device
ble.writeToRX("Hello from Java!");
}
}
}
// Clean up when done
ble.disconnectDevice();
ble.cleanup();
Check out the examples
folder in the repository to see working implementations of BLE-Interact. These examples demonstrate how to integrate the library into your Java applications and provide practical use cases for common BLE operations.
- Ensure you have the Windows SDK installed
- Build the DLL using Visual Studio or your preferred C++ compiler
- Place the compiled DLL in your Java project's native library path
- The library handles BLE connection status changes and notifies the Java application through callback methods
- The search function scans for devices for 6 seconds before returning results
- The library properly cleans up resources when disconnected or when the JVM is unloaded
- Error handling is implemented throughout the library for robustness
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by imroodydev