下面列出了android.bluetooth.BluetoothAdapter#STATE_DISCONNECTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onConnectionStateChanged(Connection connection, int newState) {
if (connectionStateListener != null) {
connectionStateListener.onConnectionStateChanged(connection, newState);
}
if (newState == BluetoothAdapter.STATE_DISCONNECTED) {
NeatleLogger.d("Will try to reconnect to " + connection.getDevice().getAddress() + " after " + (reconnectTimeout / 1000) + " seconds");
handler.removeCallbacks(reconnectRunnable);
handler.postDelayed(reconnectRunnable, reconnectTimeout);
reconnectTimeout = Math.min(reconnectTimeout * 2, MAX_RECONNECT_TIMEOUT);
} else if (newState == BluetoothAdapter.STATE_CONNECTED) {
reconnectTimeout = DEFAULT_RECONNECT_TIMEOUT;
}
}
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
switch (state) {
case BluetoothAdapter.STATE_DISCONNECTED:
return "DISCONNECTED";
case BluetoothAdapter.STATE_CONNECTED:
return "CONNECTED";
case BluetoothAdapter.STATE_CONNECTING:
return "CONNECTING";
case BluetoothAdapter.STATE_DISCONNECTING:
return "DISCONNECTING";
case BluetoothAdapter.STATE_OFF:
return "OFF";
case BluetoothAdapter.STATE_ON:
return "ON";
case BluetoothAdapter.STATE_TURNING_OFF:
// Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
return "TURNING_OFF";
case BluetoothAdapter.STATE_TURNING_ON:
// Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
return "TURNING_ON";
default:
return "INVALID";
}
}
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
switch (state) {
case BluetoothAdapter.STATE_DISCONNECTED:
return "DISCONNECTED";
case BluetoothAdapter.STATE_CONNECTED:
return "CONNECTED";
case BluetoothAdapter.STATE_CONNECTING:
return "CONNECTING";
case BluetoothAdapter.STATE_DISCONNECTING:
return "DISCONNECTING";
case BluetoothAdapter.STATE_OFF:
return "OFF";
case BluetoothAdapter.STATE_ON:
return "ON";
case BluetoothAdapter.STATE_TURNING_OFF:
// Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
return "TURNING_OFF";
case BluetoothAdapter.STATE_TURNING_ON:
// Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
return "TURNING_ON";
default:
return "INVALID";
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
/*
* status -
*
* 8 (0x08) - connection timeout / device went out of range
* 19 (0x13) - connection terminated by peer user
* 22 (0x16) - connection terminated by local host
*
* For more information, please refer to:
* https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/refs/tags/android-cts-5.1_r28/stack/include/gatt_api.h
*/
BluetoothDevice device = gatt.getDevice();
log("Connection state changed - " + device.getName() + " " + device.getAddress() + " status: " + status + " newState: " + newState);
if (ignoredDevices.contains(device.getAddress())) {
return;
}
if (status == 22) {
// will get here after calling 'removeBond'
handleDeviceUnpaired(gatt);
return;
}
switch (newState) {
case BluetoothAdapter.STATE_CONNECTED:
handleDeviceConnection(gatt);
break;
case BluetoothAdapter.STATE_DISCONNECTED:
handleDeviceDisconnection(gatt);
break;
}
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "blueConnectStateBroadcastReceiver action>>>>" + intent.getAction());
if (intent.getAction() == null)
return;
if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
int blueconState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
switch (blueconState) {
case BluetoothAdapter.STATE_CONNECTED:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTED");
voiceMediator.setBlueHeadSet(true);
isBlueToothHeadsetConnected();
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTED");
voiceMediator.setBlueHeadSet(false);
voiceMediator.setSuportA2DP(false);
AudioManager mAudioManager_ = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager_.setBluetoothScoOn(false);
mAudioManager_.stopBluetoothSco();
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTING");
voiceMediator.setSuportA2DP(false);
break;
default:
break;
}
}
}
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
switch (state) {
case BluetoothAdapter.STATE_DISCONNECTED:
return "DISCONNECTED";
case BluetoothAdapter.STATE_CONNECTED:
return "CONNECTED";
case BluetoothAdapter.STATE_CONNECTING:
return "CONNECTING";
case BluetoothAdapter.STATE_DISCONNECTING:
return "DISCONNECTING";
case BluetoothAdapter.STATE_OFF:
return "OFF";
case BluetoothAdapter.STATE_ON:
return "ON";
case BluetoothAdapter.STATE_TURNING_OFF:
// Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
return "TURNING_OFF";
case BluetoothAdapter.STATE_TURNING_ON:
// Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
return "TURNING_ON";
default:
return "INVALID";
}
}
/**
* Converts BluetoothAdapter states into local string representations.
*/
private String stateToString(int state) {
switch (state) {
case BluetoothAdapter.STATE_DISCONNECTED:
return "DISCONNECTED";
case BluetoothAdapter.STATE_CONNECTED:
return "CONNECTED";
case BluetoothAdapter.STATE_CONNECTING:
return "CONNECTING";
case BluetoothAdapter.STATE_DISCONNECTING:
return "DISCONNECTING";
case BluetoothAdapter.STATE_OFF:
return "OFF";
case BluetoothAdapter.STATE_ON:
return "ON";
case BluetoothAdapter.STATE_TURNING_OFF:
// Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
return "TURNING_OFF";
case BluetoothAdapter.STATE_TURNING_ON:
// Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
return "TURNING_ON";
default:
return "INVALID";
}
}
/**
* Converts BluetoothAdapter states into local string representations.
*/
private String stateToString(int state) {
switch (state) {
case BluetoothAdapter.STATE_DISCONNECTED:
return "DISCONNECTED";
case BluetoothAdapter.STATE_CONNECTED:
return "CONNECTED";
case BluetoothAdapter.STATE_CONNECTING:
return "CONNECTING";
case BluetoothAdapter.STATE_DISCONNECTING:
return "DISCONNECTING";
case BluetoothAdapter.STATE_OFF:
return "OFF";
case BluetoothAdapter.STATE_ON:
return "ON";
case BluetoothAdapter.STATE_TURNING_OFF:
// Indicates the local Bluetooth adapter is turning off. Local clients should immediately
// attempt graceful disconnection of any remote links.
return "TURNING_OFF";
case BluetoothAdapter.STATE_TURNING_ON:
// Indicates the local Bluetooth adapter is turning on. However local clients should wait
// for STATE_ON before attempting to use the adapter.
return "TURNING_ON";
default:
return "INVALID";
}
}