Manikandan S
Manikandan S

Reputation: 135

How to set/get/request MTU from android to iOS or viceversa BLE?

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?

Upvotes: 2

Views: 8804

Answers (1)

Codo
Codo

Reputation: 79023

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);
}

Upvotes: 3

Related Questions