This blog focuses on Android Bluetooth BLE basic communication.
1, Add permission
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
However, after Android 6.0, you need to dynamically apply for location permission.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Two. Scan
- Initialize Bluetooth adapter first
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
- Start and stop scanning according to Bluetooth adapter
mBluetoothAdapter.startLeScan(mLeScanCallback);//Start scanning mBluetoothAdapter.stopLeScan(mLeScanCallback);//Stop scanning private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { //We can't do too many operations here. We need to throw them into another thread. runOnUiThread(new Runnable() { @Override public void run() { //Device: device object //rssi signal value //scanRecord broadcast package } }); } };
Three, connection
After getting the MAC address of the device, you can connect
BluetoothGatt mGatt = device.connectGatt(mContext, false, new BluetoothGattCallback() { //Callback when connection status changes @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Successful connection"); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.i(TAG, "connection dropped"); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { });
4, Receiving and sending
- Receive
If the Bluetooth device has the notification / indentation function, then we can get the data through the onCharacteristicChanged() callback, of course, we also need to set it. When the Bluetooth connection is successful, you need to turn on the notification function to tell the device to send us messages.
//Set to notify mGatt.setCharacteristicNotification(mCharacteristic, mEnable); BluetoothGattDescriptor descriptor = mCharacteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mGatt.writeDescriptor(descriptor);
- Send out
characteristicWrite.setValue(getAllDataOrder()); mGatt.writeCharacteristic(characteristicWrite);
Here, mGatt is the object after successful connection, which needs to be set as a global variable.
5, Parse data
/** * byte[] Conversion string * @param bytes * @return */ public static String bytesToHexString(byte[] bytes) { String result = ""; for (int i = 0; i < bytes.length; i++) { String hexString = Integer.toHexString(bytes[i] & 0xFF); if (hexString.length() == 1) { hexString = '0' + hexString; } result += hexString.toUpperCase(); } return result; }
OK, briefly introduce the scan, connection and data sending and receiving of Bluetooth BLE. It's over. Next time, introduce the development of SPP in Android Bluetooth protocol.