Search code examples
androidgoogle-play-servicesgoogle-nearbygoogle-nearby-connections

Nearby Connections API: Android 12 fails to advertise and discover devices with unknown status codes (8037, 8038, 8039)


I'm developing an app that uses Nearby Connections API with basic P2P strategy. Suddenly, weirdly enough I'm not able to advertise and discover devices on my Pixel 3a with newest Android 12 and Play Services. OnFailureListeners throw these exceptions:

  • for discovering: 8037: unknown status code: 8037
  • for advertising: 8038: unknown status code: 8038

Indeed they cannot be found in docs and source code. I also checked Google's "Rock Papers Scissors" official sample and it throws the same error codes.

Of course I reinstalled the app and restarted & updated my phone. EDIT: I also did the factory reset and it didn't help. But after I downgraded to latest Android 11 using Android Flash Tool, it started working again. Moreover, everything still works fine on Android 11, 10 and 9 using different phones.

Any ideas what causes this issue? It just worked fine for last two weeks and stopped working today. I believe it needs some deep investigation in source code (Xlythe if I may ask for your help, that would be really great).


Solution

  • After a while I finally managed to find a solution. It looks like Nearby Connections (all Nearbys?) have a critical bug on Android 12 that happens non-deterministically (API can work fine for few weeks then suddenly will throw these errors for another few weeks).

    This bug is related to Android 12's feature change related to Bluetooth. Quoting:

    Android 12 introduces the BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE, and BLUETOOTH_CONNECT permissions. These permissions make it easier for apps that target Android 12 to interact with Bluetooth devices, especially for apps that don't require access to device location.

    In other words, some Bluetooth permissions are now legacy and there are some new permissions that are mandatory to make the API work. And you need to handle both scenarios in your code (before and after Android 12).

    So here's a quick "mapper" from error codes to Manifest's permissions:

    • Error code 8037 while discovering - you haven't granted permission for BLUETOOTH_SCAN
    • Error code 8038 while advertising - you haven't granted permission for BLUETOOTH_ADVERTISE
    • Error code 8039 (that's a new one as well) while onEndpointFound - you haven't granted permission for BLUETOOTH_CONNECT

    More information how to implement new permissions properly can be found in documentation.

    And I'm still waiting for Google to fix it in next Nearby API update as this is a little bit of workaround. Adding proper error messages to codes might be a little work to do but I would also revise if other permissions are still required after Android 12's update.