我正在开发Android BLE应用程序。
我面临一个问题:-每次尝试连接设备BluetoothProfile.STATE_DISCONNECTED
的应用程序都在调用时。
它在5,6次尝试连接中连接1次,一旦连接,它将在接下来的4,5次调用BluetoothProfile.STATE_DISCONNECTED
。
我在断开连接时关闭了GATT连接。
这是断开连接的代码
public void disconnect() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.disconnect();
}
/**
* After using a given BLE device, the app must call this method to ensure
* resources are released properly.
*/
public void close() {
if (mBluetoothGatt == null) {
return;
}
mBluetoothGatt.close();
mBluetoothGatt = null;
}
我在:
中调用这些方法else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mConnectionState = STATE_DISCONNECTED;
disconnect();
close();
Utility.dismissProgress();
Log.i(TAG, "Disconnected from GATT server.");
Intent intent = new Intent(ACTION_GATT_DISCONNECTED);
intent.putExtra("DISCONNECTED_DEVICE_ADDRESS", gatt.getDevice().getAddress());
sendBroadcast(intent);
}.
我需要每次都应该调用connect。
答案 0 :(得分:0)
您正在使用哪个AN设备?
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
if (status!=133&&status!=62||disconnectCount>=2) { /*If more then 2 count gatt close process*/
disconnectCount=0;
setConnected(false);
intentAction = MyUtils.ACTION_GATT_DISCONNECTED;
if (getmBluetoothGatt() != null)
refreshDeviceCache(mBluetoothGatt);
getmBluetoothGatt().close();
setmBluetoothGatt(null);
}else { /*reconnection goes here*/
disconnectCount++;
if (isConnected) {
disconnectDevice();
}
connectDevice(mDevice);
}
}
尝试一下可能会有帮助。