如何蓝牙读取android定位类



我尝试使用蓝牙,android写和读一个位置。(用法android.location class)

所以,我想写一个字节,我被Parcelabled一个Location类。

这是蓝牙读写的良好条件。(使用android Api示例,bluetoothchat)但是当读取字节并转换为Location类时,写入的数据不相等。(-->BT发送应用程序数据和BT接收应用程序数据不相等。)

如果使用我的BT发送应用程序这个位置数据(Location.toString())

位置[gps 27.47754312158226126.799059 acc=??t=?!?et=?!?熊=138.43465]

但是,我在BT接收应用程序中有一个错误。

11-22 15:54:21.350: E/Bundle(32355): readBundle: bad magic number
11-22 15:54:21.350: E/Bundle(32355): readBundle: trace = java.lang.RuntimeException
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Bundle.readFromParcelInner(Bundle.java:1650)
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Bundle.<init>(Bundle.java:83)
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Parcel.readBundle(Parcel.java:1573)
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Parcel.readBundle(Parcel.java:1558)
11-22 15:54:21.350: E/Bundle(32355):    at android.location.Location$1.createFromParcel(Location.java:713)
11-22 15:54:21.350: E/Bundle(32355):    at android.location.Location$1.createFromParcel(Location.java:698)
11-22 15:54:21.350: E/Bundle(32355):    at com.example.btrecv.RemoteFragment$BTConnHandler.handleMessage(RemoteFragment.java:223)
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 15:54:21.350: E/Bundle(32355):    at android.os.Looper.loop(Looper.java:137)
11-22 15:54:21.350: E/Bundle(32355):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-22 15:54:21.350: E/Bundle(32355):    at java.lang.reflect.Method.invokeNative(Native Method)
11-22 15:54:21.350: E/Bundle(32355):    at java.lang.reflect.Method.invoke(Method.java:511)
11-22 15:54:21.350: E/Bundle(32355):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-22 15:54:21.350: E/Bundle(32355):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-22 15:54:21.350: E/Bundle(32355):    at dalvik.system.NativeStart.main(Native Method)
11-22 15:54:21.355: D/PRS(32355): **mLoc ==> Location[mProvider=gps,mTime=0,mLatitude=0.0,mLongitude=27.47754312158226,mHasAltitude=true,mAltitude=3.33597619E-315,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=120.21687,mExtras=Bundle[mParcelledData.dataSize=0]**] <--this is not equal of send data.

我的发送和读取方法是使用api示例bluetoothchat。

Location to Parcel方法。这是英国电信发送应用程序部分。

    public static void write(Object in) {
           Log.d("PRS", "in BTS.write, entered Object ==> "+in.toString());
            byte[] bytesOut = null;
             //parcelWrite
            Parcel p = Parcel.obtain();
            //location.writeToParcel(p, 0);
            ((Parcelable)in).writeToParcel(p, 0);
            p.setDataPosition(0);
            bytesOut = p.marshall();      //now you've got bytes
            p.recycle();
         // Create temporary object
         ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedThread;
 // Perform the write unsynchronized
        r.write(bytesOut);
       }
    }

Parcel to Location方法。这是英国电信收到的应用程序部分。

public Location ParcelToLoc(byte[] bytesIn) {
        //parcelRead
        Parcel e = Parcel.obtain();
        e.unmarshall(bytesIn, 0, msg.arg1); //msg.arg1 is bytes length?
        e.setDataPosition(0);
        //Parcel to Location
        Location mLoc = Location.CREATOR.createFromParcel(e);
   return mLoc;
}

我引用了如何序列化android.location类?为什么会给我一个错误?对不起,我的英语很差:)

我认为您的包裹实现是错误的

位置是否可行?

如果是的话,为什么要写静态void(Object in)?它应该在Location类中。

这是一个实现包裹的例子

如何使用Intents将对象从一个Android活动发送到另一个?

最新更新