从Xamarin中的Java绑定库接口访问属性

时间:2016-06-28 11:54:56

标签: java c# xamarin java-native-interface

尝试从Java.Lang.Object获取X时,我收到了一个AbstractMethodError。

class DataMessageHandler : Java.Lang.Object, IRouteManagerMessageHandler
{
      public void Process(Message p0)
      {
           var message = p0;
           var cartesian = Class.FromType(typeof(CartesianFloat));

           var value = (ICartesian)message.GetData(cartesian);
           var x = value.X();
      }
}

CartesianFloat是ICartesian的实现。

ICartesian:

[global::Java.Interop.JavaTypeParameters (new string [] {"T extends java.lang.Number"})]
public partial interface ICartesian : IJavaObject {

    // Metadata.xml XPath method reference: path="/api/package[@name='com.mbientlab.metawear.data']/interface[@name='Cartesian']/method[@name='x' and count(parameter)=0]"
    [Register ("x", "()Ljava/lang/Number;", "GetXHandler:Com.Mbientlab.Metawear.Data.ICartesianInvoker, MetaWearAndroid")]
    global::Java.Lang.Object X ();

    // Metadata.xml XPath method reference: path="/api/package[@name='com.mbientlab.metawear.data']/interface[@name='Cartesian']/method[@name='y' and count(parameter)=0]"
    [Register ("y", "()Ljava/lang/Number;", "GetYHandler:Com.Mbientlab.Metawear.Data.ICartesianInvoker, MetaWearAndroid")]
    global::Java.Lang.Object Y ();

    // Metadata.xml XPath method reference: path="/api/package[@name='com.mbientlab.metawear.data']/interface[@name='Cartesian']/method[@name='z' and count(parameter)=0]"
    [Register ("z", "()Ljava/lang/Number;", "GetZHandler:Com.Mbientlab.Metawear.Data.ICartesianInvoker, MetaWearAndroid")]
    global::Java.Lang.Object Z ();

}

CartesianFloat:

        public unsafe CartesianFloat ()
        : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
    {
        if (Handle != IntPtr.Zero)
            return;

        try {
            if (GetType () != typeof (CartesianFloat)) {
                SetHandle (
                        global::Android.Runtime.JNIEnv.StartCreateInstance (GetType (), "()V"),
                        JniHandleOwnership.TransferLocalRef);
                global::Android.Runtime.JNIEnv.FinishCreateInstance (Handle, "()V");
                return;
            }

            if (id_ctor == IntPtr.Zero)
                id_ctor = JNIEnv.GetMethodID (class_ref, "<init>", "()V");
            SetHandle (
                    global::Android.Runtime.JNIEnv.StartCreateInstance (class_ref, id_ctor),
                    JniHandleOwnership.TransferLocalRef);
            JNIEnv.FinishCreateInstance (Handle, class_ref, id_ctor);
        } finally {
        }
    }

    // This method is explicitly implemented as a member of an instantiated Com.Mbientlab.Metawear.Data.ICartesian
    global::Java.Lang.Object global::Com.Mbientlab.Metawear.Data.ICartesian.X ()
    {
        return global::Java.Interop.JavaObjectExtensions.JavaCast<Java.Lang.Object>(((ICartesian) this).X ());
    }

    // This method is explicitly implemented as a member of an instantiated Com.Mbientlab.Metawear.Data.ICartesian
    global::Java.Lang.Object global::Com.Mbientlab.Metawear.Data.ICartesian.Y ()
    {
        return global::Java.Interop.JavaObjectExtensions.JavaCast<Java.Lang.Object>(((ICartesian) this).Y ());
    }

    // This method is explicitly implemented as a member of an instantiated Com.Mbientlab.Metawear.Data.ICartesian
    global::Java.Lang.Object global::Com.Mbientlab.Metawear.Data.ICartesian.Z ()
    {
        return global::Java.Interop.JavaObjectExtensions.JavaCast<Java.Lang.Object>(((ICartesian) this).Z ());
    }

}

如果我尝试将值转换为CartesianFloat,则表示没有方法X()存在于值上。

对值进行监视,其值为{(-0.062,0.070,1.010)}

1 个答案:

答案 0 :(得分:0)

Metwear API在BLE数据包中发送编码数据,您可以使用以下代码对其进行解码。我从它们的java代码中翻译了它(而不是优化的)。

public void Process(Message p0)
  {
    var message = p0 as Bmi160ThreeAxisMessage;
    var data = message.GetData();
    var x = ((short)(data[1] << 8 | data[0])) / message.Scale;
    var y = ((short)(data[3] << 8 | data[2])) / message.Scale;
    var z = ((short)(data[5] << 8 | data[4])) / message.Scale;
}