public class DeviceManager
extends java.lang.Object
The device must be previously bonded, as this class does not initiate discovery.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
KNOWN_BLUETOOTH_DEVICE_PREF_KEY |
static java.lang.String |
KNOWN_BLUETOOTH_DEVICE_PREFERENCES |
static java.lang.String |
LAST_CONNECTED_BLUETOOTH_DEVICE_PREF_KEY |
static int |
MAX_WRITE_BUFFER_CAPACITY |
static int |
PACKET_SENDING_WAIT_TIME_MS |
static java.util.UUID |
RFCOMM_UUID |
Constructor and Description |
---|
DeviceManager(android.content.Context context)
The DeviceManager requires an Android Context in order to send the intent
to enable Bluetooth if it isn't already on.
|
Modifier and Type | Method and Description |
---|---|
android.bluetooth.BluetoothSocket |
connect(android.bluetooth.BluetoothDevice device) |
android.bluetooth.BluetoothSocket |
connect(java.lang.String targetAddress)
Connect to the target device and open a socket.
|
android.bluetooth.BluetoothGatt |
connectBLE(android.bluetooth.BluetoothDevice device) |
android.bluetooth.BluetoothGatt |
connectBLE(java.lang.String address) |
java.util.Set<android.bluetooth.BluetoothDevice> |
getCandidateDevices() |
android.bluetooth.BluetoothDevice |
getLastConnectedDevice() |
java.util.Set<android.bluetooth.BluetoothDevice> |
getPairedDevices() |
boolean |
isBLEConnected()
This is used to get the connection status using gatt callback
|
boolean |
isBLEDevice(android.bluetooth.BluetoothDevice device) |
boolean |
isBLEDevice(java.lang.String address) |
boolean |
isBLEDisconnected() |
boolean |
isConnecting() |
android.bluetooth.BluetoothServerSocket |
listen() |
void |
startDiscovery() |
void |
stop()
Immediately cancel any pending Bluetooth operations.
|
void |
storeLastConnectedDevice(android.bluetooth.BluetoothDevice device) |
boolean |
writeCharacteristicToBLE(byte[] bytes) |
public static final java.lang.String KNOWN_BLUETOOTH_DEVICE_PREFERENCES
public static final java.lang.String KNOWN_BLUETOOTH_DEVICE_PREF_KEY
public static final java.lang.String LAST_CONNECTED_BLUETOOTH_DEVICE_PREF_KEY
public static final java.util.UUID RFCOMM_UUID
public static final int MAX_WRITE_BUFFER_CAPACITY
public static final int PACKET_SENDING_WAIT_TIME_MS
public DeviceManager(android.content.Context context) throws BluetoothException
BluetoothException
public void startDiscovery()
public android.bluetooth.BluetoothServerSocket listen()
public android.bluetooth.BluetoothSocket connect(java.lang.String targetAddress) throws BluetoothException
Returns a socket connected to the device.
BluetoothException
public android.bluetooth.BluetoothSocket connect(android.bluetooth.BluetoothDevice device) throws BluetoothException
BluetoothException
public void stop()
The BluetoothSocket.connect() function blocks while waiting for a connection, but it's thread safe and we can cancel that by calling close() on it at any time.
Importantly we don't want to close the socket any other time, because we want to leave that up to the user of the socket - if you call close() twice, or close Input/Output streams associated with the socket simultaneously, it can cause a segfault due to a bug in some Android Bluetooth stacks. Awesome!
public java.util.Set<android.bluetooth.BluetoothDevice> getPairedDevices()
public void storeLastConnectedDevice(android.bluetooth.BluetoothDevice device)
public android.bluetooth.BluetoothDevice getLastConnectedDevice()
public java.util.Set<android.bluetooth.BluetoothDevice> getCandidateDevices()
public boolean isConnecting()
public boolean isBLEDevice(java.lang.String address)
public boolean isBLEDevice(android.bluetooth.BluetoothDevice device)
public android.bluetooth.BluetoothGatt connectBLE(java.lang.String address) throws BluetoothException
BluetoothException
public android.bluetooth.BluetoothGatt connectBLE(android.bluetooth.BluetoothDevice device) throws BluetoothException
BluetoothException
public boolean isBLEConnected()
public boolean isBLEDisconnected()
public boolean writeCharacteristicToBLE(byte[] bytes)