下面列出了android.bluetooth.BluetoothGatt#STATE_DISCONNECTING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Disconnects from the device or cancels the pending connection attempt. Does nothing if device was not connected.
* @return true if device is to be disconnected. False if it was already disconnected.
*/
public boolean disconnect() {
userDisconnected = true;
initialConnection = false;
if (bluetoothGatt != null) {
connectionState = BluetoothGatt.STATE_DISCONNECTING;
callbacks.onDeviceDisconnecting(bluetoothGatt.getDevice());
final boolean wasConnected = connected;
bluetoothGatt.disconnect();
if (!wasConnected) {
// There will be no callback, the connection attempt will be stopped
connectionState = BluetoothGatt.STATE_DISCONNECTED;
callbacks.onDeviceDisconnected(bluetoothGatt.getDevice());
}
return true;
}
return false;
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothGatt.STATE_DISCONNECTED || newState == BluetoothGatt.STATE_DISCONNECTING) {
runOnUiThread(new Runnable() {
@Override
public void run() {
disconnectWithModal();
}
});
}
}
private boolean internalDisconnect() {
userDisconnected = true;
initialConnection = false;
ready = false;
if (bluetoothGatt != null) {
connectionState = BluetoothGatt.STATE_DISCONNECTING;
log(Log.VERBOSE, connected ? "Disconnecting..." : "Cancelling connection...");
final BluetoothDevice device = bluetoothGatt.getDevice();
if (connected) {
postCallback(c -> c.onDeviceDisconnecting(device));
postConnectionStateChange(o -> o.onDeviceDisconnecting(device));
}
log(Log.DEBUG, "gatt.disconnect()");
bluetoothGatt.disconnect();
final boolean wasConnected = connected;
if (wasConnected)
return true;
// If the device wasn't connected, there will be no callback after calling
// gatt.disconnect(), the connection attempt will be stopped.
connectionState = BluetoothGatt.STATE_DISCONNECTED;
log(Log.INFO, "Disconnected");
postCallback(c -> c.onDeviceDisconnected(device));
postConnectionStateChange(o -> o.onDeviceDisconnected(device, ConnectionObserver.REASON_SUCCESS));
}
// request may be of type DISCONNECT or CONNECT (timeout).
// For the latter, it has already been notified with REASON_TIMEOUT.
if (request != null && request.type == Request.Type.DISCONNECT) {
if (bluetoothDevice != null)
request.notifySuccess(bluetoothDevice);
else
request.notifyInvalidRequest();
}
nextRequest(true);
return true;
}
static RxBleConnectionState mapConnectionStateToRxBleConnectionStatus(int newState) {
switch (newState) {
case BluetoothGatt.STATE_CONNECTING:
return RxBleConnectionState.CONNECTING;
case BluetoothGatt.STATE_CONNECTED:
return RxBleConnectionState.CONNECTED;
case BluetoothGatt.STATE_DISCONNECTING:
return RxBleConnectionState.DISCONNECTING;
default:
return RxBleConnectionState.DISCONNECTED;
}
}
/**
* Disconnects from the sensor.
*/
public final void disconnect() {
final int state = bleManager.getConnectionState();
if (state == BluetoothGatt.STATE_DISCONNECTED || state == BluetoothGatt.STATE_DISCONNECTING) {
bleManager.close();
onDeviceDisconnected(bluetoothDevice);
return;
}
bleManager.disconnect().enqueue();
}
private boolean isDisconnectedOrDisconnecting(int newState) {
return newState == BluetoothGatt.STATE_DISCONNECTED || newState == BluetoothGatt.STATE_DISCONNECTING;
}
boolean isNativelyDisconnecting()
{
return getConnectionState() == BluetoothGatt.STATE_DISCONNECTING;
}
boolean isNativelyDisconnecting()
{
return getConnectionState() == BluetoothGatt.STATE_DISCONNECTING;
}