Java Code Examples for android.bluetooth.bluetoothgatt#requestMtu()

The following examples show how to use android.bluetooth.bluetoothgatt#requestMtu() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 7 votes vote down vote up
@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);
            }
        });
    }
}
 
Example 2
Source File: BluetoothSite.java    From physical-web with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 3
Source File: SerialSocket.java    From SimpleBluetoothLeTerminal with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 4
Source File: RequestMtuGattTransaction.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@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);
    });
}
 
Example 5
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: MtuRequestOperation.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
    return bluetoothGatt.requestMtu(mtu);
}
 
Example 8
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
public static boolean requestMtu(BluetoothGatt gatt, int mtu) {
    return gatt.requestMtu(mtu);
}
 
Example 9
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static boolean requestMtu(BluetoothGatt gatt, int mtu) {
    return gatt.requestMtu(mtu);
}