C# Xamarin Android反射配对蓝牙目标异常
我对蓝牙配对确认有点问题。下面是Java行代码,它运行良好:C# Xamarin Android反射配对蓝牙目标异常,c#,xamarin,xamarin.android,system.reflection,C#,Xamarin,Xamarin.android,System.reflection,我对蓝牙配对确认有点问题。下面是Java行代码,它运行良好: // java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10094 nor current process has android.permission.BLUETOOTH_PRIVILEGED. // above perm is only granted to system apps, not third party app
// java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10094 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
// above perm is only granted to system apps, not third party apps ...
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
但在Xamarin中,当我启动这一行时,它显示了一个TagretException
device.Class.GetMethod("setPairingConfirmation", new Java.Lang.Class[] { Java.Lang.Boolean.Type }).Invoke(device, Java.Lang.Boolean.True);
有人知道为什么会这样吗?我使用了一个反射系统,比如
device.GetType().GetMethod("SetPairingConfirmation").Invoke(device, new object[] { true });
而且它也不起作用。真的很困惑。你让事情变得更加复杂了 您可以只使用
SetPairingConfirmation()
函数!如果API级别大于19,您只需确保进行检查即可运行代码,因为那时才引入了BluetoothDevice
。您可能还希望使用不同的异常捕获,如Java.IO.IOException
public bool IsPairingConfirmed(Android.Bluetooth.BluetoothDevice device)
{
bool confirmation = false;
try
{
if ((int)Android.OS.Build.VERSION.SdkInt >= 19)
confirmation = device.SetPairingConfirmation(true);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return confirmation;
}
我希望这是有意义的,并确保你做的每件事都是正确的