Package 

Class BleManager

  • 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.

    • Constructor Detail

      • BleManager

        BleManager(Context context)
        The manager constructor.
        Parameters:
        context - the context.
      • BleManager

        BleManager(Context context, Handler handler)
        The manager constructor.
        Parameters:
        context - the context.
        handler - the handler used for delaying operations, timeouts and, most of all, therequest callbacks (done/fail/with, etc).
    • Method Detail

      • 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.
      • 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.

      • 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.
      • 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.

      • 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.