为什么安卓ACTION_UUID返回错误的对象

2024-07-03 06:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用kivy的python for android为android编写python蓝牙库。我使用jnius模块中的autoclass导入java类。到目前为止,它工作得很好,但是,当我调用函数fetchUuidsWithSdp()并检查获取的UUID的操作意图时,我得到了一个错误的对象。我使用的代码是:

elif action == ACTION_UUID:
    print("....... ACTION_UUID .......")
    extras = intent.getExtras()
    fetchedUUIDs = extras.get(EXTRA_UUID)
    # fetchedUUIDs = intent.getParcelableArrayExtra(EXTRA_UUID)
    print("fetchedUUIDs object type is: ..............")
    print (fetchedUUIDs)
    for u in fetchedUUIDs:
        print u.toString()

因此,我在adb控制台中得到以下打印输出:

I/python  (30971): ....... ACTION_UUID .......
I/python  (30971): fetchedUUIDs object type is: ..............
I/python  (30971): [<android.os.Parcelable at 0x7c1f2600 jclass=android/os/Parcelable jself=<LocalRef obj=0x20f00dfa at 0x79f7bab0>>,     <android.os.Parcelable at 0x7c1

显示“fetcheduids”的对象类型为android.os.Parcelable包'列表而不是'parcelouid'列表。调用toString()时,我得到以下跟踪:

I/python  (30971):  Traceback (most recent call last):
I/python  (30971):    File "jnius/jnius_proxy.pxi", line 47, in jnius.jnius.PythonJavaClass.invoke (jnius/jnius.c:24931)
I/python  (30971):    File "jnius/jnius_proxy.pxi", line 73, in jnius.jnius.PythonJavaClass._invoke (jnius/jnius.c:25609)
I/python  (30971):    File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/android/broadcast.py", line 18, in onReceive
I/python  (30971):    File "main.py", line 235, in on_broadcast
I/python  (30971):      print u.toString()
I/python  (30971):  AttributeError: 'android.os.Parcelable' object has no attribute 'toString'

我的代码有什么问题吗?非常感谢您的帮助。你知道吗


Tags: inforobjectuuidoslineactionat
2条回答

我的猜测是,您要么没有在java代码的上游创建UUID,要么没有像预期的那样将其传递到Intent extra中。你知道吗

您确定fetchUuidsWithSdp()在java代码中返回UUID吗?您确定UUID正确地通过java代码中的Intent传递了吗?你知道吗

查看用于生成和传输似乎丢失的原始UUID的代码会很有帮助。你知道吗

谢谢格伦的帮助。我使用的代码是androidsdk所解释的。你知道吗

我终于使用设备对象获得了服务UUID,该对象也在intent extras中传递,如下所示:

elif action == ACTION_UUID:
    print("....... ACTION_UUID .......")
    extras = intent.getExtras()
    fetchedDevice = extras.get(EXTRA_DEVICE)
    fetchedUUIDs = fetchedDevice.getUuids()
    if fetchedUUIDs:
        for u in fetchedUUIDs:
            print u.toString() 

相关问题 更多 >