树莓派蓝牙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.setter
   def my_writeonly_characteristic_cb(self, value, options):
      # Your characteristics will need to handle bytes.
      self._some_value = value
      print(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 asyncio

async 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.pdf
    my_timeout = 0 # never timeout
    advert = 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时手机端配对失败

// TODO:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值