android嵌入python_python调用android API

这篇博客探讨了在Kivy中使用Python调用Android蓝牙API的两种方法——Plyer和JNIUS。尽管Plyer提供跨平台的便利,但它不支持蓝牙功能,因此对于蓝牙操作仍需使用JNIUS。文中给出了一个使用JNIUS控制Android蓝牙的示例代码,展示了如何连接和发送数据。作者指出,目前使用Python开发Android程序会遇到一些挑战,但Python语法的易读性是一大优点。
摘要由CSDN通过智能技术生成

kivy提供了两种方法调用android API——plyer和jnius

plyer 封装了jnius(android)、Pyobjus(ios)和一些桌面系统的API

jnius更为底层,直接调用java api

从这里看,为了更好的跨平台,我们应该尽量使用plyer,但可能api会少一些,一些特定的地方还是要使用jnius去操作。

plyer资料:

https://github.com/kivy/plyer

https://plyer.readthedocs.io

plyer当前版本的api支持情况:

plyer-support.jpg

可惜并不支持android蓝牙,所以要操作蓝牙还是得用jnius

以下是网上找到的操作蓝牙代码,先贴着后面再研究

要通过python控制android蓝牙,需先通过Pyjnius去控制android java api…….

蛋疼蛋疼蛋疼,不想写java就是这样~

来自网上的示例:

'''

Bluetooth/Pyjnius example

=========================

This was used to send some bytes to an arduino via bluetooth.

The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn't

tested without BLUETOOTH_ADMIN, maybe it works.)

Connect your device to your phone, via the bluetooth menu. After the

pairing is done, you'll be able to use it in the app.

'''

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__':

recv_stream, send_stream = get_socket_stream('linvor')

send_stream.write('hello\n')

send_stream.flush()

# Same as before, with a kivy-based UI

'''

Bluetooth/Pyjnius example

=========================

This was used to send some bytes to an arduino via bluetooth.

The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn't

tested without BLUETOOTH_ADMIN, maybe it works.)

Connect your device to your phone, via the bluetooth menu. After the

pairing is done, you'll be able to use it in the app.

'''

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()

所以说现阶段,使用python开发android程序,非但不会方便,还会有各种问题。唯一的好处只是python看着舒服点。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值