H-Ble
H-ble is a rapid development framework for connecting and exchanging data between Ble modules and mobile phones above Android 4.3
Based on callbacks, it exposes search, connect, send, receive, disconnect interfaces, and enables Ble communication without concern for detailed operations
API
For details, see Github project examples
Get the BleController instance and initialize it (initialization only needs to be done once)
- BleController.getInstance().init(this);
Search for devices and obtain the device list
- BleController.getInstance().scanBle(int time,ScanCallback scanCallbak);
Connected devices
- BleController.getInstance().connect(int time,String deviceAdress,ConnectCallback connectCallback);
To send data
- BleController.getInstance().writeBuffer(byte[] buf,OnWriteCallback onWriteCallback);
Register a listener to receive data
- BleController.getInstance().registReciveListener(String requestKey,OnReceiverCallback onReceiveCallback);
Unlog the listener receiving data
- BleController.getInstance().unregistReciveListener(String requestKey);
disconnect
- BleController.getInstance().closeBleConn();
Necessary action
Add permissions
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Copy the code
The location permission must be added; otherwise, the device cannot be searched for on Android 5.0 or later
Permissions can be obtained dynamically
Specify UUID
- Open the BleController. Class
-
Change the values of the following member variables to the UUID of the BLE module you want to communicate with
private static final String BLUETOOTH_S = "0000fff0-0000-1000-8000-00805f9b34fb"; private static final String BLUETOOTH_NOTIFY_C = "0000fff7-0000-1000-8000-00805f9b34fb"; private static final String BLUETOOTH_WRITE_C = "0000fff6-0000-1000-8000-00805f9b34fb"; Copy the code
Last letter of variable name:
S is the UUID of the service.
C stands for characteristic UUID;
If the hardware does not provide the UUID, download the Ble Debugging Assistant to view it
Making the address
If you like it, welcome Star
The UUID of common Ble watches, heart rate monitors and other devices is not included in this library for automatic matching. Other supported libraries can be used for this purpose.