这是概率很低的问题,每次复现这个问题的情况都不太一样,我也还没搞清楚为什么会出现这种问题。
上一次出现这个问题的情况是:
在这次连接之前也有重复连接/断开其他蓝牙设备,最后一次连接蓝牙设备时,蓝牙端显示已连接上,但app端还没收到回调,过了一阵子,onConnectionStateChange才收到回调,接着又自动断开重连,于是,问题就出现了。
当出现这个问题时,BluetoothGattCallback里面的每一个方法断开、连接、发现服务、接收数据等都会重复接收,所以判断是BluetoothGattCallback重复了,但我看了后台服务,确实是只有一个服务;
排除是蓝牙端的问题,因为换蓝牙或者蓝牙关机等都只要APP不重启这个问题依旧在。
所以,应该是还有一个BluetoothGattCallback的对象存在。
那么,有哪些原因可能会导致这种情况呢?如果出现这种情况该怎么解决?比如有没有什么方法可以列出所有的BluetoothGattCallback,然后close其他的BluetoothGattCallback对象,只保留一个?
这是连接部分的代码,这些都是在后台service中执行的
public boolean connect(final String address) {
if (mBluetoothAdapter == null) {
return false;
}
if (address == null) {
return false;
}
connectAdress = address;
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt!=null) {
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
final BluetoothDevice deivce = mBluetoothAdapter.getRemoteDevice(address);
if (deivce == null) {
Log.e(TAG, "connect: Device not found. unable to connect." );
return false;
}
mBluetoothGatt = deivce.connectGatt(this, true, mGattCallback);
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void disconnect() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.e(TAG, "disconnect: BluetoothAdapter not initialized" );
return;
}
is_click_disconnect = true;
VERTICAL_DISCOVER_FLAG = 0;
LEVEL_DISCOVER_FLAG = 0;
current_connecting_mac_address = "";
mBluetoothGatt.disconnect();
stopSelf();
}
然后这是回调部分的代码,
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
/**
* 连接状态改变
* @param gatt
* @param status
* @param newState
*/
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// super.onConnectionStateChange(gatt, status, newState);
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
BleMessage message = new BleMessage();
message.setAction(ACTION_GATT_CONNECTED);
message.setConnectState(STATE_CONNECTED);
// 连接成功
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STAAE_DISCONNECTED;
VERTICAL_DISCOVER_FLAG = 0;
LEVEL_DISCOVER_FLAG = 0;
Log.e(TAG, "vitec 蓝牙断开连接onConnectionStateChange: Disconnect from gatt server" );
broadcastUpdate(intentAction);
textToSpeechHelper.speakChinese("蓝牙连接断开");
//当手动断开连接,关闭mBluetoothGatt
if (is_click_disconnect) {
mBluetoothGatt.close();
mBluetoothGatt = null;
is_click_disconnect = false;
}
}
}
/**
* 发现服务
* @param gatt
* @param status
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
enableTXNotification(SYSTEM_RX_SERVICE_UUID, SYSTEM_TX_CHAR_UUID);
}
/**
* 读取特征值
* @param gatt
* @param characteristic
* @param status
*/
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
Log.e(TAG, "onCharacteristicRead: 当前读取到的特征值为:"+ new String(characteristic.getValue()));
}
}
/**
* 写入特征值
* @param gatt
* @param characteristic
* @param status
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
/**
*
* @param gatt
* @param characteristic
*/
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);
}
};