下面列出了android.bluetooth.BluetoothGatt#STATE_CONNECTING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@VisibleForTesting
void connectWithGatt() {
int oldState;
int newState = BluetoothGatt.STATE_CONNECTING;
synchronized (lock) {
oldState = state;
state = BluetoothGatt.STATE_CONNECTING;
}
NeatleLogger.d("Connecting with " + device.getName() + "[" + device.getAddress() + "]");
BluetoothGatt gatt = device.connectGatt(context, false, callback);
synchronized (lock) {
if (state == BluetoothGatt.STATE_DISCONNECTED) {
gatt = null;
}
this.gatt = gatt;
if (gatt == null) {
state = BluetoothGatt.STATE_DISCONNECTED;
newState = BluetoothGatt.STATE_DISCONNECTED;
}
}
notifyConnectionStateChange(oldState, newState);
}
/**
* 连接状态改变,主要用来分析设备的连接与断开
* @param gatt GATT
* @param status 改变前状态
* @param newState 改变后状态
*/
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
ViseLog.i("onConnectionStateChange status: " + status + " ,newState: " + newState +
" ,thread: " + Thread.currentThread());
if (newState == BluetoothGatt.STATE_CONNECTED) {
gatt.discoverServices();
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
close();
if (connectCallback != null) {
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
ViseBle.getInstance().getDeviceMirrorPool().removeDeviceMirror(deviceMirror);
if (status == BluetoothGatt.GATT_SUCCESS) {
connectState = ConnectState.CONNECT_DISCONNECT;
connectCallback.onDisconnect(isActiveDisconnect);
} else {
connectState = ConnectState.CONNECT_FAILURE;
connectCallback.onConnectFailure(new ConnectException(gatt, status));
}
}
} else if (newState == BluetoothGatt.STATE_CONNECTING) {
connectState = ConnectState.CONNECT_PROCESS;
}
}
private void deviceDiscovered() {
stopDiscovery();
int state = getState();
if (state == BluetoothGatt.STATE_CONNECTING) {
NeatleLogger.i("Device discovered. Continuing with connecting");
connectWithGatt();
} else {
NeatleLogger.e("Device discovered but no longer connecting");
}
}
public void run() {
stopDiscovery();
int state = getState();
if (state == BluetoothGatt.STATE_CONNECTING) {
NeatleLogger.e("Device no discovered failing connection attempt.");
connectionFailed(BluetoothGatt.GATT_FAILURE);
} else {
NeatleLogger.e("Discover timeout but we are not connecting anymore.");
}
}
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;
}
}
@Override
public void connect() {
int oldState;
int newState;
boolean doConnectGatt = false;
boolean doDiscovery = false;
boolean adapterEnabled = adapter != null && adapter.isEnabled();
synchronized (lock) {
if (isConnected() || isConnecting()) {
return;
}
if (this.gatt != null) {
throw new IllegalStateException();
}
oldState = state;
if (!adapterEnabled) {
//newState = BluetoothAdapter.STATE_OFF;
NeatleLogger.d("BT off. Won't connect to " + device.getName() + "[" + device.getAddress() + "]");
connectionFailed(BluetoothGatt.GATT_FAILURE);
return;
} else {
newState = BluetoothGatt.STATE_CONNECTING;
if (device.getType() == BluetoothDevice.DEVICE_TYPE_UNKNOWN) {
doDiscovery = true;
} else {
doConnectGatt = true;
}
}
}
//call these methods outside of the lock, to prevent deadlocks
if (doConnectGatt) {
connectWithGatt();
return;
}
synchronized (lock) {
state = newState;
}
notifyConnectionStateChange(oldState, newState);
if (doDiscovery) {
NeatleLogger.d("Device unknown, let's discover it" + device.getName() + "[" + device.getAddress() + "]");
discoverDevice();
}
}
public boolean isConnecting() {
synchronized (lock) {
return state == BluetoothGatt.STATE_CONNECTING;
}
}
boolean isNativelyConnecting()
{
return getConnectionState() == BluetoothGatt.STATE_CONNECTING;
}
boolean isNativelyConnectingOrConnected()
{
int state = getConnectionState();
return state == BluetoothGatt.STATE_CONNECTED || state == BluetoothGatt.STATE_CONNECTING;
}
/**
* Connects to the Bluetooth Smart device.
*
* @param device a device to connect to
*/
public void connect(final BluetoothDevice device) {
if (connected)
return;
synchronized (lock) {
if (bluetoothGatt != null) {
// There are 2 ways of reconnecting to the same device:
// 1. Reusing the same BluetoothGatt object and calling connect() - this will force the autoConnect flag to true
// 2. Closing it and reopening a new instance of BluetoothGatt object.
// The gatt.close() is an asynchronous method. It requires some time before it's finished and
// device.connectGatt(...) can't be called immediately or service discovery
// may never finish on some older devices (Nexus 4, Android 5.0.1).
// If shouldAutoConnect() method returned false we can't call gatt.connect() and have to close gatt and open it again.
if (!initialConnection) {
bluetoothGatt.close();
bluetoothGatt = null;
try {
Thread.sleep(200); // Is 200 ms enough?
} catch (final InterruptedException e) {
// Ignore
}
} else {
// Instead, the gatt.connect() method will be used to reconnect to the same device.
// This method forces autoConnect = true even if the gatt was created with this flag set to false.
initialConnection = false;
connectionState = BluetoothGatt.STATE_CONNECTING;
callbacks.onDeviceConnecting(device);
bluetoothGatt.connect();
return;
}
} else {
// Register bonding broadcast receiver
context.registerReceiver(bluetoothStateBroadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
context.registerReceiver(bondingBroadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}
}
final boolean shouldAutoConnect = shouldAutoConnect();
userDisconnected = !shouldAutoConnect; // We will receive Linkloss events only when the device is connected with autoConnect=true
// The first connection will always be done with autoConnect = false to make the connection quick.
// If the shouldAutoConnect() method returned true, the manager will automatically try to reconnect to this device on link loss.
if (shouldAutoConnect)
initialConnection = true;
bluetoothDevice = device;
connectionState = BluetoothGatt.STATE_CONNECTING;
callbacks.onDeviceConnecting(device);
bluetoothGatt = device.connectGatt(context, false, gattCallback = new BleManagerGattCallback());
}
boolean isNativelyConnecting()
{
return getConnectionState() == BluetoothGatt.STATE_CONNECTING;
}
boolean isNativelyConnectingOrConnected()
{
int state = getConnectionState();
return state == BluetoothGatt.STATE_CONNECTED || state == BluetoothGatt.STATE_CONNECTING;
}