Class DeviceManager

java.lang.Object
com.github.hypfvieh.bluetooth.DeviceManager

public class DeviceManager extends Object
The 'main' class to get access to all DBus/bluez related objects.
Author:
hypfvieh
  • Method Details

    • createInstance

      public static DeviceManager createInstance(boolean _sessionConnection) throws org.freedesktop.dbus.exceptions.DBusException
      Create a new DeviceManager using the UnixDomainSockets and use either the global SYSTEM interface or create a new interface just for this session (if _sessionConnection = true).
      Parameters:
      _sessionConnection - true to create user-session, false to use system session
      Returns:
      DeviceManager
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on error
    • closeConnection

      public void closeConnection()
      Close current connection.
    • createInstance

      public static DeviceManager createInstance(String _address) throws org.freedesktop.dbus.exceptions.DBusException
      Create a new DeviceManager instance using the given DBus address (e.g. tcp://127.0.0.1:13245)
      Parameters:
      _address - address to connect to
      Returns:
      DeviceManager
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on error
    • getInstance

      public static DeviceManager getInstance()
      Get the created instance.
      Returns:
      DeviceManager, never null
    • scanForBluetoothAdapters

      public List<BluetoothAdapter> scanForBluetoothAdapters()
      Search for all bluetooth adapters connected to this machine. Will set the defaultAdapter to the first adapter found if no defaultAdapter was specified before.
      Returns:
      List of adapters, maybe empty, never null
    • scanForBluetoothDevices

      public List<BluetoothDevice> scanForBluetoothDevices(int _timeout)
      Scan for bluetooth devices using the default adapter.
      Parameters:
      _timeout - timout to use for scanning
      Returns:
      list of found BluetoothDevice
    • scanForBluetoothDevices

      public List<BluetoothDevice> scanForBluetoothDevices(String _adapter, int _timeoutMs)
      Scan for Bluetooth devices for on the given adapter. If adapter is null or could not be found, the default adapter is used.
      Parameters:
      _adapter - adapter to use (either MAC or Dbus-Devicename (e.g. hci0))
      _timeoutMs - timeout in milliseconds to scan for devices
      Returns:
      list of found BluetoothDevice
    • findBtDevicesByIntrospection

      public void findBtDevicesByIntrospection(BluetoothAdapter adapter)
      Gets all devices found by the given adapter and published by bluez using DBus Introspection API.
      Parameters:
      adapter - bluetooth adapter
    • setScanFilter

      Setup bluetooth scan/discovery filter.
      Parameters:
      _filter - filter to apply
      Throws:
      BluezInvalidArgumentsException - when argument is invalid
      BluezNotReadyException - when bluez not ready
      BluezNotSupportedException - when operation not supported
      BluezFailedException - on failure
    • getAdapter

      public BluetoothAdapter getAdapter()
      Get the current adapter in use.
      Returns:
      the adapter currently in use, maybe null
    • getAdapter

      public BluetoothAdapter getAdapter(String _ident)
      Find an adapter by the given identifier (either MAC or device name). Will scan for devices if no default device is given and given ident is also null. Will also scan for devices if the requested device could not be found in device map.
      Parameters:
      _ident - mac address or device name
      Returns:
      device, maybe null if no device could be found with the given ident
    • getAdapters

      public List<BluetoothAdapter> getAdapters()
      Returns all found bluetooth adapters. Will query for adapters if scanForBluetoothAdapters() was not called before.
      Returns:
      list, maybe empty
    • getDevices

      public List<BluetoothDevice> getDevices()
      Get all bluetooth devices connected to the defaultAdapter.
      Returns:
      list - maybe empty
    • getDevices

      public List<BluetoothDevice> getDevices(boolean _doNotScan)
      Get all bluetooth devices connected to the defaultAdapter.
      Parameters:
      _doNotScan - true to disable new device recovery, just return all devices already known by bluez, false to scan before returning devices
      Returns:
      list - maybe empty
    • getDevices

      public List<BluetoothDevice> getDevices(String _adapterMac)
      Get all bluetooth devices connected to the adapter with the given MAC address.
      Parameters:
      _adapterMac - adapters MAC address
      Returns:
      list - maybe empty
    • getDevices

      public List<BluetoothDevice> getDevices(String _adapterMac, boolean _doNotScan)
      Get all bluetooth devices connected to the adapter with the given MAC address.
      Parameters:
      _adapterMac - adapters MAC address
      _doNotScan - true to disable new device recovery, just return all devices already known by bluez, false to scan before returning devices
      Returns:
      list - maybe empty
    • setDefaultAdapter

      public void setDefaultAdapter(String _adapterMac) throws BluezDoesNotExistException
      Setup the default bluetooth adapter to use by giving the adapters MAC address.
      Parameters:
      _adapterMac - MAC address of the bluetooth adapter
      Throws:
      BluezDoesNotExistException - when item does not exist if there is no bluetooth adapter with the given MAC
    • setDefaultAdapter

      public void setDefaultAdapter(BluetoothAdapter _adapter) throws BluezDoesNotExistException
      Setup the default bluetooth adapter to use by giving an adapter object.
      Parameters:
      _adapter - bluetooth adapter object
      Throws:
      BluezDoesNotExistException - when item does not exist if there is no bluetooth adapter with the given MAC or adapter object was null
    • setLazyScan

      public void setLazyScan(boolean _scan)
      Enable/disable lazy scanning. When looking for new devices already known devices will usually be queried for GATT services again.
      When lazy scan is enabled, re-querying of GATT services will NOT be performed.
      The default is false (lazy scanning disabled)
      Parameters:
      _scan - true to enable
    • registerPropertyHandler

      public void registerPropertyHandler(org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler _handler) throws org.freedesktop.dbus.exceptions.DBusException
      Register a PropertiesChanged callback handler on the DBusConnection.
      Parameters:
      _handler - callback class instance
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on error
    • unRegisterPropertyHandler

      public void unRegisterPropertyHandler(org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler _handler) throws org.freedesktop.dbus.exceptions.DBusException
      Unregister a PropertiesChanged callback handler on the DBusConnection.
      Parameters:
      _handler - callback class instance
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on error
    • registerSignalHandler

      public <T extends org.freedesktop.dbus.messages.DBusSignal> void registerSignalHandler(org.freedesktop.dbus.handlers.AbstractSignalHandlerBase<T> _handler) throws org.freedesktop.dbus.exceptions.DBusException
      Register a signal handler callback on the connection.
      Type Parameters:
      T - a DBusSignal or a subclass of it
      Parameters:
      _handler - callback class extending AbstractSignalHandlerBase
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on DBus error
    • unRegisterSignalHandler

      public <T extends org.freedesktop.dbus.messages.DBusSignal> void unRegisterSignalHandler(org.freedesktop.dbus.handlers.AbstractSignalHandlerBase<T> _handler) throws org.freedesktop.dbus.exceptions.DBusException
      Unregister a signal handler callback on the connection.
      Type Parameters:
      T - a DBusSignal or a subclass of it
      Parameters:
      _handler - callback class extending AbstractSignalHandlerBase
      Throws:
      org.freedesktop.dbus.exceptions.DBusException - on DBus error
    • getDbusConnection

      public org.freedesktop.dbus.connections.impl.DBusConnection getDbusConnection()
      Get the DBusConnection provided in constructor.
      Returns:
      DBusConnection