JVM 异常 occured:Need 蓝牙权限:当前进程的用户 10258 都没有 android.permission.Bluetooth python jnius?
JVM exception occured:Need Bluetooth permission: Neither user 10258 not current process has android.permission.Bluetooth python jnius?
我想创建蓝牙应用程序,我有 .apk
包含此代码的文件:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from jnius import autoclass
from kivy.uix.label import Label
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
class Bluetooth():
def __init__(nameOfDevice):
self.recv_stream, self.send_stream = get_socket_stream(nameOfDevice)
def send(self, cmd):
self.send_stream.write('{}\n'.format(cmd))
self.send_stream.flush()
class ExampleApp(App):
def build(self):
global BluetoothAdapter, BluetoothDevice, BluetoothSocket, UUID
self.fl = FloatLayout()
try:
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')
get_socket_stream('DESKTOP-I2CNCPQ')
self.fl.add_widget(Label(text='no errors', pos=(0, 0), font_size=(40)))
except Exception as error:
self.fl.add_widget(Label(text=str(error), pos=(0, 0), font_size=(20)))
return self.fl
if __name__ == '__main__':
ExampleApp().run()
我得到错误:
JVM exception occured:Need Bluetooth permission: Neither user 10258 not current process has android.permission.Bluetooth
我该如何解决?我没有在 python 中找到如何修复它的答案,希望你能帮助我...................... ...................................
当您缺少权限时,您必须在 .p4a 文件中或通过命令行添加它们
--permission BLUETOOTH
会有帮助。
可能你需要
--permission BLUETOOTH_ADMIN
还有。
如果您使用 buildozer,则必须添加
android.permissions = BLUETOOTH_ADMIN,BLUETOOTH
在buildozer.spec
我想创建蓝牙应用程序,我有 .apk
包含此代码的文件:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from jnius import autoclass
from kivy.uix.label import Label
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
class Bluetooth():
def __init__(nameOfDevice):
self.recv_stream, self.send_stream = get_socket_stream(nameOfDevice)
def send(self, cmd):
self.send_stream.write('{}\n'.format(cmd))
self.send_stream.flush()
class ExampleApp(App):
def build(self):
global BluetoothAdapter, BluetoothDevice, BluetoothSocket, UUID
self.fl = FloatLayout()
try:
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')
get_socket_stream('DESKTOP-I2CNCPQ')
self.fl.add_widget(Label(text='no errors', pos=(0, 0), font_size=(40)))
except Exception as error:
self.fl.add_widget(Label(text=str(error), pos=(0, 0), font_size=(20)))
return self.fl
if __name__ == '__main__':
ExampleApp().run()
我得到错误:
JVM exception occured:Need Bluetooth permission: Neither user 10258 not current process has android.permission.Bluetooth
我该如何解决?我没有在 python 中找到如何修复它的答案,希望你能帮助我...................... ...................................
当您缺少权限时,您必须在 .p4a 文件中或通过命令行添加它们
--permission BLUETOOTH
会有帮助。 可能你需要
--permission BLUETOOTH_ADMIN
还有。
如果您使用 buildozer,则必须添加
android.permissions = BLUETOOTH_ADMIN,BLUETOOTH
在buildozer.spec