Package com.github.hypfvieh.bluetooth
Class DeviceManager
java.lang.Object
com.github.hypfvieh.bluetooth.DeviceManager
The 'main' class to get access to all DBus/bluez related objects.
- Author:
- hypfvieh
-
Method Summary
Modifier and TypeMethodDescriptionvoidClose current connection.static DeviceManagercreateInstance(boolean _sessionConnection) Create a newDeviceManagerusing the UnixDomainSockets and use either the global SYSTEM interface or create a new interface just for this session (if _sessionConnection = true).static DeviceManagercreateInstance(String _address) Create a newDeviceManagerinstance using the given DBus address (e.g. tcp://127.0.0.1:13245)voidGets all devices found by the given adapter and published by bluez using DBus Introspection API.Get the current adapter in use.getAdapter(String _ident) Find an adapter by the given identifier (either MAC or device name).Returns all found bluetooth adapters.org.freedesktop.dbus.connections.impl.DBusConnectionGet the DBusConnection provided in constructor.Get all bluetooth devices connected to the defaultAdapter.getDevices(boolean _doNotScan) Get all bluetooth devices connected to the defaultAdapter.getDevices(String _adapterMac) Get all bluetooth devices connected to the adapter with the given MAC address.getDevices(String _adapterMac, boolean _doNotScan) Get all bluetooth devices connected to the adapter with the given MAC address.static DeviceManagerGet the created instance.voidregisterPropertyHandler(org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler _handler) Register a PropertiesChanged callback handler on the DBusConnection.<T extends org.freedesktop.dbus.messages.DBusSignal>
voidregisterSignalHandler(org.freedesktop.dbus.handlers.AbstractSignalHandlerBase<T> _handler) Register a signal handler callback on the connection.Search for all bluetooth adapters connected to this machine.scanForBluetoothDevices(int _timeout) Scan for bluetooth devices using the default adapter.scanForBluetoothDevices(String _adapter, int _timeoutMs) Scan for Bluetooth devices for on the given adapter.voidsetDefaultAdapter(BluetoothAdapter _adapter) Setup the default bluetooth adapter to use by giving an adapter object.voidsetDefaultAdapter(String _adapterMac) Setup the default bluetooth adapter to use by giving the adapters MAC address.voidsetLazyScan(boolean _scan) Enable/disable lazy scanning.voidsetScanFilter(Map<DiscoveryFilter, Object> _filter) Setup bluetooth scan/discovery filter.voidunRegisterPropertyHandler(org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler _handler) Unregister a PropertiesChanged callback handler on the DBusConnection.<T extends org.freedesktop.dbus.messages.DBusSignal>
voidunRegisterSignalHandler(org.freedesktop.dbus.handlers.AbstractSignalHandlerBase<T> _handler) Unregister a signal handler callback on the connection.
-
Method Details
-
createInstance
public static DeviceManager createInstance(boolean _sessionConnection) throws org.freedesktop.dbus.exceptions.DBusException Create a newDeviceManagerusing 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 newDeviceManagerinstance 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
Get the created instance.- Returns:
DeviceManager, never null
-
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
Scan for bluetooth devices using the default adapter.- Parameters:
_timeout- timout to use for scanning- Returns:
- list of found
BluetoothDevice
-
scanForBluetoothDevices
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
Gets all devices found by the given adapter and published by bluez using DBus Introspection API.- Parameters:
adapter- bluetooth adapter
-
setScanFilter
public void setScanFilter(Map<DiscoveryFilter, Object> _filter) throws BluezInvalidArgumentsException, BluezNotReadyException, BluezNotSupportedException, BluezFailedExceptionSetup bluetooth scan/discovery filter.- Parameters:
_filter- filter to apply- Throws:
BluezInvalidArgumentsException- when argument is invalidBluezNotReadyException- when bluez not readyBluezNotSupportedException- when operation not supportedBluezFailedException- on failure
-
getAdapter
Get the current adapter in use.- Returns:
- the adapter currently in use, maybe null
-
getAdapter
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
Returns all found bluetooth adapters. Will query for adapters ifscanForBluetoothAdapters()was not called before.- Returns:
- list, maybe empty
-
getDevices
Get all bluetooth devices connected to the defaultAdapter.- Returns:
- list - maybe empty
-
getDevices
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
Get all bluetooth devices connected to the adapter with the given MAC address.- Parameters:
_adapterMac- adapters MAC address- Returns:
- list - maybe empty
-
getDevices
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
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
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- aDBusSignalor a subclass of it- Parameters:
_handler- callback class extendingAbstractSignalHandlerBase- 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- aDBusSignalor a subclass of it- Parameters:
_handler- callback class extendingAbstractSignalHandlerBase- 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
-