下面列出了android.bluetooth.BluetoothGatt#requestMtu ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED && status == gatt.GATT_SUCCESS) {
Log.i(TAG, "Connected to GATT server");
mBluetoothGatt = gatt;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
gatt.requestMtu(505);
} else {
gatt.discoverServices();
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected to GATT server");
// ensure progress dialog is removed and running is set false
close();
} else if (status != gatt.GATT_SUCCESS) {
Log.i(TAG, "Status is " + status);
close();
}
}
private void connectCharacteristics2(BluetoothGatt gatt) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "request max MTU");
if (!gatt.requestMtu(MAX_MTU))
onSerialConnectError(new IOException("request MTU failed"));
// continues asynchronously in onMtuChanged
} else {
connectCharacteristics3(gatt);
}
}
@Override
protected void transaction(GattTransactionCallback callback) {
super.transaction(callback);
getConnection().setState(GattState.REQUESTING_MTU);
boolean success = false;
if(FitbitGatt.atLeastSDK(LOLLIPOP)) {
BluetoothGatt localGatt = getConnection().getGatt();
if(localGatt != null) {
success = localGatt.requestMtu(this.mtu);
} else {
Timber.w("Couldn't request a new MTU because the gatt was null");
}
if(success) {
return;
}
} else {
Timber.v("[%s] This can only be done on Lollipop and higher", getDevice());
}
getConnection().setState(GattState.REQUEST_MTU_FAILURE);
TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
builder.gattState(getConnection().getGattState())
.resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
mainThreadHandler.post(() -> {
callCallbackWithTransactionResultAndRelease(callback, builder.build());
getConnection().setState(GattState.IDLE);
});
}
@SuppressWarnings("NewApi")
@Override
public void setMtu(final BleDevice device, int mtu, final BleMtuCallback callback) {
checkNotNull(callback, BleMtuCallback.class);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mHandler.post(new Runnable() {
@Override
public void run() {
callback.onFailure(BleCallback.FAIL_OTHER,
"The minimum android api version which setMtu supports is 21", device);
}
});
return;
}
if (!checkConnection(device, callback)) {
return;
}
mMtuCallbackMap.put(device.address, callback);
BluetoothGatt gatt = mGattMap.get(device.address);
if (mtu < 23) {
mtu = 23;
} else if (mtu > 512) {
mtu = 512;
}
if (gatt == null || !gatt.requestMtu(mtu)) {
mHandler.post(new Runnable() {
@Override
public void run() {
callback.onFailure(BleCallback.FAIL_OTHER, "fail to read rssi because of unknown reason", device);
}
});
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean internalRequestMtu(@IntRange(from = 23, to = 517) final int mtu) {
final BluetoothGatt gatt = bluetoothGatt;
if (gatt == null || !connected)
return false;
log(Log.VERBOSE, "Requesting new MTU...");
log(Log.DEBUG, "gatt.requestMtu(" + mtu + ")");
return gatt.requestMtu(mtu);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean internalRequestMtu(final int mtu) {
final BluetoothGatt gatt = bluetoothGatt;
if (gatt == null)
return false;
return gatt.requestMtu(mtu);
}
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
return bluetoothGatt.requestMtu(mtu);
}
public static boolean requestMtu(BluetoothGatt gatt, int mtu) {
return gatt.requestMtu(mtu);
}
public static boolean requestMtu(BluetoothGatt gatt, int mtu) {
return gatt.requestMtu(mtu);
}