我正在我的应用程序中处理BLE连接。一切正常,直到我按下后退键。按下后退按钮时,BLE连接应断开,然后在加载活动时再次尝试连接到工具。当活动开始时,我仍然得到关贸总协定!=空为真。我找不到这个问题。我认为在onConnectionStateChanged()中的代码中存在一些问题,服务已断开连接。我会把我的代码贴在下面。请看一下。
llBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = mContext.getSupportFragmentManager();
BleUtil.disconnect_tool();
fragmentManager.popBackStack();
mContext.recreate();
}
内部BleUtil类:
public static void disconnect_tool()
{
mBluetoothGatt.disconnect();
}
OnConnectionStateChaged()内部:
private BluetoothGattCallback gattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
if (newState == STATE_CONNECTED) {
gattInterface.onToolConnected();
gatt.discoverServices();
Toast.makeText(activity,"Connected",Toast.LENGTH_SHORT).show();
} else if (newState == STATE_DISCONNECTED) {
gatt.close();
mBluetoothGatt.close();
gatt = null;
mBluetoothGatt = null;
gattInterface.onToolDisconnected();
Log.d("checkdisco","disconn");
Toast.makeText(activity,"Disconnected",Toast.LENGTH_SHORT).show();
if(gatt != null) {
gatt.close();
gatt = null;
}
}
}
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { gattInterface.onservicefound(gatt);}