5

接続しているデバイスが 2 つあります。アプリを終了すると、デバイスから切断されます。どちらも同じプロセスを経ますが、アプリを強制終了するまでデバイスの 1 つが接続を維持することがあります。

デバイスには、まだ接続されていると認識されていることを確認するインジケーター ライトがあり、アプリの他のインスタンスは、最初のインスタンスを強制的に閉じるまで接続できません。以下のログでは、最初にリストされたデバイスが接続されたままになっています。

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: F0:3D:A0:04:CA:E7
BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=F0:3D:A0:04:CA:E7

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=7

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: FF:A9:CA:EF:08:A4
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=FF:A9:CA:EF:08:A4

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=5

gatt.close() を呼び出した後、BluetoothManager を取得し、接続されているデバイスのリストで自分のデバイスを探します。リストにあります。

からBluetoothGattCallback

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    final String address = gatt.getDevice().getAddress();
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        onConnected(gatt, address);
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        onDisconnected(gatt, address);
    }
}

private void onDisconnected(BluetoothGatt gatt, String address) {
    Log.v(TAG, address + " disconnected");
    if (devices.containsKey(address)) {
        devices.get(address).setState(Connection.STATE_DISCONNECTED);
        connect(gatt.getDevice());
    } else {
        gatt.close();
        verifyDisconnect(gatt, address);
    }
}

private void verifyDisconnect(BluetoothGatt gatt, String address) {
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    final List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    for (final BluetoothDevice device : devices) {
        if (device.getAddress().equals(address)) {
            Log.d(TAG, address + " is still connected!!!!");
            addCommand(new DisconnectCommand(gatt));
            break;
        }
    }
}

このクラスは、コマンド キューに追加されます。

public class DisconnectCommand extends BluetoothCommand {

    private final BluetoothGatt gatt;

    public DisconnectCommand(BluetoothGatt gatt) {
        this.gatt = gatt;
        priority = BluetoothCommand.DISCONNECT_PRIORITY;
    }

    public boolean execute() {
        gatt.disconnect();
        return true;
    }
}
4

2 に答える 2

1

最初に電話する必要があります

bluetoothGatt.disconnect();

次に、接続を正常に閉じることができるように、コールバックが呼び出されるのを待つ必要があります

public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.d(TAG, "Connected to GATT server.");
                    bluetoothGatt.discoverServices(); // onServicesDiscovered callback will handle the action
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    bluetoothGatt.close();
                    bluetoothGatt = null;
                    Log.d(TAG, "Disonnected fromActivity GATT server.");
                }
            }
于 2021-06-19T15:52:52.357 に答える
0

試す:

public void disconnect() {
    if (blegatt== null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
    }
    else {
        bleGatt.disconnect();
        close();
        gatt = null;
    }
}
/**
 * After using a given BLE device, the app must call this method to ensure resources are
 * released properly.
 */
public void close() {
    if (gatt == null) {
        return;
    }
    gatt.close();
}
于 2017-01-06T05:24:46.873 に答える