public final class BluetoothHost extends Object
The BluetoothHost#getDefaultHost method can be used to obtain a BluetoothHost instance,
which can be used to perform Bluetooth operations on a device.
You can use enableBt() to enable Bluetooth, disableBt() to disable Bluetooth,
startBtDiscovery() to start a device scan, and cancelBtDiscovery() to cancel a device scan.
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
BLE_CAP_2M_PHY |
Indicates that the Bluetooth chip of the current device supports the BLE 2M PHY feature.
|
static int |
BLE_CAP_CODED_PHY |
Indicates that the Bluetooth chip of the current device supports the BLE coded PHY feature.
|
static int |
BLE_CAP_EXTENDED_ADV |
Indicates that the Bluetooth chip of the current device supports the BLE extended advertising feature.
|
static int |
BLE_CAP_MULTIPLE_ADV |
Indicates that the Bluetooth chip of the current device supports the BLE multiple advertising feature.
|
static int |
BLE_CAP_OFFLOADED_FILTER |
Indicates that the Bluetooth chip of the current device supports the BLE offloaded filter feature.
|
static int |
BLE_CAP_OFFLOADED_SCAN_BATCH |
Indicates that the Bluetooth chip of the current device supports the BLE offloaded scan batch feature.
|
static int |
BLE_CAP_PERIODIC_ADV |
Indicates that the Bluetooth chip of the current device supports the BLE periodic advertising feature.
|
static String |
EVENT_HOST_DISCOVERY_FINISHED |
Indicates the event reported when a Bluetooth scan is complete.
|
static String |
EVENT_HOST_DISCOVERY_STARTED |
Indicates the event reported when a Bluetooth scan starts.
|
static String |
EVENT_HOST_NAME_UPDATE |
Indicates the event reported when the Bluetooth name changes.
|
static String |
EVENT_HOST_SCAN_MODE_UPDATE |
Indicates the event reported when the Bluetooth scan mode changes.
|
static String |
EVENT_HOST_STATE_UPDATE |
Indicates the event reported when the Bluetooth state changes.
|
static String |
HOST_PARAM_CUR_STATE |
Indicates the Bluetooth state after a state update.
|
static String |
HOST_PARAM_DISCOVERABLE_TERM |
Indicates the remaining duration in seconds, in which the host is discoverable.
|
static String |
HOST_PARAM_HOST_NAME |
Indicates the bluetooth name after a host name changes.
|
static String |
HOST_PARAM_PRE_STATE |
Indicates the Bluetooth state after a state update.
|
static String |
HOST_PARAM_SCAN_METHOD |
Indicates the scan mode after a scan mode update.
|
static int |
SCAN_MODE_CONNECTABLE |
Indicates that the Bluetooth inquiry scan is disabled and page scan is enabled on the current device.
|
static int |
SCAN_MODE_CONNECTABLE_DISCOVERABLE |
Indicates that Bluetooth inquiry scan and page scan are enabled on the current device.
|
static int |
SCAN_MODE_NONE |
Indicates that Bluetooth inquiry scan and page scan are disabled on the current device.
|
static int |
STATE_BLE_ON |
Indicates that Bluetooth BLE is enabled on the current device.
|
static int |
STATE_BLE_TURNING_OFF |
Indicates that Bluetooth BLE is being disabled on the current device.
|
static int |
STATE_BLE_TURNING_ON |
Indicates that Bluetooth BLE is being disabled on the current device.
|
static int |
STATE_OFF |
Indicates that Bluetooth is disabled on the current device.
|
static int |
STATE_ON |
Indicates that Bluetooth BR is enabled on the current device.
|
static int |
STATE_TURNING_OFF |
Indicates that Bluetooth BR is being disabled on the current device.
|
static int |
STATE_TURNING_ON |
Indicates that Bluetooth BR is being enabled on the current device.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
cancelBtDiscovery() |
Stops Bluetooth device scanning.
|
boolean |
disableBt() |
Disables Bluetooth on a device.
|
boolean |
enableBt() |
Enables Bluetooth on a device.
|
int |
getBleCapabilities() |
Obtains the Bluetooth BLE capability of a device.
|
int |
getBleMaxAdvertisingDataLength() |
Obtains the maximum length of advertising data supported by the Bluetooth BLE of a device.
|
int |
getBtScanMode() |
Obtains the Bluetooth scanning mode of a device.
|
int |
getBtState() |
Obtains the Bluetooth status of a device.
|
static BluetoothHost |
getDefaultHost(Context context) |
Obtains a default
BluetoothHost instance to perform Bluetooth operations on a device. |
Optional<String> |
getLocalName() |
Obtains the Bluetooth friendly name of a device.
|
List<BluetoothRemoteDevice> |
getPairedDevices() |
Obtains the list of Bluetooth devices that have been paired with the current device.
|
int |
getProfileConnState(int profile) |
Obtains the status of the connection between current device's Bluetooth profile and other Bluetooth devices.
|
BluetoothRemoteDevice |
getRemoteDev(String addr) |
Obtains the
BluetoothRemoteDevice object with a specified Bluetooth address. |
boolean |
isBtDiscovering() |
Checks whether Bluetooth scanning is ongoing on a device.
|
static boolean |
isValidBluetoothAddr(String addr) |
Checks whether the Bluetooth address is valid.
|
boolean |
setBtScanMode(int mode,
int duration) |
Sets the Bluetooth scan mode for a device.
|
boolean |
setDevicePairingConfirmation(BluetoothRemoteDevice device,
boolean accept) |
Sets the confirmation of pairing with a certain device.
|
boolean |
setLocalName(String name) |
Sets the Bluetooth friendly name of a device.
|
boolean |
startBtDiscovery() |
Starts scanning Bluetooth devices.
|
public static final int BLE_CAP_2M_PHY
public static final int BLE_CAP_CODED_PHY
public static final int BLE_CAP_EXTENDED_ADV
public static final int BLE_CAP_MULTIPLE_ADV
public static final int BLE_CAP_OFFLOADED_FILTER
public static final int BLE_CAP_OFFLOADED_SCAN_BATCH
public static final int BLE_CAP_PERIODIC_ADV
public static final String EVENT_HOST_DISCOVERY_FINISHED
EVENT_HOST_DISCOVERY_FINISHED does not contain any other information.
Output: none
EVENT_HOST_DISCOVERY_FINISHED,
常量字段值public static final String EVENT_HOST_DISCOVERY_STARTED
EVENT_HOST_DISCOVERY_STARTED does not contain any other information.
Output: none
EVENT_HOST_DISCOVERY_STARTED,
常量字段值public static final String EVENT_HOST_NAME_UPDATE
EVENT_HOST_NAME_UPDATE contains detailed information
about the Bluetooth name change.
Output: HOST_PARAM_HOST_NAME indicates the Bluetooth name.
HOST_PARAM_HOST_NAME,
常量字段值public static final String EVENT_HOST_SCAN_MODE_UPDATE
EVENT_HOST_SCAN_MODE_UPDATE contains detailed information about the change
in the Bluetooth scan mode.
Output: HOST_PARAM_SCAN_METHOD indicates the Bluetooth scan mode.
EVENT_HOST_SCAN_MODE_UPDATE,
HOST_PARAM_SCAN_METHOD,
常量字段值public static final String EVENT_HOST_STATE_UPDATE
EVENT_HOST_STATE_UPDATE contains detailed information
about the Bluetooth state change.
Output: HOST_PARAM_PRE_STATE indicates the Bluetooth state
before the change;
HOST_PARAM_CUR_STATE indicates the Bluetooth state after the change.
EVENT_HOST_STATE_UPDATE,
HOST_PARAM_PRE_STATE,
HOST_PARAM_CUR_STATE,
常量字段值public static final String HOST_PARAM_CUR_STATE
STATE_OFF, STATE_TURNING_ON,
STATE_ON, or STATE_TURNING_OFF.public static final String HOST_PARAM_DISCOVERABLE_TERM
public static final String HOST_PARAM_HOST_NAME
public static final String HOST_PARAM_PRE_STATE
STATE_OFF, STATE_TURNING_ON,
STATE_ON, or STATE_TURNING_OFF.public static final String HOST_PARAM_SCAN_METHOD
SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE, or SCAN_MODE_CONNECTABLE_DISCOVERABLE.public static final int SCAN_MODE_CONNECTABLE
public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE
public static final int SCAN_MODE_NONE
public static final int STATE_BLE_ON
public static final int STATE_BLE_TURNING_OFF
public static final int STATE_BLE_TURNING_ON
public static final int STATE_OFF
public static final int STATE_ON
public static final int STATE_TURNING_OFF
public static final int STATE_TURNING_ON
public static BluetoothHost getDefaultHost(Context context)
BluetoothHost instance to perform Bluetooth operations on a device.context - Ability context that contains package name.BluetoothHost instance.public boolean enableBt()
This method is used to start Bluetooth hardware and all Bluetooth services, such as A2DP and HFP.
This is an asynchronous method. The result is returned immediately after your application calls the method.
Your application must listen to EVENT_HOST_STATE_UPDATE broadcast
for the change of the Bluetooth status.
true if Bluetooth is being enabled; returns false if an error occurs.public boolean disableBt()
This method is used to disable Bluetooth hardware and all Bluetooth services, such as A2DP and HFP.
This is an asynchronous method. The result is returned immediately after your application calls the method.
Your application must listen to EVENT_HOST_STATE_UPDATE broadcast
for the change of the Bluetooth status.
true if Bluetooth is being disabled; returns false if an error occurs.public int getBtState()
STATE_OFF, STATE_TURNING_ON,
STATE_ON, STATE_TURNING_OFF, STATE_BLE_TURNING_ON, STATE_BLE_ON,
or STATE_BLE_TURNING_OFF.public BluetoothRemoteDevice getRemoteDev(String addr)
BluetoothRemoteDevice object with a specified Bluetooth address.
The letters in the Bluetooth address must be in uppercase.
addr - Indicates a valid Bluetooth address.BluetoothRemoteDevice instance with the specified address.IllegalArgumentException - Throws this exception if the Bluetooth address is invalid.public Optional<String> getLocalName()
The name is visible to other Bluetooth devices.
Optional#empty() if an exception occurs.public boolean setLocalName(String name)
This is an asynchronous method. Your application must listen to the EVENT_HOST_NAME_UPDATE broadcast
for the setting result.
name - Indicates a valid Bluetooth name.true if the Bluetooth name is set successfully; returns false otherwise.public boolean startBtDiscovery()
This is an asynchronous method.
An inquiry scan takes about 12.8s. Your application must listen to EVENT_HOST_DISCOVERY_STARTED and
EVENT_HOST_DISCOVERY_STARTED broadcasts for the start and stop of the scan, and listen to the
BluetoothRemoteDevice#EVENT_DEVICE_DISCOVERED broadcast for whether Bluetooth devices
are detected in the scan.
true if the scan is started successfully; returns false otherwise.public boolean cancelBtDiscovery()
true if scanning is stopped successfully; returns false otherwise.public boolean isBtDiscovering()
true if Bluetooth scanning is ongoing; returns false otherwise.public int getBtScanMode()
SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE,
or SCAN_MODE_CONNECTABLE_DISCOVERABLE.public int getProfileConnState(int profile)
Currently, this method applies only to the ProfileBase#HFP and ProfileBase#A2DP profiles.
profile - Indicates the profile, either ProfileBase#HFP or ProfileBase#A2DP.ProfileBase#STATE_DISCONNECTED,
ProfileBase#STATE_CONNECTING, ProfileBase#STATE_CONNECTED,
or ProfileBase#STATE_DISCONNECTING.public List<BluetoothRemoteDevice> getPairedDevices()
The return value of this method is valid only if the Bluetooth status of the current device is
STATE_ON.
BluetoothRemoteDevice.public int getBleCapabilities()
BLE_CAP_2M_PHY,
BLE_CAP_CODED_PHY, BLE_CAP_EXTENDED_ADV, BLE_CAP_PERIODIC_ADV,
BLE_CAP_MULTIPLE_ADV, BLE_CAP_OFFLOADED_FILTER, or BLE_CAP_OFFLOADED_SCAN_BATCH.public int getBleMaxAdvertisingDataLength()
public boolean setBtScanMode(int mode,
int duration)
mode - Indicates the Bluetooth scan mode to set. It can be:
SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE, or SCAN_MODE_CONNECTABLE_DISCOVERABLE.duration - Indicates the duration in seconds, in which the host is discoverable.true if the Bluetooth scan mode is set; returns false otherwise.public boolean setDevicePairingConfirmation(BluetoothRemoteDevice device, boolean accept)
device - a device need to be accepted.accept - Indicates the accept value.true if the pairing confirmation is set; returns false otherwise..public static boolean isValidBluetoothAddr(String addr)
Letters in the Bluetooth address string must be uppercase, for example, AA:BB:03:04:05.
addr - Indicates the Bluetooth address in string.true if the Bluetooth address is valid; returns false otherwise.