首页 > 解决方案 > 如何将十进制值1从bluepy写入ble设备

问题描述

我有这段代码,我想使用 bluepy 从 python 脚本将十进制值 1 写入 ble 设备:

#!/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

标签: pythonpython-2.7bluetooth-lowenergy

解决方案


推荐阅读