本文主要是介绍树莓派蓝牙BLE做从机Peripheral,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装环境
安装bluez
sudo apt install bluez
安装python包bluez-peripheral
pip install bluez-peripheral
如果安装失败,可以下载源代码安装
git clone https://github.com/spacecheese/bluez_peripheral.git
cd bluez_peripheral
python setup.py install
示例代码
from bluez_peripheral.gatt.service import Service
from bluez_peripheral.gatt.characteristic import characteristic, CharacteristicFlags as CharFlags
from bluez_peripheral.gatt.descriptor import descriptor, DescriptorFlags as DescFlags# Define a service like so.
class MyService(Service):def __init__(self):self._some_value = None# Call the super constructor to set the UUID.super().__init__("BEEF", True)# Use the characteristic decorator to define your own characteristics.# Set the allowed access methods using the characteristic flags.@characteristic("BEF0", CharFlags.READ)def my_readonly_characteristic(self, options):# Characteristics need to return bytes.return bytes("Hello World!", "utf-8")# This is a write only characteristic.@characteristic("BEF1", CharFlags.WRITE)def my_writeonly_characteristic(self, options):# This function is a placeholder.# In Python 3.9+ you don't need this function (See PEP 614)pass# In Python 3.9+:# @characteristic("BEF1", CharFlags.WRITE).setter# Define a characteristic writing function like so.@my_writeonly_characteristic.setterdef my_writeonly_characteristic_cb(self, value, options):# Your characteristics will need to handle bytes.self._some_value = valueprint(value)# Associate a descriptor with your characteristic like so.# Descriptors have largely the same flags available as characteristics.@descriptor("BEF2", my_readonly_characteristic, DescFlags.READ)# Alternatively you could write this:# @my_writeonly_characteristic.descriptor("BEF2", DescFlags.READ)def my_readonly_descriptors(self, options):# Descriptors also need to handle bytes.return bytes("This characteristic is completely pointless!", "utf-8")from bluez_peripheral.util import *
from bluez_peripheral.advert import Advertisement
from bluez_peripheral.agent import NoIoAgent
import asyncioasync def main():# Alternativly you can request this bus directly from dbus_next.bus = await get_message_bus()service = MyService()await service.register(bus)# An agent is required to handle pairing agent = NoIoAgent()# This script needs superuser for this to work.await agent.register(bus)adapter = await Adapter.get_first(bus)my_service_ids = ["BEEF"] # The services that we're advertising.my_appearance = 0 # The appearance of my service.# See https://specificationrefs.bluetooth.com/assigned-values/Appearance%20Values.pdfmy_timeout = 0 # never timeoutadvert = Advertisement("My Device Name", my_service_ids, my_appearance, my_timeout)await advert.register(bus, adapter)while True:await asyncio.sleep(5)if __name__ == "__main__":asyncio.run(main())
问题一:频繁提示配对
关闭电池电量请求,在/usr/lib/systemd/system/bluetooth.service中的ExecStart这一行最后添加 -P battery
systemctl daemon-reload
systemctl restart bluetooth
问题二:raspberrypi zero 2w使用NoIoAgent时手机端配对失败
这篇关于树莓派蓝牙BLE做从机Peripheral的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!