- Home /
Disable/Enable Bluetooth on Android (NATIVE)
hello, I have been looking how to use (call) [BluetoothAdapter Enable()][1] and [Disable][2] (If this is how you can disable/enable Bluetooth) in C#
I have been looking up to modifying this code
public bool setWifiEnabled(bool enabled)
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
try
{
using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
{
return wifiManager.Call<bool>("setWifiEnabled", enabled);
}
}
catch (Exception e)
{
}
}
return false;
}
But I did not find a way. So if somebody knows the code or could explain how it works. Please post. [1]: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable() [2]: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#disable()
Comment
Best Answer
Answer by healthplexus · Apr 06, 2018 at 02:41 AM
@Mpl2014 This worked for us in Unity 2017.3.0f3, thanks:
// to enable Bluetooth on Android
public void setAndroidBluetoothEnabled()
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
try
{
using (var BluetoothManager = activity.Call<AndroidJavaObject>("getSystemService", "bluetooth"))
{
using (var BluetoothAdapter = BluetoothManager.Call<AndroidJavaObject>("getAdapter"))
{
Log("set Bluetooth Enabled");
BluetoothAdapter.Call<bool>("enable");
}
}
}
catch (Exception e)
{
Log("setBluetoothEnabled Exception: " + e);
}
}
}