2

We are sending mtu request from android to iOS

Android - Requesting mtu from this function onServicesDiscovered callback

But I do not know the way how to find out if peer device support requested MTU and what is actually negotiated MTU. The required function: BluetoothGattCallback.onMtuChanged(BluetoothGatt gatt, int mtu, int status) was added only in API level 22 (Android L 5.1).

My problem is that I do not know how many bytes in packet I can send.

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

                    gatt.requestMtu(182);

                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
                List<BluetoothGattService> Services = gatt.getServices();
                for (BluetoothGattService gattService : Services) {
                    if (SERVICE_UUID.equals(gattService.getUuid())) {
                        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
                        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                            if (CHARACTERISTIC_UUID.equals(gattCharacteristic.getUuid())) {
                                gatt.writeCharacteristic(gattCharacteristic);
                                List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors();
                                for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
                                    gatt.readDescriptor(gattDescriptor);
                                }
                            }
                        }
                    }
                }
            } else {
                Log.w(MainActivity.TAG, "onServicesDiscovered received: " + status);
          }
    }

Ex: gatt.requestMtu(182)

IOS - not triggered didSubscribeTo characteristic callback

- (void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{
  NOTIFY_MTU = central.maximumUpdateValueLength;
  NSLog(@"Central subscribed to characteristic");
  NSLog(@"Supported to BLE Device Info:--> %lu",(unsigned long)[central maximumUpdateValueLength]);
  [peripheral setDesiredConnectionLatency:CBPeripheralManagerConnectionLatencyLow forCentral:central];
}

We need to set packet size based on the connected BLE Devices.if MTU not requested we have a callback on didSubscribeTo characteristic, were the minimum MTU size is 20. How to get & set this mtu size form android.

How to we set the MTU?

Manikandan S
  • 115
  • 3
  • 18

1 Answers1

3

The method requestMtu starts an MTU exchange between the two Bluetooth devices. They will agree on a value that both devices support.

So you can request a high MTU:

gatt.requestMtu(2000);

Then wait for the onMtuChanged callback. It will tell you the MTU that has been agreed:

@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
    Log.i(TAG, "MTU: " + mtu);
}
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Hi @Codo, hear is the response we get in iOS after requesting the mtu Maximum size from Android. subscribed to " )> – Manikandan S Aug 07 '19 at 14:24
  • 1
    Do you implement the correct order: increase MTU, wait for response, subscribe to characteristic? – Codo Aug 07 '19 at 15:22
  • Hi @Codo Thanks, Yes we have set the MTU request first from android (central) and then subscribing to the characteristic. Here is what happens, we are setting up the gatt.requestMtu(2000) from android and we don't see the didSubscribeToCharacteristic() in the iOS callback triggered. – Manikandan S Aug 08 '19 at 05:20
  • Neither in your code nor in your comments I can see any trace of waiting for the relevant callbacks. Many BLE operations are asynchronous and the Android API just starts them. They don't complete immediately. Are you waiting for the callbacks before calling the next operation, e.g. after the call to `requestMtu`? – Codo Aug 08 '19 at 06:17
  • Now we are waiting for callback on request MTU, But we are not receiving the callback. please provide us some other pointers. which would improve performance from android to iOS & viceversa. thanks. – Manikandan S Aug 09 '19 at 04:52
  • Increasing the MTU is the single most effective measure to increase performance. And both Android and iOS a certainly capable of MTUs higher than 20 bytes. I recommend you start a new question to investigate this particular problem. Provide complete but minimal code so it can easily be reproduced. – Codo Aug 09 '19 at 08:06
  • Can you please provide your mail id?, I have send our source code android & iOS. – Manikandan S Aug 09 '19 at 10:13