首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝牙如何读取android定位类?

蓝牙如何读取android定位类?
EN

Stack Overflow用户
提问于 2013-11-22 07:09:34
回答 1查看 282关注 0票数 0

我试着用蓝牙,android写和读取一个位置。(使用android.location类)

所以,我想写一个字节,我是Parcelabled一个位置类。

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

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

定位系统27.47754312158226,126.799059 acc=?t=?!?et=?!?bear=138.43465

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

代码语言:javascript
复制
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示例蓝牙聊天。

定位到包裹方法。是BT发送应用程序部件。

代码语言:javascript
复制
    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);
       }
    }

包裹到定位的方法。这是BT收到的应用程序部件。

代码语言:javascript
复制
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;
}

我指的是how to serialize for android.location class?,为什么它给了我一个错误?对不起,我糟糕的英语:)

EN

回答 1

Stack Overflow用户

发布于 2013-11-22 07:21:09

我认为你们的包裹实现是错误的。

地点可分解吗?

如果是的话,你为什么会有静态的空写(对象在里面)?应该在定位课上。

下面是一个实现包的示例

How to send an object from one Android Activity to another using Intents?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20139084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档