代码之家  ›  专栏  ›  技术社区  ›  marciokoko

如何将bluepy中的十进制值1写入ble设备

  •  0
  • marciokoko  · 技术社区  · 5 年前

    我有一段代码,我想用bluepy将十进制值1从python脚本写入一个可编程设备:

    #!/usr/bin/env python
    
    import bluepy.btle as btle
    print ("outside")
    
    def letsgobaby():
      print ("turning1on")
      p = btle.Peripheral("bb:00:00:15:27:19") #zl-rc04a
    
      # GET SERVICES
      services=p.getServices()
      for service in services:
         print service
    
      # NORMAL get to write to ...
      s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb") #ffe0
      c = s.getCharacteristics()[0]
    
      #1 is relay1on, 2 is relay1off
      c.write(1)
      p.disconnect()
    
    if __name__ == "__main__":
      letsgobaby()
    

    但我有个错误:

    pi@raspberrypi:~/Documents/python/failedPythonBLE $ python 1on.py
    outside
    turning1on
    Hallo from Peripheral
    discovering services
    Hello
    Service <uuid=Generic Attribute handleStart=8 handleEnd=11>
    Service <uuid=Generic Access handleStart=1 handleEnd=7>
    commonName= 0000ffe0-0000-1000-8000-00805f9b34fb
    commonName= ffe0
    Service <uuid=ffe0 handleStart=12 handleEnd=17>
    17
    Traceback (most recent call last):
    File "1on.py", line 43, in <module>
    letsgobaby()
    File "1on.py", line 39, in letsgobaby
    c.write(1)
    File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 168, in write
    return self.peripheral.writeCharacteristic(self.valHandle, val, withResponse)
    File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 519, in writeCharacteristic
    self._writeCmd("%s %X %s\n" % (cmd, handle, binascii.b2a_hex(val).decode('utf-8')))
    TypeError: must be string or buffer, not int
    
    0 回复  |  直到 5 年前