2

I am trying to create an connection between an Tablet and my MCU which connects as BLE and send/receive characteristics via GATT. The MCU is the GATT Server and the Tablet is my GATT Client.

What is working so far: Both devices are connecting to each other. I can send notifications with my GATT Server so my GATT Client is reading the Chosen Characteristic in the onCharacterChanged for further Tasks.

But i have some weird behavior on writing characteristics from my Gatt Client to the Server. I am triggering the send process by a onClick function:

buttonOne.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        byte[] testByte = new byte[1];
        testByte[0] = 3;
        characteristic.setValue(testByte);
        bluetoothGatt.writeCharacteristic(characteristic);
        }
     });

And this is working so far (i am filling the characteristic object before the onClick). Now i have another onClickListener just to Change the Value of the Characteristic and send it again.

buttonTwo.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
    byte[] testByte = new byte[1];
    testByte[0] = 7;
    characteristic.setValue(testByte);
    bluetoothGatt.writeCharacteristic(characteristic);
    }
 });

So once my characteristic on the GATT Server should have the value 3 and once 7. And this is working only once till my Connection Drops and get reopened, then i can resend the characteristic. The writeCharacteristic is from the BluetoothGatt class and looks like this:

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
        && (characteristic.getProperties() &
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;

    if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
    if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;

    BluetoothGattService service = characteristic.getService();
    if (service == null) return false;

    BluetoothDevice device = service.getDevice();
    if (device == null) return false;

    synchronized(mDeviceBusy) {
        if (mDeviceBusy) return false;
        mDeviceBusy = true;
    }

    try {
        mService.writeCharacteristic(mClientIf, device.getAddress(),
            service.getType(), service.getInstanceId(),
            new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
            new ParcelUuid(characteristic.getUuid()),
            characteristic.getWriteType(), AUTHENTICATION_NONE,
            characteristic.getValue());
    } catch (RemoteException e) {
        Log.e(TAG,"",e);
        mDeviceBusy = false;
        return false;
    }

    return true;
}

I've found out that the process is stuck in

synchronized(mDeviceBusy) {
    if (mDeviceBusy) return false;
    mDeviceBusy = true;
}

but i have no idea why so far. Is there any bit i should set to tell my GATT Server that i will "rewrite" that characteristic? Do i Need to use a thread or something and put it on sleep?

B. Ben
  • 109
  • 1
  • 9
  • What do you mean by "working only once"? First you click `buttonOne` and after that you click `buttonTwo`, right? Are both values written to the GATT server correctly or not? And why/when does your connection drop? Please clarify and provide more detail about the exact sequence of actions. – reflective_mind Oct 11 '16 at 01:47
  • Yeah you are right. It is like this: 1. Click buttonOne -> values is getting written in the GATT Server 2. Connection Drops -> reconnect needed from the MCU side 3. reconnected 4. click buttonTwo -> new value is getting written in the GATT Server 5. like 2. ongoing It seems like something is missing in the process of acknowledgements but i am sending an Response from my gatt Server that the new value has been written to my Client. – B. Ben Oct 11 '16 at 07:14
  • ok the Problem is the synchronized part thats for sure now because of every time it is reaching the mService.writeCharacteristic in the try block my values i getting sent to the Server. but still don't know why – B. Ben Oct 13 '16 at 12:16

0 Answers0