我正在尝试从我的树莓派向我的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是随机的。任何建议都是恰当的。