public class BluetoothCentralManager
extends java.lang.Object
Constructor and Description |
---|
BluetoothCentralManager(@NotNull android.content.Context context,
@NotNull BluetoothCentralManagerCallback bluetoothCentralManagerCallback,
@NotNull android.os.Handler handler)
Construct a new BluetoothCentralManager object
|
Modifier and Type | Method and Description |
---|---|
void |
autoConnectPeripheral(@NotNull BluetoothPeripheral peripheral,
@NotNull BluetoothPeripheralCallback peripheralCallback)
Automatically connect to a peripheral when it is advertising.
|
void |
autoConnectPeripheralsBatch(@NotNull java.util.Map<BluetoothPeripheral,BluetoothPeripheralCallback> batch)
Autoconnect to a batch of peripherals.
|
void |
cancelConnection(@NotNull BluetoothPeripheral peripheral)
Cancel an active or pending connection for a peripheral.
|
void |
close()
Closes BluetoothCentralManager and cleans up internals.
|
void |
connectPeripheral(@NotNull BluetoothPeripheral peripheral,
@NotNull BluetoothPeripheralCallback peripheralCallback)
Connect to a known peripheral immediately.
|
@NotNull java.util.List<BluetoothPeripheral> |
getConnectedPeripherals()
Get the list of connected peripherals.
|
@NotNull BluetoothPeripheral |
getPeripheral(@NotNull java.lang.String peripheralAddress)
Get a peripheral object matching the specified mac address.
|
boolean |
isBluetoothEnabled()
Check if Bluetooth is enabled
|
boolean |
isScanning()
Check if a scanning is active
|
boolean |
removeBond(@NotNull java.lang.String peripheralAddress)
Remove bond for a peripheral.
|
void |
scanForPeripherals()
Scan for any peripheral that is advertising.
|
void |
scanForPeripheralsUsingFilters(@NotNull java.util.List<android.bluetooth.le.ScanFilter> filters)
Scan for any peripheral that matches the supplied filters
|
void |
scanForPeripheralsWithAddresses(@NotNull java.lang.String[] peripheralAddresses)
Scan for peripherals that have any of the specified peripheral mac addresses.
|
void |
scanForPeripheralsWithNames(@NotNull java.lang.String[] peripheralNames)
Scan for peripherals with advertisement names containing any of the specified peripheral names.
|
void |
scanForPeripheralsWithServices(@NotNull java.util.UUID[] serviceUUIDs)
Scan for peripherals that advertise at least one of the specified service UUIDs.
|
boolean |
setPinCodeForPeripheral(@NotNull java.lang.String peripheralAddress,
@NotNull java.lang.String pin)
Set a fixed PIN code for a peripheral that asks for a PIN code during bonding.
|
void |
setScanMode(@NotNull ScanMode scanMode)
Set the default scanMode.
|
void |
startPairingPopupHack()
Make the pairing popup appear in the foreground by doing a 1 sec discovery.
|
void |
stopScan()
Stop scanning for peripherals.
|
public BluetoothCentralManager(@NotNull @NotNull android.content.Context context, @NotNull @NotNull BluetoothCentralManagerCallback bluetoothCentralManagerCallback, @NotNull @NotNull android.os.Handler handler)
context
- Android application environment.bluetoothCentralManagerCallback
- the callback to call for updateshandler
- Handler to use for callbacks.public void close()
public void setScanMode(@NotNull @NotNull ScanMode scanMode)
scanMode
- the scanMode to setpublic void scanForPeripheralsWithServices(@NotNull @NotNull java.util.UUID[] serviceUUIDs)
serviceUUIDs
- an array of service UUIDspublic void scanForPeripheralsWithNames(@NotNull @NotNull java.lang.String[] peripheralNames)
Substring matching is used so only a partial peripheral names has to be supplied.
peripheralNames
- array of partial peripheral namespublic void scanForPeripheralsWithAddresses(@NotNull @NotNull java.lang.String[] peripheralAddresses)
peripheralAddresses
- array of peripheral mac addresses to scan forpublic void scanForPeripheralsUsingFilters(@NotNull @NotNull java.util.List<android.bluetooth.le.ScanFilter> filters)
filters
- A list of ScanFilterspublic void scanForPeripherals()
public void stopScan()
public boolean isScanning()
public void connectPeripheral(@NotNull @NotNull BluetoothPeripheral peripheral, @NotNull @NotNull BluetoothPeripheralCallback peripheralCallback)
peripheral
- BLE peripheral to connect withpublic void autoConnectPeripheral(@NotNull @NotNull BluetoothPeripheral peripheral, @NotNull @NotNull BluetoothPeripheralCallback peripheralCallback)
peripheral
- the peripheralpublic void cancelConnection(@NotNull @NotNull BluetoothPeripheral peripheral)
peripheral
- the peripheralpublic void autoConnectPeripheralsBatch(@NotNull @NotNull java.util.Map<BluetoothPeripheral,BluetoothPeripheralCallback> batch)
Use this function to autoConnect to a batch of peripherals, instead of calling autoConnect on each of them. Calling autoConnect on many peripherals may cause Android scanning limits to kick in, which is avoided by using autoConnectPeripheralsBatch.
batch
- the map of peripherals and their callbacks to autoconnect to@NotNull public @NotNull BluetoothPeripheral getPeripheral(@NotNull @NotNull java.lang.String peripheralAddress)
peripheralAddress
- mac address@NotNull public @NotNull java.util.List<BluetoothPeripheral> getConnectedPeripherals()
public boolean isBluetoothEnabled()
public boolean setPinCodeForPeripheral(@NotNull @NotNull java.lang.String peripheralAddress, @NotNull @NotNull java.lang.String pin)
This PIN code will be used to programmatically bond with the peripheral when it asks for a PIN code. The normal PIN popup will not appear anymore.
Note that this only works for peripherals with a fixed PIN code.peripheralAddress
- the address of the peripheralpin
- the 6 digit PIN code as a string, e.g. "123456"public boolean removeBond(@NotNull @NotNull java.lang.String peripheralAddress)
peripheralAddress
- the address of the peripheralpublic void startPairingPopupHack()
If the pairing popup is shown within 60 seconds, it will be shown in the foreground.