How to send data over a Bluetooth Low Energy (BLE) link?

63,053

Solution 1

You need to break this process into a few steps, when you connect to a BLE device and discover Services:

  1. Display available gattServices in onServicesDiscovered for your callback

  2. To check whether you can write a characteristic or not
    check for BluetoothGattCharacteristic PROPERTIES -I didn't realize that need to enable the PROPERTY_WRITE on the BLE hardware and that wasted a lot of time.

  3. When you write a characteristic, does the hardware perform any action to explicitly indicate the operation (in my case i was lighting an led)

Suppose mWriteCharacteristic is a BluetoothGattCharacteristic The part where to check the PROPERTY should be like:

if (((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) |
     (charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {
 // writing characteristic functions
 mWriteCharacteristic = characteristic;
  }

And, to write your characteristic:

// "str" is the string or character you want to write
byte[] strBytes = str.getBytes();
byte[] bytes = activity.mWriteCharacteristic.getValue();  
YourActivity.this.mWriteCharacteristic.setValue(bytes);
YourActivity.this.writeCharacteristic(YourActivity.this.mWriteCharacteristic);  

Those are the useful parts of the code that you need to implement precisely.

Refer this github project for an implementation with just a basic demo.

Solution 2

A noob-friendly guide to make Android interact with a LED-lamp.

Step 1. Get an tool to scan your BLE device. I used "Bluetooth LE Lab" for Win10, but this one will do it as well: https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner

Step 2. Analyse the behavior of the BLE device by entering data, I recommend to enter hex values.

Step 3. Get the sample of the Android docs. https://github.com/googlesamples/android-BluetoothLeGatt

Step 4. Modify the UUIDs you find in SampleGattAttributes

My config:

    public static String CUSTOM_SERVICE = "0000ffe5-0000-1000-8000-00805f9b34fb";
    public static String CLIENT_CHARACTERISTIC_CONFIG = "0000ffe9-0000-1000-8000-00805f9b34fb";

    private static HashMap<String, String> attributes = new HashMap();

    static {
        attributes.put(CUSTOM_SERVICE, CLIENT_CHARACTERISTIC_CONFIG);
        attributes.put(CLIENT_CHARACTERISTIC_CONFIG, "LED");
    }

enter image description here

Step 5. In BluetoothService.java modify onServicesDiscovered:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {

        for (BluetoothGattService gattService : gatt.getServices()) {
            Log.i(TAG, "onServicesDiscovered: ---------------------");
            Log.i(TAG, "onServicesDiscovered: service=" + gattService.getUuid());
            for (BluetoothGattCharacteristic characteristic : gattService.getCharacteristics()) {
                Log.i(TAG, "onServicesDiscovered: characteristic=" + characteristic.getUuid());

                if (characteristic.getUuid().toString().equals("0000ffe9-0000-1000-8000-00805f9b34fb")) {

                    Log.w(TAG, "onServicesDiscovered: found LED");

                    String originalString = "560D0F0600F0AA";

                    byte[] b = hexStringToByteArray(originalString);

                    characteristic.setValue(b); // call this BEFORE(!) you 'write' any stuff to the server
                    mBluetoothGatt.writeCharacteristic(characteristic);

                    Log.i(TAG, "onServicesDiscovered: , write bytes?! " + Utils.byteToHexStr(b));
                }
            }
        }

        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    } else {
        Log.w(TAG, "onServicesDiscovered received: " + status);
    }
}

Convert the byte-String using this function:

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
    data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
            + Character.digit(s.charAt(i + 1), 16));
}
return data;
}

PS: The above code is far away from production, but I hope it helps those, who are new to BLE.

Share:
63,053
My God
Author by

My God

Sr. Software Engineer (Information Technology) Technologies - Ruby ON Rails, Java/J2EE, Android Development. Certifications - Sun Certified Java Programmer (SCJP) Experience - 7 years Far better is it to dare mighty things, to win glorious triumphs, even though checkered by failure... than to rank with those poor spirits who neither enjoy nor suffer much, because they live in a gray twilight that knows not victory nor defeat.

Updated on November 03, 2020

Comments

  • My God
    My God over 3 years

    I am able to discover, connect to bluetooth.

    Source Code---

    Connect via bluetooth to Remote Device:

    //Get the device by its serial number
     bdDevice = mBluetoothAdapter.getRemoteDevice(blackBox);
    
     //for ble connection
     bdDevice.connectGatt(getApplicationContext(), true, mGattCallback);
    

    Gatt CallBack for Status:

     private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        //Connection established
        if (status == BluetoothGatt.GATT_SUCCESS
            && newState == BluetoothProfile.STATE_CONNECTED) {
    
            //Discover services
            gatt.discoverServices();
    
        } else if (status == BluetoothGatt.GATT_SUCCESS
            && newState == BluetoothProfile.STATE_DISCONNECTED) {
    
            //Handle a disconnect event
    
        }
        }
    
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    
        //Now we can start reading/writing characteristics
    
        }
    };
    

    Now I want to send commands to Remote BLE device but don't know how to do that.

    Once the command is sent to the BLE device, the BLE device will respond by broadcasting data which my application can receive.