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

通过蓝牙从Raspberry Pi Zero W向iOS应用程序发送信息

  •  0
  • Skyler  · 技术社区  · 3 年前

    我正在尝试从我的树莓派向我的iPhone发送数据。我在Pi上设置了蓝牙,我可以在iPhone上连接到它。在我下载的蓝牙检查器应用程序中,我可以清楚地看到Pi正在宣传的服务的UUID,但上面写着“服务已经发布广告,但尚未发现”

    这是我的代码

    from pybleno import *
    import array
    import sys
    from builtins import str
    from builtins import range
    import threading
    import time
    import random
    
    class EchoCharacteristic(Characteristic):
    
      def __init__(self):
        Characteristic.__init__(
          self, {
            'uuid': 'a64e8b82-4b3c-444a-b379-0a066c910740',
            'properties': ['read', 'write', 'notify'],
            'value': None
          })
    
        self._value = array.array('B', [0] * 0)
        self._updateValueCallback = None
    
      def onReadRequest(self, offset, callback):
        print('EchoCharacteristic - onReadRequest: value = ' + str(self._value))
        callback(Characteristic.RESULT_SUCCESS, self._value[offset:])
    
      def onWriteRequest(self, data, offset, withoutResponse, callback):
        self._value = data
        print('EchoCharacteristic - onWriteRequest: value = ' + str(self._value))
    
        if self._updateValueCallback:
          print('EchoCharacteristic - onWriteRequest:     notifying')
          self._updateValueCallback(self._value)
        callback(Characteristic.RESULT_SUCCESS)
    
      def onSubscribe(self, maxValueSize, updateValueCallback):
        print('EchoCharacteristic - onSubscribe')
        self._updateValueCallback = updateValueCallback
    
      def onUnsubscribe(self):
        print('EchoCharacteristic - onUnsubscribe')
        self._updateValueCallback = None
      def update(self, newValue):
        self._value = array.array('B', newValue)
        if self._updateValueCallback:
          self._updateValueCallback(self._value)
    
    def getData():
      return [random.randint(0, 100)]
    
    def main():
      bleno = Bleno()
      echoCharacteristic = EchoCharacteristic()
      def updateCharacteristic():     # background thread function
        while True:
          newValue = getData()
          echoCharacteristic.update(newValue)
          time.sleep(1)
      thread = threading.Thread(target=updateCharacteristic)
      thread.start()
      
      def onStateChange(state):
        print('on -> stateChange: ' + state)
        if state == 'poweredOn':
          bleno.startAdvertising('Echo', ['a64e8b82-4b3c-444a-b379-0a066c910741'])
        elif state == 'poweredOff':
          bleno.stopAdvertising()
    
      bleno.on('stateChange', onStateChange)
    
      def onAdvertisingStart(error):
        print('on -> advertisingStart: ' +
              ('error ' + str(error) if error else 'success'))
        if not error:
          bleno.setServices([
            BlenoPrimaryService({
              'uuid': 'a64e8b82-4b3c-444a-b379-0a066c910741',
              'characteristics': [echoCharacteristic]
            })
          ])
    
      bleno.on('advertisingStart', onAdvertisingStart)
    
      def onDisconnect(clientAddress):
        bleno.stopAdvertising()
    
      bleno.on('disconnect', onDisconnect)
    
      bleno.start()
    
      print('Hit <ENTER> to disconnect')
      input()
      bleno.stopAdvertising()
      bleno.disconnect()
    
    
    if __name__ == "__main__":
      main()
    
    

    它应该只是发送一个每秒更新一次的随机数。UUID是随机的。任何建议都是恰当的。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Skyler    3 年前

    这可能不是任何人想要的解决方案,但我通过重新使用NodeJS而不是python来解决这个问题,并且它起到了作用。我几乎为代码做了一个精确的端口,并修复了它。我必须使用节点8.9.0并安装python 2.7(用于其中一个依赖项),所以Bleno肯定有点过时了,但它解决了问题。如果您仍在尝试使用python运行它,也许可以尝试安装python 2?