4

I am working on App which is communicate with BLE device.I can write 20 bytes easily on characteristics but when it is more than 20 bytes it's create problem.I am using

mBluetoothGatt.requestMtu(512);

write charateristics after getting success.

 @Override
                    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
                        super.onMtuChanged(gatt, mtu, status);

                        if (status == BluetoothGatt.GATT_SUCCESS) {
                            System.out.print("Mtu Granted");
//                            this.supportedMTU = mtu;
                        }
                    }

which is working fine in marshmallow and lolipop.But it is not working in Nougat(Samsung galaxy s6).

MIkka Marmik
  • 1,101
  • 11
  • 29
  • Any specific error for Nought ? – Piyush Sep 11 '17 at 06:12
  • No,There is no specific error.I got success in onMtuchanged but it's not reflect on BLE device. – MIkka Marmik Sep 11 '17 at 06:13
  • In Nought OS there is some change for Bluetooth connection. Please check android developers official sites – Piyush Sep 11 '17 at 06:14
  • 2
    May be it's device specific because when i request MTU in mi note 5 then it's granted MTU size 180.In samsung galaxy s6 is granted MTU request but it's not allowed to write more than 20 bytes on write characteristic. – MIkka Marmik Sep 12 '17 at 13:59

2 Answers2

3

I had the same problem. It was because I had multiple requests from the Gatt object at the same time. I called GATT.discoverServices() and GATT.requestMtu(512) at the same time. And always, only the first call worked. Gatt object has several callbacks, so for example, you can first request changing the MTU. Then when onMtuChanged is called, you call to ask for the other request. In my case, GATT.discoverServices()

Ehsan Shadi
  • 609
  • 6
  • 17
  • 1
    I would recommend the other way around, since onMtuChanged might be called without you requesting it immediately after onConnectionStateChange, in case the device was already connected (to another app) on the system. In that case you can note this and then avoid sending the mtu request when service discovery is complete. – Emil Feb 28 '21 at 11:48
2

I was having this exact same issue, so I put the mtu request in a loop and it seems to work after 2 attempts regularly.

            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!mtuConfirmed) {
                        mBluetoothGatt.requestMtu(512);
                        mtuRequestCounter++;
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Log.d(TAG, "MTU change reply received after " + mtuRequestCounter + " attempts");
                }
            }).start();
Grammo
  • 29
  • 4
  • 5
    All requests to a BluetoothGatt object must be serialised. You can't request mtu while you have another pending operation. – Emil Dec 01 '17 at 18:22