kivy jnius未找到课程

时间:2015-09-25 05:48:24

标签: android python bluetooth kivy

我搜索kivy bluetooth示例,找到这些代码,然后复制粘贴,但不能正常工作。我应该怎么做才能解决问题。感谢。

版本:

python:2.7

kivy:1.9

jnius:1.0.2

来源:https://gist.github.com/tito/7432757

错误消息:

Traceback (most recent call last):
  File "/home/frank/workspace/798/bluetooth.py", line 13, in <module>
    BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
  File "/home/frank/package/kivyPython2Venv/local/lib/python2.7/site-packages/jnius/reflect.py", line 106, in autoclass
    c = find_javaclass(clsname)
  File "jnius_export_func.pxi", line 23, in jnius.find_javaclass (jnius/jnius.c:7190)
jnius.JavaException: Class not found 'android/bluetooth/BluetoothAdapter'

bluetooth.py

from jnius import autoclass

BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')

def get_socket_stream(name):
    paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
    socket = None
    for device in paired_devices:
        if device.getName() == name:
            socket = device.createRfcommSocketToServiceRecord(
                UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
            recv_stream = socket.getInputStream()
            send_stream = socket.getOutputStream()
            break
    socket.connect()
    return recv_stream, send_stream

if __name__ == '__main__':
    kv = '''
BoxLayout:
    Button:
        text: '0'
        on_release: app.reset([b1, b2, b3, b4, b5])
    ToggleButton:
        id: b1
        text: '1'
        on_release: app.send(self.text)
    ToggleButton:
        id: b2
        text: '2'
        on_release: app.send(self.text)
    ToggleButton:
        id: b3
        text: '3'
        on_release: app.send(self.text)
    ToggleButton:
        id: b4
        text: '4'
        on_release: app.send(self.text)
    ToggleButton:
        id: b5
        text: '5'
        on_release: app.send(self.text)
    '''
    from kivy.lang import Builder
    from kivy.app import App

    class Bluetooth(App):
        def build(self):
            self.recv_stream, self.send_stream = get_socket_stream('linvor')
            return Builder.load_string(kv)

        def send(self, cmd):
            self.send_stream.write('{}\n'.format(cmd))
            self.send_stream.flush()

        def reset(self, btns):
            for btn in btns:
                btn.state = 'normal'
            self.send('0\n')

    Bluetooth().run()

1 个答案:

答案 0 :(得分:0)

如果您使用Qpython,您还必须导入Kivy,因为PyJnius只是Kivy的一部分。在这种情况下使用:

#qpy:kivy
from jnius import autoclass

如果您使用Py4A编译了自己的发行版,我认为编译时出错。