question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setPreferredPhy in initialize() fails on Android 13

See original GitHub issue

Setting a preferred PHY in the initialize() method, fails on Android 13 (on a Pixel 4a) but works on older Android versions.

onDeviceReady() is never called on Android 13.

Setting the preferredPHY in the connect request doesn’t seem to work at all, even on older devices.

 clientManager.connect(device)
    .useAutoConnect(false)
    .usePreferredPhy(PhyRequest.PHY_LE_2M_MASK)
    .enqueue()

On older devices, I read the PHY in onDeviceReady() and it displays 2M only when setting it in initialize() not via the connectrequest.

I adapted the GattService.kt example to work with my BLE device (uses Nordic UART service). The Android 13 version receives the notifications but onDeviceReady() is not called.

I am testing with 2.5.1 on the latest commit 52d60a4de110b73faef05938ea31a699a9a38163


override fun onDeviceReady() {
    Log.i(TAG, "--------------------------------------")
    Log.i(TAG, "device is ready")
    Log.i(TAG, "--------------------------------------")
    readPhy().enqueue()
    super.onDeviceReady()
}

override fun initialize() {

    setNotificationCallback(mTXCharacteristic)
        .with { _, data ->
            if (data.value != null) {
                val value = String(data.value!!, Charsets.UTF_8)
                defaultScope.launch {
                    myCharacteristicChangedChannel?.send(value)
                }
            }
        }

    enableNotifications(mTXCharacteristic).enqueue()

    if (true && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val bluetoothManager =
            context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        if (bluetoothManager.adapter?.isLe2MPhySupported == true) {
            setPreferredPhy(
                PhyRequest.PHY_LE_2M_MASK,
                PhyRequest.PHY_LE_2M_MASK,
                PhyRequest.PHY_OPTION_NO_PREFERRED
            ).enqueue()
        } else {
            Log.w(TAG, "PHY LE 2M not supported. Run with default 1M")
        }
    }
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:30 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
bobatsarcommented, Sep 29, 2022

It seems to work on my Pixel 4a. Thanks for the fix @philips77

When will be an official release?

1reaction
weliemcommented, Sep 29, 2022

The issue is that the call actually succeeds, but the callback is never called…

Read more comments on GitHub >

github_iconTop Results From Across the Web

BluetoothGattCharacteristic > write crashes with Android 13
If the Bluetooth service is crashing when you write a characteristic, it's a bug in the Bluetooth stack which you should report to...
Read more >
Android-BLE-Library/BleManagerHandler.java at main - GitHub
A library that makes working with Bluetooth LE on Android a pleasure. Seriously. ... Flag set to true when the initialization queue is...
Read more >
BluetoothGatt - Android Developers
A GATT operation failed, errors other than the above ... void, setPreferredPhy(int txPhy, int rxPhy, int phyOptions) ... Constant Value: 13 (0x0000000d) ...
Read more >
Diff - platform/frameworks/base - Google Git
Merge "DeviceInfoUtils.java: support to show kernel version compiled with clang" diff --git a/api/current.txt b/api/current.txt index c095202..71df2e0 ...
Read more >
Android 13: Unable to reconnect reliably to bonded peripheral
Ever since the Android 13 beta, we've observed Android 13 being very inconsistent with reconnection behavior when it comes to bonded Bluetooth Low...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found