我使用 RaspberryPi 作为跟踪各种传感器数据的设备。现在我想创建一个移动应用程序(Flutter)来使用蓝牙低功耗(BLE)实时读取数据。
我知道有很多教程,但它们或多或少都比较复杂。我需要一个非常非常简单的 python 脚本来执行以下操作:
我尝试了 bluepy 并执行了以下操作:我创建了一个
Peripheral
对象,并每 5 秒无限循环地调用 peripheral.writeCharacteristic(0x0011, b"Hello World", withResponse=True)
。但是连接部分呢?
我还有一个问题:在哪里可以设置我的Raspberry与其他BLE设备连接的名称?因为现在当我使用 BLE Scanner 移动应用程序时,它会找到很多设备,但大多数设备的名称为“N/A”,而我的 Raspberry 没有出现。
这里有一个Raspberry Pi Python蓝牙库
https://github.com/petzval/btferret
有关 LE 服务器详细信息和 le_server.py 文件,请参阅文档中的第 3.7 节。此代码设置服务器及其特征,并每 5 秒发送一次通知。必须首先从客户端启用通知。 Pi 名称在 devices.txt 文件中设置(此处为我的 Pi)。
#Separate devices.txt file defines characteristics
#
#DEVICE = My Pi TYPE=MESH NODE=1 ADDRESS = B8:27:EB:F1:50:C3
# PRIMARY_SERVICE = 1800
# LECHAR = Device name PERMIT=06 SIZE=06 UUID=2A00 ; index 0
# PRIMARY_SERVICE = 112233445566778899AABBCCDDEEFF00
# LECHAR = My data PERMIT=16 SIZE=16 UUID=ABCD ; index 1 notify
import btfpy
def callback(clientnode,operation,cticn):
if(operation == btfpy.LE_CONNECT):
# clientnode has just connected
print("Connected")
elif(operation == btfpy.LE_DISCONNECT):
# clientnode has just disconnected
print("Disconnected")
return(btfpy.SERVER_EXIT)
elif(operation == btfpy.LE_TIMER):
# The server timer calls here every timerds deci-seconds
# clientnode and cticn are invalid
# This is called by the server not a client
# Writing the characteristic sends it as a notification
btfpy.Write_ctic(btfpy.Localnode(),1,"Hello world",0)
pass
return(btfpy.SERVER_CONTINUE)
if btfpy.Init_blue("devices.txt") == 0:
exit(0)
# Set My data (index 1) value
btfpy.Write_ctic(btfpy.Localnode(),1,"Hello world",0)
btfpy.Le_server(callback,50) # timerds = 5 seconds
btfpy.Close_all()