{"id":17096,"date":"2023-12-15T11:06:36","date_gmt":"2023-12-15T03:06:36","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=17096"},"modified":"2023-12-15T11:06:36","modified_gmt":"2023-12-15T03:06:36","slug":"python%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app-%e8%93%9d%e7%89%99%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/17096\/","title":{"rendered":"python\u5f00\u53d1\u5b89\u5353app \u84dd\u7259\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"

\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\u5e76\u5b9e\u73b0\u84dd\u7259\u529f\u80fd\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u5373\u4f7f\u662f\u521d\u5b66\u8005\u4e5f\u80fd\u8f7b\u677e\u8ddf\u4e0a\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u6709\u5173\u7528Python\u8fdb\u884cAndroid\u5f00\u53d1\u7684\u4fe1\u606f\u3002Kivy\u548cBeeWare\u662f\u4e24\u4e2a\u6d41\u884c\u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728Android\u4e2d\u4f7f\u7528Python\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Kivy\u3002<\/p>\n

1. \u4f7f\u7528Kivy\u8fdb\u884cAndroid\u5f00\u53d1<\/p>\n

Kivy\u662f\u4e00\u4e2a\u5f00\u653e\u6e90\u4ee3\u7801\u7684Python\u5e93\uff0c\u7528\u4e8e\u5f00\u53d1\u591a\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528Kivy\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u5bf9Java\u3001kotlin\u6216XML\u7684\u539f\u751fAndroid\u7f16\u7a0b\u6709\u592a\u591a\u7684\u7406\u89e3\uff0c\u5c31\u80fd\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u84dd\u7259\u529f\u80fd\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

2. \u5f00\u53d1\u73af\u5883\u8bbe\u7f6e<\/p>\n

\u8981\u5f00\u59cb\u4f7f\u7528Kivy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Python\uff0c\u7136\u540e\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`<\/p>\n

pip install kivy<\/p>\n

“`<\/p>\n

\u8981\u6784\u5efaAndroid\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5b89\u88c5Buildozer\u5de5\u5177\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n

“`<\/p>\n

pip install buildozer<\/p>\n

“`<\/p>\n

3. Python\u84dd\u7259\u901a\u4fe1<\/p>\n

Pryo\u7684PyBluez\u662f\u5b9e\u73b0Python\u84dd\u7259\u901a\u4fe1\u7684\u4e00\u79cd\u7b80\u5355\u65b9\u6cd5\u3002\u5bf9\u4e8e\u672c\u6559\u7a0b\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n

“`<\/p>\n

pip install pybluez<\/p>\n

“`<\/p>\n

4. \u7f16\u5199\u4e00\u4e2a\u84dd\u7259\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`main.py`\u7684\u6587\u4ef6\uff0c\u5e76\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528\u7a0b\u5e8f\u3002\u7531\u4e8ePyBluez\u5e93\u5728Android\u4e0a\u5e76\u4e0d\u603b\u662f\u53ef\u7528\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Java\u539f\u751f\u4ee3\u7801\uff08jnius\uff09\u5728\u793a\u4f8b\u4e2d\u521b\u5efa\u84dd\u7259\u5957\u63a5\u5b57\u3002<\/p>\n

“`python<\/p>\n

from kivy.app import App<\/p>\n

from kivy.uix.button import Button<\/p>\n

from kivy.uix.label import Label<\/p>\n

from kivy.uix.boxlayout import BoxLayout<\/p>\n

from jnius import autoclass<\/p>\n

class BluetoothExampleApp(App):<\/p>\n

\tdef build(self):<\/p>\n

\t\t# \u5e03\u5c40<\/p>\n

\t\tlayout = BoxLayout(orientation=’vertical’, spacing=10)<\/p>\n

\t\t# \u67e5\u627e\u5e76\u8fde\u63a5\u6309\u94ae<\/p>\n

\t\tself.connect_button = Button(text=’Find and Connect’)<\/p>\n

\t\tself.connect_button.bind(on_release=self.find_devices)<\/p>\n

\t\tlayout.add_widge<\/p>\n

<\/figure>\n<\/p>\n

t(self.connect_button)<\/p>\n

\t\t# \u84dd\u7259\u8fde\u63a5\u72b6\u6001\u6807\u7b7e<\/p>\n

\t\tself.status_label = Label(text=’Not connected.’)<\/p>\n

\t\tlayout.add_widget(self.status_label)<\/p>\n

\t\treturn layout<\/p>\n

\tdef find_devices(self, instance):<\/p>\n

\t\tself.status_label.text = ‘Searching for devices…’<\/p>\n

\t\tself.connect_device() # \u8fd9\u91cc\u5c06\u8c03\u7528\u84dd\u7259\u8bbe\u5907\u8fde\u63a5\u903b\u8f91<\/p>\n

\tdef connect_device(self):<\/p>\n

\t\t# \u7f16\u5199\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u4ee3\u7801\uff0c\u4f8b\u5982\u4f7f\u7528 jnius \u548c\u84dd\u7259\u8bbe\u5907\u5730\u5740<\/p>\n

\t\tpass<\/p>\n

if __name__ == ‘__main__’:<\/p>\n

\tBluetoothExampleApp().run\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>()<\/p>\n

“`<\/p>\n

\u8fd9\u4e2a\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\u5305\u542b\u4e00\u4e2a\u6309\u94ae\u548c\u4e00\u4e2a\u72b6\u6001\u6807\u7b7e\u3002\u5f53\u7528\u6237\u5355\u51fb\u201c\u67e5\u627e\u5e76\u8fde\u63a5\u201d\u6309\u94ae\u65f6\uff0c\u901a\u8fc7`find_devices`\u65b9\u6cd5\u6765\u67e5\u627e\u53ef\u7528\u7684\u84dd\u7259\u8bbe\u5907\u5e76\u521b\u5efa\u8fde\u63a5\u3002\u5b9e\u65bd\u7ec6\u8282\u6709\u5f85\u8865\u5145\u3002<\/p>\n

5. \u6784\u5efaAndroid\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`buildozer.spec`\u7684\u6587\u4ef6\u5e76\u5728\u5176\u4e2d\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

[app]<\/p>\n

title = BluetoothExample<\/p>\n

package.name = bluetoothexample<\/p>\n

source.dir = .<\/p>\n

requirements = python3,kivy,jnius,pybluez<\/p>\n

orientation = portrait<\/p>\n

[buildozer]<\/p>\n

libraries = sdl2_ttf,sdl2_image,sdl2_mixer,sdl2_gfx<\/p>\n

android.permissions = BLUETOOTH, BLUETOOTH_ADMIN, INTERNET, ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION<\/p>\n

“`<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u5728\u7ec8\u5b89\u5353app<\/a>\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u6784\u5efa\uff1a<\/p>\n

“`<\/p>\n

buildozer android debug deploy run<\/p>\n

“`<\/p>\n

\u6574\u4e2a\u6784\u5efa\u8fc7\u7a0b\u53ef\u80fd\u8981\u82b1\u8d39\u4e00\u5b9a\u7684\u65f6\u95f4\u3002\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u4f60\u7684Android\u8bbe\u5907\u4e0a\u5c06\u5b89\u88c5\u5e76\u8fd0\u884c\u65b0\u521b\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

\u73b0\u5728\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\uff0c\u5305\u542b\u4e00\u4e2a\u6309\u94ae\u548c\u4e00\u4e2a\u72b6\u6001\u6807\u7b7e\u3002\u8fd9\u4ec5\u4ec5\u662f\u4e00\u4e2a\u8d77\u70b9\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0`connect_device`\u65b9\u6cd5\u5e76\u6dfb\u52a0\u754c\u9762\u5143\u7d20\u6765\u6269\u5c55\u8be5\u793a\u4f8b\u4ee5\u6ee1\u8db3\u9879\u76ee\u9700\u6c42\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b66\u4e60\u66f4\u591a\u5173\u4e8eKivy\u3001JNIus\u548c\u84dd\u7259\u901a\u4fe1\u7684\u77e5\u8bc6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1Android\u5e94\u7528\u7a0b\u5e8f\u5e76\u5b9e\u73b0\u84dd\u7259\u529f\u80fd\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8bbe\u7f6e\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u5373\u4f7f\u662f\u521d\u5b66\u8005\u4e5f\u80fd\u8f7b\u677e\u8ddf\u4e0a\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u6709\u5173\u7528Python\u8fdb\u884cAndroid\u5f00\u53d1\u7684<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[23501,14,23500,5309,3249],"class_list":{"0":"post-17096","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","8":"tag-14","10":"tag-5309","11":"tag-3249"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=17096"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17096\/revisions"}],"predecessor-version":[{"id":17141,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17096\/revisions\/17141"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=17096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=17096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=17096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}