1

I have an issue only in my Oneplus device, where as Pixel, Nokia works great.

Here is my issue. I have BLE device and connect to it via my app using following code.

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;
}

Pairing works fine until i turn off/switch off my Phone. If i restart the phone the next time when i try to pair/connect, it will not work. It says "Trying to create a new connection" as per my code and says as attached logs below.

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

I am stuck and i really need some suggestions. This issue is occuring only on my Oneplus 3T and in few other phones i have tested it works fine.

When this issue occurs i have to reinstall the app and restart the phone. Only then i can pair/connect again.

SRBhagwat
  • 1,291
  • 1
  • 14
  • 21

1 Answers1

2

Autoconnect with value true only works if Android has the device in its cache or when the device is bonded. When you restart your phone, the cache is cleared. So you have to scan for it again....

  • Yeah even if i Scan again and try to pair, i am unable to connect/pair to it. – SRBhagwat Mar 01 '19 at 06:17
  • If you instead use the nRF Connect app on the same phone, does it work better? (try both autoConnect false and true) – Emil Mar 01 '19 at 08:03