簡體   English   中英

如何設置/獲取/請求從Android到iOS或反之亦然BLE的MTU?

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

我們正在將MTU請求從Android發送到iOS

Android-從此函數onServicesDiscovered回調請求MTU

但是我不知道如何確定對等設備支持是否請求了MTU,以及如何實際協商的MTU。 僅在API級別22(Android L 5.1)中添加了必需的函數:BluetoothGattCallback.onMtuChanged(BluetoothGatt gatt,int mtu,int狀態)。

我的問題是我不知道我可以發送多少個字節的數據包。

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

例如:gatt.requestMtu(182)

iOS-未觸發didSubscribeTo特征回調

- (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];
}

我們需要根據連接的BLE Devices.U設置數據包大小,如果未請求MTU,則對didSubscribeTo特性進行回調,最小MTU大小為20。如何從android獲取和設置此mtu大小。

我們如何設置MTU?

方法requestMtu啟動兩個藍牙設備之間的MTU交換。 他們將在兩個設備都支持的價值上達成共識。

因此,您可以要求較高的MTU:

gatt.requestMtu(2000);

然后等待onMtuChanged回調。 它會告訴您已達成一致的MTU:

@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
    Log.i(TAG, "MTU: " + mtu);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM