1. <tfoot id='24CVI'></tfoot>

      <legend id='24CVI'><style id='24CVI'><dir id='24CVI'><q id='24CVI'></q></dir></style></legend>

      <small id='24CVI'></small><noframes id='24CVI'>

    2. <i id='24CVI'><tr id='24CVI'><dt id='24CVI'><q id='24CVI'><span id='24CVI'><b id='24CVI'><form id='24CVI'><ins id='24CVI'></ins><ul id='24CVI'></ul><sub id='24CVI'></sub></form><legend id='24CVI'></legend><bdo id='24CVI'><pre id='24CVI'><center id='24CVI'></center></pre></bdo></b><th id='24CVI'></th></span></q></dt></tr></i><div id='24CVI'><tfoot id='24CVI'></tfoot><dl id='24CVI'><fieldset id='24CVI'></fieldset></dl></div>

        • <bdo id='24CVI'></bdo><ul id='24CVI'></ul>
      1. 如何在 Kivy 中将 android.bluetooth.socket 输入流转换为 python 字符串?

        how to convert android.bluetooth.socket inputstream to python string in Kivy?(如何在 Kivy 中将 android.bluetooth.socket 输入流转换为 python 字符串?)
        <tfoot id='ZLtJD'></tfoot>

        <legend id='ZLtJD'><style id='ZLtJD'><dir id='ZLtJD'><q id='ZLtJD'></q></dir></style></legend>
          <bdo id='ZLtJD'></bdo><ul id='ZLtJD'></ul>
              • <small id='ZLtJD'></small><noframes id='ZLtJD'>

                <i id='ZLtJD'><tr id='ZLtJD'><dt id='ZLtJD'><q id='ZLtJD'><span id='ZLtJD'><b id='ZLtJD'><form id='ZLtJD'><ins id='ZLtJD'></ins><ul id='ZLtJD'></ul><sub id='ZLtJD'></sub></form><legend id='ZLtJD'></legend><bdo id='ZLtJD'><pre id='ZLtJD'><center id='ZLtJD'></center></pre></bdo></b><th id='ZLtJD'></th></span></q></dt></tr></i><div id='ZLtJD'><tfoot id='ZLtJD'></tfoot><dl id='ZLtJD'><fieldset id='ZLtJD'></fieldset></dl></div>
                  <tbody id='ZLtJD'></tbody>

                  本文介绍了如何在 Kivy 中将 android.bluetooth.socket 输入流转换为 python 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发 Kivy 应用程序.

                  I'm working on Kivy app.

                  因为我想从蓝牙适配器获取数据,所以我使用了下面的代码.

                  Since I want to get data from bluetooth adapter, I used code below.

                  from kivy.app import App
                  from kivy.uix.label import Label
                  from kivy.uix.scatter import Scatter
                  from kivy.properties import ObjectProperty,NumericProperty
                  from kivy.clock import Clock
                  from kivy.lang import Builder
                  from jnius import cast,autoclass
                  from kivy.logger import Logger
                  
                  BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
                  bufferedreader = autoclass('android.bluetooth.BluetoothAdapter')
                  BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
                  BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
                  InputStreamReader = autoclass('java.io.InputStreamReader')
                  BufferedReader = autoclass('java.io.BufferedReader')
                  UUID = autoclass('java.util.UUID')
                  StringBuilder = autoclass('java.lang.StringBuilder')
                  
                  Builder.load_string('''
                  <bluetooth>:
                      Button:
                          pos:root.width/3,root.height/2
                          text: root.data
                          size: (300,100)
                  
                  
                  ''')
                  
                  class bluetooth(Scatter):
                      socket = ObjectProperty(None,allownone = True)
                      data = ObjectProperty('getting data',allownone = True)
                      recv = ObjectProperty(None,allownone = True)
                      counter = NumericProperty(0)
                  
                      def change_data(self,dt):
                          Logger.info('Im in the change_data!!')
                          self.data = 'change_data'
                          paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
                          for device in paired_devices:
                              self.data = str(device.getName())
                              Logger.info('Im in the loop!!'+str(device))
                              if device.getName() == 'HC-06':
                  
                                  self.socket = device.createRfcommSocketToServiceRecord(UUID.fromString('00001101-0000-1000-8000-00805F9B34FB'))
                                  bufferedreader = BufferedReader(InputStreamReader(self.socket.getInputStream(),"UTF-8"))
                  
                                  StringBuilder.append(bufferedreader.read())
                                  self.data = StringBuilder.toString()
                  
                          #if self.socket == None:
                          #   pass
                          #else:
                          #   self.socket.connect()
                  class myApp(App):
                      def build(self):
                          bt = bluetooth()
                          Clock.schedule_interval(bt.change_data,1)
                          return bt
                  myApp().run()
                  

                  也许我错过了一些代码..我不知道如何将 bluetooth.socket 输入流转换为 python 字符串.有人可以帮忙吗?

                  Maybe I missed some code.. I can't find out how to get bluetooth.socket inputstream to python string. Can someone please help?

                  推荐答案

                  我终于找到了一个似乎可行的解决方案.我有一个 Kivy 应用程序通过蓝牙与基于 Arduino 的设备通信.在 Arduino 上,我使用 SerialCommand 库来接收自定义命令并做出相应的响应.当命令在主线程中发送到我的 Arduino 时,我有一个带有循环的第二个线程,它从我的蓝牙套接字读取 InputStream.来自 Arduino 的响应包含在 <> 中,当我得到正确的响应时,我会提取括号之间的文本并将其发送到我的主线程中的一个函数.我希望这对你有帮助.

                  I have finally find a solution that seems to work. I have a Kivy app communicating with an Arduino based device over Bluetooth. On the Arduino I use the SerialCommand library to recieve custom commands and respond accordingly. While the commands is send to my Arduino in the main thread, I have a second thread with a loop that reads the InputStream from my Bluetooth socket. The response from Arduino is enclosed with <>, and when I get a proper response I extract the text between the brackets and send it to a function in my mainthread. I hope this is helpful for you.

                  from kivy.clock import mainthread
                  import threading
                  import jnius
                  
                  def get_socket_stream(self, name):
                      paired_devices =  self.BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
                      socket = None
                      for device in paired_devices:
                          if device.getName() == name:
                              socket = device.createRfcommSocketToServiceRecord(
                              self.UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
                              reader = self.InputStreamReader(socket.getInputStream(), 'US-ASCII')
                              recv_stream = self.BufferedReader(reader)
                              send_stream = socket.getOutputStream()
                              break
                      socket.connect()
                      return recv_stream, send_stream
                  
                  def connect(self, *args):
                      device = self.config.get('bluetooth', 'bt_name')
                      try:
                          self.recv_stream, self.send_stream = self.get_socket_stream(device)
                      except AttributeError as e:
                          print e.message
                          return False
                      except jnius.JavaException as e:
                          print e.message
                          return False
                      except:
                          print sys.exc_info()[0]
                          return False
                  
                      threading.Thread(target=self.stream_reader).start()
                  
                  def stream_reader(self, *args):
                      stream = ''
                      while True:
                          if self.stop.is_set():
                              jnius.detach()
                              return
                          if self.recv_stream.ready():
                              try:
                                  stream = self.recv_stream.readLine()
                              except self.IOException as e:
                                  print "IOException: ", e.message
                              except jnius.JavaException as e:
                                  print "JavaException: ", e.message
                              except:
                                  print "Misc error: ", sys.exc_info()[0]
                  
                              try:
                                  start = stream.rindex("<") + 1
                                  end = stream.rindex(">", start)
                                  self.got_response(stream[start:end])
                              except ValueError:
                                  pass
                  
                  @mainthread
                  def got_response(self, response):
                      do something...
                  

                  这篇关于如何在 Kivy 中将 android.bluetooth.socket 输入流转换为 python 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

                      <bdo id='PpkJk'></bdo><ul id='PpkJk'></ul>

                    • <tfoot id='PpkJk'></tfoot>

                      1. <legend id='PpkJk'><style id='PpkJk'><dir id='PpkJk'><q id='PpkJk'></q></dir></style></legend>

                          <small id='PpkJk'></small><noframes id='PpkJk'>

                            <i id='PpkJk'><tr id='PpkJk'><dt id='PpkJk'><q id='PpkJk'><span id='PpkJk'><b id='PpkJk'><form id='PpkJk'><ins id='PpkJk'></ins><ul id='PpkJk'></ul><sub id='PpkJk'></sub></form><legend id='PpkJk'></legend><bdo id='PpkJk'><pre id='PpkJk'><center id='PpkJk'></center></pre></bdo></b><th id='PpkJk'></th></span></q></dt></tr></i><div id='PpkJk'><tfoot id='PpkJk'></tfoot><dl id='PpkJk'><fieldset id='PpkJk'></fieldset></dl></div>

                              <tbody id='PpkJk'></tbody>