-
- All Implemented Interfaces:
-
no.nordicsemi.android.ble.utils.ILogger
public abstract class BleManager implements ILogger
The BleManager is responsible for managing the low level communication with a Bluetooth LE device. Please see profiles implementation in Android nRF Blinky or Android nRF Toolbox app for an example of use.
This base manager has been tested against number of devices and samples from Nordic SDK.
The manager handles connection events and initializes the device after establishing the connection.
- For bonded devices it ensures that the Service Changed indications, if this characteristic is present, are enabled. Before Android Marshmallow, Android did not enable them by default, leaving this to the developers.
- The manager tries to read the Battery Level characteristic. No matter the result of this operation (for example the Battery Level characteristic may not have the READ property) it tries to enable Battery Level notifications to get battery updates from the device.This feature is now deprecated and will not work with the new API. Instead, read or enabledBattery Level notifications just like any other.
- After connecting and service discovery, the manager initializes the device using given queue of commands. See initialize method for more details.
- When initialization complete, the done callback is called.
The manager also is responsible for parsing the Battery Level values and calling method.
To get logs, override the log method.
The BleManager should be overridden in your app and all the 'high level' callbacks should be called from there.
-
-
Field Summary
Fields Modifier and Type Field Description public final static intPAIRING_VARIANT_PINpublic final static intPAIRING_VARIANT_PASSKEYpublic final static intPAIRING_VARIANT_PASSKEY_CONFIRMATIONpublic final static intPAIRING_VARIANT_CONSENTpublic final static intPAIRING_VARIANT_DISPLAY_PASSKEYpublic final static intPAIRING_VARIANT_DISPLAY_PINpublic final static intPAIRING_VARIANT_OOB_CONSENTBondingObserverbondingObserverConnectionObserverconnectionObserver
-
Constructor Summary
Constructors Constructor Description BleManager(Context context)The manager constructor. BleManager(Context context, Handler handler)The manager constructor.
-
Method Summary
Modifier and Type Method Description final BondingObservergetBondingObserver()Returns the current bonding state observer object. final voidsetBondingObserver(@Nullable() BondingObserver callback)Sets the observer, that will receive events related to bonding. final ConnectionObservergetConnectionObserver()Returns the current connection observer object. final voidsetConnectionObserver(@Nullable() ConnectionObserver callback)Sets the connection observer. voidclose()Closes and releases resources. voidsetGattCallbacks(@NonNull() BleManagerCallbacks callbacks)Sets the manager callback listener. final voiduseServer(@NonNull() BleServerManager server)This method binds the manager with the give server instance. BluetoothDevicegetBluetoothDevice()Returns the Bluetooth device object used in connect. final booleanisConnected()This method returns true if the device is connected. final booleanisReady()Returns true if the device is connected and the initialization has finished,that is when onDeviceReady was called. final intgetConnectionState()Method returns the connection state: STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING, STATE_DISCONNECTED final intgetBatteryValue()Returns the last received value of Battery Level characteristic, or -1 if suchdoes not exist, hasn't been read or notification wasn't received yet. intgetMinLogPriority()Returns the minimum log priority that should be logged. voidlog(int priority, @NonNull() String message)Logs the given message with given log priority into the all managed devices' log session. voidlog(int priority, @StringRes() int messageRes, @Nullable() Array<Object> params)Logs the given message with given log priority into the all managed devices' log session. final ConnectRequestconnect(@NonNull() BluetoothDevice device)Creates a Connect request that will try to connect to the given Bluetooth LE device. final ConnectRequestconnect(@NonNull() BluetoothDevice device, int phy)Creates a Connect request that will try to connect to the given Bluetooth LE device usingpreferred PHY. final DisconnectRequestdisconnect()Disconnects from the device or cancels the pending connection attempt.Does nothing if device was not connected. voidattachClientConnection(BluetoothDevice client)"Server only" alternative to using connect in onDeviceConnectedToServer.This simply associates the connection to the passed client. -
-
Constructor Detail
-
BleManager
BleManager(Context context)
The manager constructor.- Parameters:
context- the context.
-
-
Method Detail
-
getBondingObserver
@RestrictTo(value = RestrictTo.Scope.LIBRARY_GROUP)@Nullable() final BondingObserver getBondingObserver()
Returns the current bonding state observer object.
-
setBondingObserver
final void setBondingObserver(@Nullable() BondingObserver callback)
Sets the observer, that will receive events related to bonding.This callback will be called using the handler given in BleManager.
- Parameters:
callback- the callback.
-
getConnectionObserver
@RestrictTo(value = RestrictTo.Scope.LIBRARY_GROUP)@Nullable() final ConnectionObserver getConnectionObserver()
Returns the current connection observer object.
-
setConnectionObserver
final void setConnectionObserver(@Nullable() ConnectionObserver callback)
Sets the connection observer.This callback will be called using the handler given in BleManager.
- Parameters:
callback- the callback listener.
-
close
void close()
Closes and releases resources. This method will be called automatically aftercalling disconnect. When the device disconnected with link loss and shouldAutoConnect returned true you have to call this method toclose the connection.
-
setGattCallbacks
@Deprecated() void setGattCallbacks(@NonNull() BleManagerCallbacks callbacks)
Sets the manager callback listener.
- Parameters:
callbacks- the callback listener.
-
useServer
final void useServer(@NonNull() BleServerManager server)
This method binds the manager with the give server instance. Apps that allow multiplesimultaneous connections and GATT server should use a single server instance, sharedbetween all clients.
- Parameters:
server- the server instance.
-
getBluetoothDevice
@Nullable() BluetoothDevice getBluetoothDevice()
Returns the Bluetooth device object used in connect.
-
isConnected
final boolean isConnected()
This method returns true if the device is connected. Services could have not beendiscovered yet.
-
isReady
final boolean isReady()
Returns true if the device is connected and the initialization has finished,that is when onDeviceReady was called.
-
getConnectionState
final int getConnectionState()
Method returns the connection state: STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING, STATE_DISCONNECTED
-
getBatteryValue
@IntRange(from = -1, to = 100)@Deprecated() final int getBatteryValue()
Returns the last received value of Battery Level characteristic, or -1 if suchdoes not exist, hasn't been read or notification wasn't received yet.
The value returned will be invalid if overridden readBatteryLevel and enableBatteryLevelNotifications were used.
-
getMinLogPriority
int getMinLogPriority()
Returns the minimum log priority that should be logged.
-
log
void log(int priority, @NonNull() String message)
Logs the given message with given log priority into the all managed devices' log session.
- Parameters:
priority- the log priority.message- the message to be logged.
-
log
void log(int priority, @StringRes() int messageRes, @Nullable() Array<Object> params)
Logs the given message with given log priority into the all managed devices' log session.
- Parameters:
priority- the log priority.messageRes- string resource id.params- additional (optional) parameters used to fill the message.
-
connect
@NonNull() final ConnectRequest connect(@NonNull() BluetoothDevice device)
Creates a Connect request that will try to connect to the given Bluetooth LE device.Call enqueue or await in order to executethe request.
This method returns a ConnectRequest which can be used to set completionand failure callbacks. The completion callback (done) will be called after the initializationis complete, after onDeviceReady has beencalled.
Calling await will make this requestsynchronous (the callbacks set will be ignored, instead the synchronous method willreturn or throw an exception).
For asynchronous call usage, enqueue must be called on the returnedrequest.
- Parameters:
device- a device to connect to.
-
connect
@NonNull()@Deprecated() final ConnectRequest connect(@NonNull() BluetoothDevice device, int phy)
Creates a Connect request that will try to connect to the given Bluetooth LE device usingpreferred PHY. Call enqueue or await in order to execute the request.
This method returns a ConnectRequest which can be used to set completionand failure callbacks. The completion callback will be called after the initializationis complete, after onDeviceReady has beencalled.
Calling await will make this requestsynchronous (the callbacks set will be ignored, instead the synchronous method willreturn or throw an exception).
For asynchronous call usage, enqueue must be called on the returnedrequest.
- Parameters:
device- a device to connect to.phy- preferred PHY for connections to remote LE device.
-
disconnect
@NonNull() final DisconnectRequest disconnect()
Disconnects from the device or cancels the pending connection attempt.Does nothing if device was not connected.
-
attachClientConnection
void attachClientConnection(BluetoothDevice client)
"Server only" alternative to using connect in onDeviceConnectedToServer.This simply associates the connection to the passed client.
-
-
-
-