1

我只在我的 Oneplus 设备中遇到问题,作为 Pixel,诺基亚运行良好。

这是我的问题。我有 BLE 设备并使用以下代码通过我的应用程序连接到它。

public boolean connect(final String address) {

    if (mBluetoothAdapter == null || address == null) {
        Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    String mBluetoothDeviceAddress = Util.getDeviceAddress(this);

    // Previously connected device.  Try to reconnect.
    if (mBluetoothGatt != null && Util.isBLEDeviceConnected(this, mBluetoothDeviceAddress)) {
        Log.d(TAG, "Already connected");

        broadcastUpdate(ACTION_GATT_CONNECTED, mBluetoothDeviceAddress);
        return true;
    } else if (mBluetoothGatt != null) {
        Log.d(TAG, "GATT Connection is Required");

        mBluetoothGatt.connect();
        return true;
    }

    if (!mIsConnecting) {
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        // We want to auto connect to the device, so we are setting the autoConnect
        // parameter to true.
        Log.d(TAG, "Trying to create a new connection.");
        mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
        mIsConnecting = true;
    } else {
        Log.d(TAG, "Already connection is in progress");
    }

    return true;
}

@Override
public void onDestroy() {
    super.onDestroy();
    disconnect();
    close();
    unregisterReceiver(bluetoothReceiver);
    unregisterReceiver(mGattDisconnectReceiver);

}

public void disconnect() {
    Log.d(TAG, "Disconnecting Gatt " + mBluetoothGatt);

    if (mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothGatt not initialized");
        return;
    }
    mBluetoothGatt.disconnect();
    mIsConnecting = false;
}

public void close() {
    Log.d(TAG, "Closing Gatt " + mBluetoothGatt);

    if (mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothGatt not initialized");
        return;
    }
    mBluetoothGatt.close();
    mIsConnecting = false;
    mBluetoothGatt = null;
}

配对工作正常,直到我关闭/关闭我的手机。如果我下次尝试配对/连接时重新启动手机,它将无法正常工作。它根据我的代码显示“尝试创建新连接”,并在下面附加日志中显示。

2019-03-01 09:01:08.083 9983-9983/com.wrkspot.employee.dev D/BLEService: Trying to create a new connection.
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: connect() - device: C1:B4:70:12:4B:23, auto: true
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp()
2019-03-01 09:01:08.085 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp() - UUID=a18ee742-4543-473c-8789-37a22845a96c
2019-03-01 09:01:08.087 9983-10064/com.wrkspot.employee.dev D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8

我被困住了,我真的需要一些建议。这个问题只发生在我的 Oneplus 3T 和我测试过的其他几部手机上,它工作正常。

发生此问题时,我必须重新安装应用程序并重新启动手机。只有这样我才能再次配对/连接。

4

1 回答 1

2

仅当 Android 将设备放在其缓存中或设备已绑定时,值为 true 的自动连接才有效。当您重新启动手机时,缓存将被清除。所以你必须重新扫描它......

于 2019-03-01T05:54:02.287 回答