MTU是什么?
MTU是指在一个协议数据单元中(Protocol Data Unit, PDU) 有效的最大传输Byte。
MTU默认是23byte,但是供我们使用的只有20byte。所以有时候不能满足我们的需求,需要我们手动设置MTU的大小。
core spec中ATT的默认MTU为23个Byte,ATT的Opcode占1个Byte、ATT的Handle占2个Byte、GATT占20个Byte。
23Byte(ATT)=1Byte(Opcode)+2Byte(Handler)+20Byte(BATT)。
BLE版本不同MTU不同
不同的蓝牙版本最大MTU不同,例如:蓝牙4.2的最大MTU=247Byte(不一定正确,也有说是257Byte、也有说是241Byte),蓝牙5.0的最大MTU=512Byte,有效的最大MTU还需要减去协议Byte、Opcode和Handler。
蓝牙4.2:1Byte(Opcode)+2Byte(Handler)+244Byte(BATT)=247Byte(不一定正确,自己测试下)
蓝牙5.0:512Byte(不一定正确,自己测试下)
在Android中修改MTU很简单,只需要调用BluetoothGatt.requestMtu(int MTU)方法即可。requestMtu(intMTU)必须在发现蓝牙服务并建立蓝牙服务连接之后才能调用,否则MTU会默认为20Byte。如果调用成功会自定回调BluetoothGattCallback类中的onMtuChanged(BluetoothGatt gatt, int mtu, int status)方法。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// 蓝牙已连接
mConnectionState = STATE_CONNECTED;
sendBleBroadcast(ACTION_GATT_CONNECTED);
// // 搜索GATT服务。跟设置MTU有冲突,不能放在此处
// mBluetoothGatt.discoverServices();
//设置MTU。连接成功之后设置Mtu的值
mBluetoothGatt.requestMtu(512);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// 蓝牙已断开连接
mConnectionState = STATE_DISCONNECTED;
sendBleBroadcast(ACTION_GATT_DISCONNECTED);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
// 发现GATT服务
if (status == BluetoothGatt.GATT_SUCCESS) {
setBleNotification();
}
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if(BluetoothGatt.GATT_SUCCESS==status) {
Log.e("MTU change success = " , mtu+",");
// 搜索GATT服务。Mtu设置成功之后再去搜索服务
mBluetoothGatt.discoverServices();
setMtuSuccess=true;
} else {
Log.e("MTU change fail!",",");
setMtuSuccess=false;
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
// 收到数据
sendBleBroadcast(ACTION_DATA_AVAILABLE, characteristic);
}
};
我看到一些文档提到在public void onServicesDiscovered(BluetoothGatt gatt, int status) { }方法中设置MTU,但是亲自尝试之后不起作用。所以在连接成功之后立即设置MTU,成功之后再去搜索服务。



