/** * Reads the current RSSI (Received Signal Strength Indication). * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @return The request. */ protected final ReadRssiRequest readRssi() { return Request.newReadRssiRequest().setManager(this); }
/** * Cancels all the enqueued operations. The one currently executed will be finished. */ protected final void clearQueue() { final BleManagerGattCallback callback = mGattCallback; if (callback != null) { callback.cancelQueue(); } }
/** * Enqueues the request for asynchronous execution. * <p> * Use {@link #timeout(long)} to set the maximum time the manager should wait until the device * is ready. When the timeout occurs, the request will fail with * {@link FailCallback#REASON_TIMEOUT} and the device will get disconnected. */ @Override public final void enqueue() { super.enqueue(); }
/** * This method disables notifications on the Battery Level characteristic. * * @deprecated Use {@link #disableNotifications(BluetoothGattCharacteristic)} instead. */ @Deprecated protected void disableBatteryLevelNotifications() { Request.newDisableBatteryLevelNotificationsRequest().setManager(this) .done(device -> log(Log.INFO, "Battery Level notifications disabled")) .enqueue(); }
public void enableBatteryLevelCharacteristicNotifications() { if (isConnected()) { // If the Battery Level characteristic is null, the request will be ignored setNotificationCallback(mBatteryLevelCharacteristic) .with(mBatteryLevelDataCallback); enableNotifications(mBatteryLevelCharacteristic) .done(device -> log(Log.INFO, "Battery Level notifications enabled")) .fail((device, status) -> log(Log.WARN, "Battery Level characteristic not found")) .enqueue(); } }
@Override protected void initialize() { setNotificationCallback(mTXCharacteristic) .with((device, data) -> { final String text = data.getStringValue(0); log(LogContract.Log.Level.APPLICATION, "\"" + text + "\" received"); mCallbacks.onDataReceived(device, text); }); requestMtu(260).enqueue(); enableNotifications(mTXCharacteristic).enqueue(); }
@Override Request getNext() { if (!initialized) { initialized = true; return newBeginReliableWriteRequest(); } if (super.isEmpty()) { closed = true; if (cancelled) return newAbortReliableWriteRequest(); return newExecuteReliableWriteRequest(); } return super.getNext(); }
/** * This method makes sure the data sent will be split to at-most MTU-3 bytes long packets. * This is because Long Write does not work with Reliable Write. */ void forceSplit() { if (dataSplitter == null) split(); }
/** * This method is called when Bluetooth Adapter has been enabled. It is also called * after the service was created if Bluetooth Adapter was enabled at that moment. * This method could initialize all Bluetooth related features, for example open the GATT server. * Make sure you call <code>super.onBluetoothEnabled()</code> at this methods reconnects to * devices that were connected before the Bluetooth was turned off. */ protected void onBluetoothEnabled() { for (final BluetoothDevice device : mManagedDevices) { final BleManager<BleManagerCallbacks> manager = mBleManagers.get(device); if (!manager.isConnected()) manager.connect(device).enqueue(); } }
@Override void notifyStarted(@NonNull final BluetoothDevice device) { if (timeout > 0L) { timeoutCallback = () -> { timeoutCallback = null; if (!finished) { notifyFail(device, FailCallback.REASON_TIMEOUT); timeoutHandler.onRequestTimeout(this); } }; handler.postDelayed(timeoutCallback, timeout); } super.notifyStarted(device); }
/** * Reads the current PHY for this connections. * <p> * PHY LE 2M and PHY LE Coded are supported only on Android Oreo or newer. * You may safely read PHY on older platforms, but you will get PHY LE 1M as TX and RX PHY * in the callback. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @return The request. */ protected final PhyRequest readPhy() { return Request.newReadPhyRequest().setManager(this); }
/** * Disconnects from the device or cancels the pending connection attempt. * Does nothing if device was not connected. * * @return The disconnect request. The completion callback will be called after the device * has disconnected and the connection was closed. If the device was not connected, * the completion callback will be called immediately with device parameter set to null. */ @NonNull public final DisconnectRequest disconnect() { return Request.disconnect().setManager(this); }
/** * Enqueues a sleep operation with given duration. The next request will be performed after * at least given number of milliseconds. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param delay the delay in milliseconds. * @return The request. */ protected final SleepRequest sleep(@IntRange(from = 0) final long delay) { return Request.newSleepRequest(delay).setManager(this); }
/** * Requests new MTU. On Android Lollipop or newer it will send the MTU request to the connected * device. On older versions of Android the * {@link MtuCallback#onMtuChanged(BluetoothDevice, int)} set with * {@link MtuRequest#with(MtuCallback)} will be called with current MTU value. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @return The request. */ protected final MtuRequest requestMtu(@IntRange(from = 23, to = 517) final int mtu) { return Request.newMtuRequest(mtu).setManager(this); }
@NonNull @Override public ReliableWriteRequest add(@NonNull final Operation operation) { super.add(operation); // Make sure the write request uses splitting, as Long Write is not supported // in Reliable Write sub-procedure. if (operation instanceof WriteRequest) { ((WriteRequest) operation).forceSplit(); } return this; }
/** * Alias for {@link #cancelQueue()}. */ public void abort() { cancelQueue(); }
@Override public void cancelQueue() { cancelled = true; super.cancelQueue(); }
@Override public int size() { int size = super.size(); // Add Begin Reliable Write if (!initialized) size += 1; // Add Execute or Abort Reliable Write if (!closed) size += 1; return size; }
/** * Enqueues the request for asynchronous execution. */ public void enqueue() { manager.enqueue(this); }
@Override boolean hasMore() { // If no operations were added, consider the RW request empty, no requests will be executed. if (!initialized) return super.hasMore(); return !closed; } }