简体   繁体   English

Java.Lang GetClass 内 C#

[英]Java.Lang GetClass within C#

I am trying to pass a pin to a Bluetooth device within my own receiver on the Android part of my Xamarin Forms application.我正在尝试在我的 Xamarin Forms 应用程序的 Android 部分上将引脚传递给我自己的接收器中的蓝牙设备。 I am getting the correct action stage, but am having issues getting the setPin and convertPinToBytes method.我得到了正确的操作阶段,但是在获取setPinconvertPinToBytes方法时遇到了问题。 I followed the suggestion found here: https://stackoverflow.com/a/19776382/13902576 , but I am having trouble converting this into C#.我遵循了此处的建议: https://stackoverflow.com/a/19776382/13902576 ,但我无法将其转换为 C#。

My implementation is as below:我的实现如下:

var convert = device.BluetoothClass.Class.GetMethod("convertPinToBytes", Java.Lang.Class.FromType(typeof(Java.Lang.String)));
byte[] pin = (byte[])convert.Invoke(device.BluetoothClass.Class, "1234");
var setPin = device.BluetoothClass.Class.GetMethod("setPin", Java.Lang.Class.FromType(typeof(Java.Lang.Byte)));
var connectResult = (bool)setPin.Invoke(device, pin);
Console.WriteLine(connectResult);

I am getting the following exception:我收到以下异常:

java.lang.NoSuchMethodException: convertPinToBytes [class java.lang.String]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at crc6444c83b34196acfb6.BluetoothInstance_BluetoothReceiver.n_onReceive(Native Method)
at crc6444c83b34196acfb6.BluetoothInstance_BluetoothReceiver.onReceive(BluetoothInstance_BluetoothReceiver.java:29)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52226(LoadedApk.java:1319)
at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.$m$0(Unknown Source:4)
at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.run(Unknown Source:0)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Any suggestions would be appreciated, thanks!任何建议将不胜感激,谢谢!

There appeared to be a native solution within C#. C# 中似乎有一个本地解决方案。 As there is a instance of the BluetoothDevice class within C#, some of these methods are available.由于 C# 中有BluetoothDevice设备 class 的实例,因此其中一些方法可用。

The solution could be greatly simplified using by doing:通过执行以下操作可以大大简化解决方案:

byte[] pin = Encoding.UTF8.GetBytes("1234");
var setPin = device.SetPin(pin);

I ended up fixing the first method error using instead:我最终改用以下方法修复了第一个方法错误:

var deviceClass = Java.Lang.Class.ForName(device.Class.CanonicalName);
var convert = deviceClass.GetMethod("convertPinToBytes", Java.Lang.Class.FromType(typeof(Java.Lang.String)));

I could not fix the setPin method error.我无法修复setPin方法错误。 However, my final solution is from example one above.但是,我的最终解决方案来自上面的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM