-1

我正在尝试将 PythonJavaClass (NokeServiceListener) 发送到 mNokeService.registerNokeListener() 但它一直出错,当打印 NokeServiceListener 对象时,我在 0x97389b70 处得到 nokeLock.NokeServiceListener 对象,它看起来不像 java 对象,然后我收到错误 JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x6e617278

from jnius import autoclass,PythonJavaClass,cast,java_method

Context =  autoclass('android.content.Context')
    Parcelable = autoclass('android.os.Parcelable')
    Intent = autoclass('android.content.Intent')
    Uri = autoclass('android.net.Uri')
    PythonActivity = autoclass('org.kivy.android.PythonActivity')

    NokeDeviceManagerService = autoclass('com.noke.nokemobilelibrary.NokeDeviceManagerService')
    #LocalBinder = autoclass('com.noke.nokemobilelibrary.NokeDeviceManagerService$LocalBinder')
    NokeDevice = autoclass('com.noke.nokemobilelibrary.NokeDevice')
    NokeMobileError = autoclass('com.noke.nokemobilelibrary.NokeMobileError')
    #NokeServiceListener =autoclass('com.noke.nokemobilelibrary.NokeServiceListener')

    global mNokeService

    #@run_on_ui_thread
    class NokeApi():
        def __init__(self):
            self.python_activity = PythonActivity.mActivity
            self.service_connection = ServiceConnection()

        def initiateNokeService(self):
            currentActivity = cast('android.app.Activity', self.python_activity)
            context = cast('android.content.Context', currentActivity.getApplicationContext())

            nokeIntent = Intent()
            nokeIntent.setClassName(context, 'com.noke.nokemobilelibrary.NokeDeviceManagerService')
            self.python_activity.bindService(nokeIntent,self.service_connection,Context.BIND_AUTO_CREATE)

        def onCreate(self):
            self.initiateNokeService()

    #@run_on_ui_thread
    class ServiceConnection(PythonJavaClass):
        __javainterfaces__ = ['android.content.ServiceConnection']
        __javacontext__ = 'app'

        @java_method('(Landroid/content/ComponentName;Landroid/os/IBinder;)V')
        def onServiceConnected(self,className, rawBinder):
            print 'debug1'
            #nokeDeviceManagerService = NokeDeviceManagerService()
            #localBinder = LocalBinder()
            nokeService = cast('com.noke.nokemobilelibrary.NokeDeviceManagerService$LocalBinder',rawBinder)
            global mNokeService
            mNokeService = nokeService.getService()
            print mNokeService
            #mNokeService = ((NokeDeviceManagerService.LocalBinder)rawBinder).getService()
            print 'debug2'
            mNokeServiceListener = NokeServiceListener()
            #mNokeServiceListener = cast('com.noke.nokemobilelibrary.NokeServiceListener',nokeServiceListener)
            print mNokeServiceListener
            print 'debug2.5'
            mNokeService.registerNokeListener(mNokeServiceListener)
            print 'debug3'
            noke1 = NokeDevice("NOKE3P", "F7:F3:F1:2C:66:25")
            print 'debug4'
            mNokeService.addNokeDevice(noke1)
            print 'debug5'
            mNokeService.setUploadUrl("https://coreapi-sandbox.appspot.com/upload/")
            print 'debug6'
            mNokeService.startScanningForNokeDevices()
            print "Scanning for devices"

            if not mNokeService.initialize():
                print "Unable to initialize Bluetooth"

    class NokeServiceListener(PythonJavaClass):
        __javainterfaces__ = ['com.noke.nokemobilelibrary.NokeServiceListener']
        __javacontext__ = 'app'

        def __init__(self):
            pass

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeDiscovered(self,noke):
            print "Connecting to Noke"
            mNokeService.connectToNoke(self,noke)

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeConnecting(self,noke):
            print "Connecting"

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeConnected(self,noke):
            print "Noke Connected"
            self.requestUnlock(noke)

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeSyncing(self,noke):
            print "NOKE SYNCING"

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeUnlocked(self,noke):
            print 'Noke Unlocked'

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;)V')
        def onNokeDisconnected(self,noke):
            print "Noke Disconnected"
            self.mNokeService.uploadData()
            self.mNokeService.startScanningForNokeDevices()

        @java_method('(I)V')
        def onBluetoothStatusChanged(self,bluetoothStatus):
            pass

        @java_method('(Lcom/noke/nokemobilelibrary/NokeDevice;ILjava/lang/String;)V')
        def onError(self,noke, error, message):
            pass

        def requestUnlock(self,noke):
            msg = '{"function":"Noke_Unlock","session":"%s","mac":"%s"}' % (noke.getSession(),noke.getMac())
            rsp = connectToServer(_host, _port, msg)
            if rsp['result'] == "success":
                commandStr = rsp["commands"]
                noke.sendCommands(commandStr)
            else:
                print "Access Denied"

我觉得有错误是因为我将一个非 java 对象传递给一个 java 函数,但我已经尝试将该对象转换为“com.noke.nokemobilelibrary.NokeServiceListener”,但这也不起作用。代码在调试 2.5 之后总是出错,所以这两个步骤之间出了点问题,但我不知道是什么。

4

2 回答 2

0
class NokeServiceListener(PythonJavaClass):
    def __init__(self, callback):
        super().__init__()
        self.callback = callback

它将起作用,因为 python 不是 java / C#。除非您明确编写基类 init,否则不会调用它。

于 2018-05-14T13:05:54.977 回答
-1

添加

class NokeServiceListener(PythonJavaClass):
    def __init__(self, callback):
        super(NokeServiceListener, self).__init__()
        self.callback = callback

似乎解决了这个问题......但不知道为什么。

于 2018-05-12T03:57:03.820 回答