使用 CircuitPython 将值发送到 BLE(蓝牙低功耗)设备

问题描述 投票:0回答:1

我正在尝试与索尼相机通信,并通过使用 adafruit 库运行 CircuitPython 的 ESP32-S3 通过 BLE 触发它。

根据我找到的文档,我需要将值“0x0107”写入服务“8000ff00-ff00-ffff-ffff-ffffffffffff”上的特征“0000ff01-0000-1000-8000-00805f9b34fb”

我似乎能够连接到相机,不幸的是我似乎找不到写入自定义服务/特性的方法。

我尝试使用不同的 UUID 格式以及获取所需对象的处理程序的方法。 设置值的行失败。

这是我写的代码:

import time
import board
import busio
import adafruit_ble
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.device_info import DeviceInfoService
from adafruit_ble.uuid import VendorUUID, UUID
SERVICE = "8000ff00-ff00-ffff-ffff-ffffffffffff" 
# CHARACTERISTIC = "0000ff01-0000-1000-8000-00805f9b34fb"
CHARACTERISTIC = "0xff01"
# Set up the BLE radio
ble = adafruit_ble.BLERadio()
COM1 = 0x0107
COM1A= bytes([0x01, 0x07])
COM2 = 0x0109
COM3 = 0x0108
COM4 = 0x0106


# Create the advertisement object
advertisement = ProvideServicesAdvertisement()

# Start advertising the services
ble.start_advertising(advertisement)

# Scan for the Sony a6100 camera and connect to it
while True:
    print("Scanning for Sony a6100 camera...")
    for device in ble.start_scan(timeout=5):
        if device.complete_name == "ILCE-6100":
            print("Found Sony a6100 camera, connecting...")
            connection = ble.connect(device)
            break
    ble.stop_scan()
    time.sleep(1)

    # Once connected, send the necessary commands to the camera to pair
    while not ble.connected:
      pass  
    print("Paired with Sony a6100 camera!")
    
    
    #dis = connection[DeviceInfoService]
    dis = connection[VendorUUID(SERVICE)]
    service_handler = connection._discovered_bleio_services[VendorUUID(SERVICE)]
    print("discovered:", service_handler)
    characteristics_handler = service_handler.characteristics
    for characteristic in characteristics_handler:
        print("CH:", type(characteristic), "|", characteristic, "uuid:", str(characteristic.uuid))
        if str(characteristic.uuid) == ("UUID("+CHARACTERISTIC+")"):
          print("CHARACTERISTIC FOUND")
          characteristic_handler=characteristic
    print("Sending commands")
    characteristic_handler.value(COM1)
    time.sleep(1)
python bluetooth bluetooth-lowenergy esp32 adafruit-circuitpython
1个回答
0
投票

我正在做类似的事情,尝试通过 BLE 将 nRf52840 板连接到索尼相机。只需查看您的代码,我的猜测是您在写入特征之前没有与相机配对。尝试在

connection.pair()
 之后致电 
connection = ble.connect(device)

如果这能解决您的问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.