在BluetoothGattCallback回调方法里调用 gatt.requestMtu(131)实现一包发送128个字节的数据,
因系统占用3个字节,所以要一包发送128个字节需要设置131。
private final BluetoothGattCallback callback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {//连接成功
gatt.discoverServices();
} else {//断开
closeGatt();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {//发现服务
gatt.requestMtu(131);//设置MTU,系统占用3字节,设置131实际上是128
} else {
closeGatt();
}
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
setNotify(gatt);
} else {
closeGatt();
}
}