代码片段:出现连接133的问题找了很久的解决办法,尝试各种亦然不行,最终的解决办法就是下面标红代码
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == gatt.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange: --------" + "\"连接成功\"");
//发现服务
gatt.discoverServices();
} else if (newState == gatt.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange: --------" + "连接失败");
}
}else {
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
//重连次数不大于最大重连次数
if(reConnectionNum < maxConnectionNum){
//重连次数自增
reConnectionNum++;
//连接设备
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mBluetoothGatt = mBluetoothDevice.connectGatt(MainActivity.this,
false, mBluetoothGattCallback, BluetoothDevice.TRANSPORT_LE);
} else {
mBluetoothGatt = mBluetoothDevice.connectGatt(MainActivity.this, false, mBluetoothGattCallback);
}
}else{
//断开连接,返回连接失败回调
}
}