Package eu.woolplatform.utils.bluetooth
Class BluetoothAdapter
- java.lang.Object
-
- eu.woolplatform.utils.bluetooth.BluetoothAdapter
-
public abstract class BluetoothAdapter extends Object
This class provides a common interface to the Bluetooth system. Normally an application sets an implementation withsetInstance(). Any software components that need the Bluetooth system can then callgetInstance()to get access. There are two known implementations: for standard Java and for Android. Before using the adapter, you may need to callprepareThread()and you must callinitDefaultAdapter().
-
-
Constructor Summary
Constructors Constructor Description BluetoothAdapter()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidcancelDiscoverDevices()Cancels the discovery of remote Bluetooth devices.abstract voidclose()Releases any resources used by this adapter.abstract voiddisable(BluetoothListener listener)Disables the Bluetooth system.abstract voiddiscoverDevices(DiscoverDevicesListener listener)Starts the discovery of remote Bluetooth devices in the vicinity of this local Bluetooth device.abstract voidenable(BluetoothListener listener)Enables the Bluetooth system.static BluetoothAdaptergetInstance()Returns a Bluetooth adapter implementation.abstract BluetoothDevice[]getPairedDevices()Returns the remote Bluetooth devices that have been paired with this local Bluetooth device.abstract BluetoothDevicegetRemoteDevice(String address)Returns the remote Bluetooth device with the specified address.abstract voidinitDefaultAdapter()Initialises the Bluetooth adapter.abstract booleanisEnabled()Returns true if the Bluetooth system is enabled.abstract BluetoothServerSocketopenServerSocket(String uuid)Opens a Bluetooth server socket for the specified service on this local Bluetooth device.abstract voidprepareThread()The Android implementation requires thatLooper.prepare()is called on a thread before the Bluetooth adapter is used.static voidsetInstance(BluetoothAdapter instance)Sets a Bluetooth adapater implementation.
-
-
-
Method Detail
-
getInstance
public static BluetoothAdapter getInstance()
Returns a Bluetooth adapter implementation. It returns the adapter that was set withsetInstance(). If no adapter was set, this method returns null.- Returns:
- the Bluetooth adapter
-
setInstance
public static void setInstance(BluetoothAdapter instance)
Sets a Bluetooth adapater implementation. This adapter will be returned bygetInstance().- Parameters:
instance- the Bluetooth adapter
-
prepareThread
public abstract void prepareThread()
The Android implementation requires thatLooper.prepare()is called on a thread before the Bluetooth adapter is used. If you create a new thread specifically for Bluetooth operations and you want that code to be independent of Android, you can call this method, so that the Android implementation will callLooper.prepare(). If you need this, you must call this method beforeinitDefaultAdapter().
-
initDefaultAdapter
public abstract void initDefaultAdapter()
Initialises the Bluetooth adapter. This method must be called before using the adapter. The Android implementation requires thatLooper.prepare()is called on a thread before the adapter is used. SeeprepareThread()for more information. If you need this, you must callprepareThread()before calling this method.
-
enable
public abstract void enable(BluetoothListener listener)
Enables the Bluetooth system. This method returns immediately and will notify the specified listener when the process has finished.- Parameters:
listener- the listener
-
disable
public abstract void disable(BluetoothListener listener)
Disables the Bluetooth system. This method returns immediately and will notify the specified listener when the process has finished.- Parameters:
listener- the listener
-
getPairedDevices
public abstract BluetoothDevice[] getPairedDevices()
Returns the remote Bluetooth devices that have been paired with this local Bluetooth device.- Returns:
- the paired devices
-
discoverDevices
public abstract void discoverDevices(DiscoverDevicesListener listener) throws Exception
Starts the discovery of remote Bluetooth devices in the vicinity of this local Bluetooth device. Events in the discovery process are sent to the specified listener.- Parameters:
listener- the listener- Throws:
Exception- if the discovery can't be started
-
cancelDiscoverDevices
public abstract void cancelDiscoverDevices()
Cancels the discovery of remote Bluetooth devices.
-
openServerSocket
public abstract BluetoothServerSocket openServerSocket(String uuid) throws IOException
Opens a Bluetooth server socket for the specified service on this local Bluetooth device. The UUID should be given in the format 00000000-0000-0000-0000-000000000000. That is 32 hexadecimal characters in four groups with lengths 8 - 4 - 4 - 4 - 12.The common serial port service has this UUID: 00001101-0000-1000-8000-00805F9B34FB
- Parameters:
uuid- the UUID of the service- Returns:
- the Bluetooth server socket
- Throws:
IOException- if the socket can't be opened
-
isEnabled
public abstract boolean isEnabled()
Returns true if the Bluetooth system is enabled.- Returns:
- true if the Bluetooth system is enabled, false otherwise
-
getRemoteDevice
public abstract BluetoothDevice getRemoteDevice(String address) throws Exception
Returns the remote Bluetooth device with the specified address. The address should be given in the format 00:00:00:00:00:00. That is 12 hexadecimal characters in groups of two.- Parameters:
address- the address- Returns:
- the remote device
- Throws:
Exception- if the remote device can't be created (for example if Bluetooth is not available)
-
close
public abstract void close()
Releases any resources used by this adapter.
-
-