Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Helper classes not working.

Discussion in 'Android' started by jonbro5556, Jul 18, 2016.

  1. jonbro5556

    jonbro5556

    Joined:
    Jan 1, 2013
    Posts:
    24
    Hey folks! I have a confusing issue where the helper classes aren't calling code as expected, even though native code works just fine.

    Code (csharp):
    1.        AndroidJavaObject intent = new AndroidJavaObject ("android.content.Intent", "android.provider.Settings.ACTION_BLUETOOTH_SETTINGS");
    2.  AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3.  AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
    4.  activity.Call("startActivity",intent);
    5.  
    returns the following error:
    Code (csharp):
    1.  
    2. AndroidJavaException: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.Settings.ACTION_BLUETOOTH_SETTINGS }
    However, if I write the same code as a java class
    Code (csharp):
    1.  
    2. package com.mostlycircle.bluetoothsettingsopener;
    3. import android.app.Application;
    4. import android.provider.Settings;
    5. import android.util.Log;
    6. import android.content.Intent;
    7. import com.unity3d.player.*;
    8. public class BluetoothSettingsOpener {
    9. public static void OpenBluetoothSettings()
    10. {
    11. Intent openBluetooth = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
    12. UnityPlayer.currentActivity.startActivity(openBluetooth);
    13. }
    14. }
    15.  
    and call it from unity code as so:
    Code (csharp):
    1.  
    2. var ajc = new AndroidJavaClass("com.mostlycircle.bluetoothsettingsopener.BluetoothSettingsOpener");
    3. ajc.CallStatic("OpenBluetoothSettings");
    4.  
    it works just fine. If anyone could provide any guidance as to why the first one doesn't work, that would be greatly appreciated!
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,560
    In Java, you are constructing the intent by passing the action using the static field on the Settings class, this is actually a helper for getting the action value (see the docs here: https://developer.android.com/reference/android/provider/Settings.html#ACTION_BLUETOOTH_SETTINGS)

    You should initialize your intent using the correct value:
    Code (CSharp):
    1. AndroidJavaObject intent = newAndroidJavaObject("android.content.Intent", "android.settings.BLUETOOTH_SETTINGS");
    *I didnt test this code, so let me know if it works.