Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BLE, can not really write characteristic

I'm working on a Android project which is to connect Nexus 7 and a bio sensor through BLE link. The problem is that, I can successfully detect and get list of services and characteristics of the sensor. When I write some data to the specific characteristic, onCharacteristicWrite is automatically called and showed me writing operation is successful. However, the sensor never receive anything from the tablet. And if I use similar app on iPhone, everything works fine. So there's no problem with the device. Does anyone have any idea of the problem?

Here is my code for write:

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnected = true;
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnected = false;
            Log.i(TAG, "Disconnected from GATT server.");
        }
    }

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

         //Once detected services, write to characteristic for 6 times.
          int count =6;
            while(count>0){

              writeCharacteristic();

                count--;

            }

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

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic,
                                      int status){

        if (status == BluetoothGatt.GATT_SUCCESS){

            Log.d(TAG,"Write to Characteristic Success! !");
        }

    }
};

public boolean writeCharacteristic(){

    //check mBluetoothGatt is available
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }

    BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
    if (Service == null) {
        Log.e(TAG, "service not found!");
        return false;
    }
    BluetoothGattCharacteristic characteristic = Service
            .getCharacteristic(UUID_MY_CHARACTERISTIC);
    if (characteristic == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] value = {(byte)300,(byte)100,(byte)100};
    characteristic.setValue(value);

    boolean status = mBluetoothGatt.writeCharacteristic(characteristic);

    return status;
}

The output shows "Write to Characteristic Success! !" for six times, thus the writing operation succeeded. However, the device shows that nothing been received from tablet. I also tried to write one byte at a time, or add a timer to let the tablet write to sensor every 2 seconds. But none of them worked. Any ideas?

like image 464
Magic Avatar asked Nov 10 '22 10:11

Magic


1 Answers

(Answered by question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

Follow Up:

The problem solved by manually pairing the tablet with the device first in the setting instead of pairing by code.

So only using the code snippet of connecting Gatt provided by Android is not good enough to pair the device. I should add another code I found online to pair the devices if I don't want to pair them manually every time:

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass()
                .getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}
like image 122
2 revs Avatar answered Nov 14 '22 23:11

2 revs