public class BlePeripheralDevice extends Object
Related operations include connecting to, disconnecting from, and disabling a BLE peripheral device.
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
CONNECTION_PRIORITY_HIGH |
Connection parameter update - Request a high priority, low latency connection.
|
static int |
CONNECTION_PRIORITY_LOW |
Connection parameter update - Request low power, reduced data rate connection parameters.
|
static int |
CONNECTION_PRIORITY_NORMAL |
Connection parameter update - Use the connection parameters recommended by the
Bluetooth SIG.
|
static int |
OPERATION_SUCC |
Indicates that the operation succeeds.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
close() |
Disables a BLE peripheral device.
|
boolean |
connect(boolean isAutoConnect,
BlePeripheralCallback callback) |
Connects to a BLE peripheral device.
|
static BlePeripheralDevice |
createInstance(String address) |
Creates a
BlePeripheralDevice object with a specified MAC address. |
boolean |
disconnect() |
Disconnects from or stops an ongoing connection to a BLE peripheral device.
|
boolean |
discoverServices() |
Starts discovering services.
|
String |
getDeviceAddr() |
Obtains the MAC address of this
BlePeripheralDevice object. |
Optional<String> |
getDeviceName() |
Obtains the name of this
BlePeripheralDevice object. |
Optional<GattService> |
getService(UUID uuid) |
Obtains a specified service of the current BLE peripheral device.
|
List<GattService> |
getServices() |
Obtains the list of services of a BLE peripheral device.
|
boolean |
readCharacteristic(GattCharacteristic characteristic) |
Reads the characteristic of a BLE peripheral device.
|
boolean |
readDescriptor(GattDescriptor descriptor) |
Reads the descriptor of a BLE peripheral device.
|
boolean |
readRemoteRssiValue() |
Reads the RSSI value of this BLE peripheral device.
|
boolean |
requestBleConnectionPriority(int connPriority) |
Requests the connection priority of this BLE peripheral device.
|
boolean |
requestBleMtuSize(int mtu) |
Request the mtu size of a BLE peripheral device.
|
boolean |
setNotifyCharacteristic(GattCharacteristic characteristic,
boolean enable) |
Enables or disables notification of a characteristic.
|
boolean |
writeCharacteristic(GattCharacteristic characteristic) |
Writes the characteristic of a BLE peripheral device.
|
boolean |
writeDescriptor(GattDescriptor descriptor) |
Writes the descriptor of a BLE peripheral device.
|
public static final int CONNECTION_PRIORITY_HIGH
CONNECTION_PRIORITY_NORMAL connection parameters to reduce
energy use.public static final int CONNECTION_PRIORITY_LOW
public static final int CONNECTION_PRIORITY_NORMAL
public static final int OPERATION_SUCC
This constant applies to all operations supported by BLE peripheral devices.
public static BlePeripheralDevice createInstance(String address)
BlePeripheralDevice object with a specified MAC address.address - Indicates the MAC address.BlePeripheralDevice object, which can be used to connect to or disconnect
from a BLE peripheral device.public String getDeviceAddr()
BlePeripheralDevice object.public boolean connect(boolean isAutoConnect,
BlePeripheralCallback callback)
The BlePeripheralCallback#connectionStateChangeEvent(int) method is called to
return the connection result.
isAutoConnect - Specifies whether to automatically connect to the device.callback - Indicates the BlePeripheralCallback.true if the connection is established; returns false otherwise.public boolean discoverServices()
This method is asynchronous. The BlePeripheralCallback#servicesDiscoveredEvent(int) method is called to
return the service discovery result.
true if services are discovered; returns false otherwise.public List<GattService> getServices()
Before calling this method, you must call the discoverServices() method to start service discovery
and ensure that the BlePeripheralCallback#servicesDiscoveredEvent(int) method is called.
public Optional<GattService> getService(UUID uuid)
uuid - Indicates the UUID of the service.Optional if the device supports the service with the specified UUID;
returns Optional#empty() otherwise.public boolean disconnect()
true if the disconnection process starts; returns false otherwise.public boolean close()
This method deregisters the device and clears the registered callbacks and handles.
true if the device is disabled; returns false otherwise.public boolean readCharacteristic(GattCharacteristic characteristic)
This method is asynchronous.
The BlePeripheralCallback#characteristicReadEvent(GattCharacteristic, int) method is called
to return the read result.
characteristic - Indicates the characteristic to read.true if the characteristic is read successfully; returns false otherwise.public boolean writeCharacteristic(GattCharacteristic characteristic)
This method is asynchronous.
The BlePeripheralCallback#characteristicWriteEvent(GattCharacteristic, int) method is called
to return the write result.
characteristic - Indicates the characteristic to write.true if the characteristic is written successfully; returns false otherwise.public boolean setNotifyCharacteristic(GattCharacteristic characteristic, boolean enable)
enable - Specifies whether to enable notification of the characteristic. The value true indicates
that notification is enabled, and the value false indicates that notification is disabled.characteristic - Indicates the characteristic to notify.true if notification of the characteristic is enabled or disabled;
returns false otherwise.public boolean readDescriptor(GattDescriptor descriptor)
The BlePeripheralCallback#descriptorReadEvent(GattDescriptor, int) method is called to
return the read result.
descriptor - Indicates the descriptor to read.true if the descriptor is read successfully; returns false otherwise.public boolean writeDescriptor(GattDescriptor descriptor)
The BlePeripheralCallback#descriptorWriteEvent(GattDescriptor, int) method is called to
return the write result.
descriptor - Indicates the descriptor to write.true if the descriptor is written successfully; returns false otherwise.public boolean readRemoteRssiValue()
true if the RSSI value is read; returns false otherwise.public boolean requestBleConnectionPriority(int connPriority)
Three connection priorities are available: CONNECTION_PRIORITY_NORMAL,
CONNECTION_PRIORITY_HIGH, and CONNECTION_PRIORITY_LOW.
connPriority - Indicates the connection priority to request.true if the request is successful; returns false otherwise.public boolean requestBleMtuSize(int mtu)
mtu - the max size.true if the request mtu is successfully; returns false otherwise.public Optional<String> getDeviceName()
BlePeripheralDevice object.Optional#empty() if the name fails to be obtained.