Search code examples
androidandroid-thingsgoogle-nearby

Android Nearby not working on Android Things


I have have the following code:

OnFailureListener onFailureListener = new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception e) {
    Log.e(TAG, "bum", e);
  }
};
connectionsClient.startAdvertising("Device A", getPackageName(), myCallback, new AdvertisingOptions(STRATEGY))
        .addOnFailureListener(onFailureListener);

If I run in on my phone it works as expected, but when I run it on my Android Things device I get the following error

com.google.android.gms.common.api.ApiException: 17: Error resolution was canceled by the user, original error message: UNKNOWN_ERROR_CODE(8050): null 

From what I have noticed so far, my phone has Google Play Services version 12.8.72, but the Android Things image has Google Play Services 12.5.20

Anyone else had the same issue, and found a solution for it?


Solution

  • Adding to Varun's answer, I'm going to guess that the default launcher is still running and hogging Connections. The IoT launcher will look something like this:

    enter image description here

    You can make your app the home app by adding an intent-filter to the manifest, like so (official documentation):

    <application
    android:label="@string/app_name">
    <activity android:name=".HomeActivity">
        <!-- Launch activity as default from Android Studio -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    
        <!-- Launch activity automatically on boot, and re-launch if the app terminates. -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    Once you add the intent-filter to your manifest and install + run your app, you'll want to make sure the default launcher is no longer running. The easiest way to do this is to just reboot your Android Things device. Since your app is now the home app, it'll be the first app to launch after the reboot.

    I'm not sure what Android Things version/system image you have, but you might also be able to do with one of the following adb commands:

    adb shell am force-stop com.android.iotlauncher.ota
    

    or maybe:

    adb shell am force-stop com.android.iotlauncher