M5 BLE

M5 BLE 是 M5Stack 通过封装 MicroPython 的底层 BLE 开发的一个库。它提供了更简单、更易用的 API。

BLE Client 应用示例:

# client
from m5ble import M5BLE

UUID_SERVICE1 = "6E400011-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE1_WR = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE1_RD = "6E400013-B5A3-F393-E0A9-E50E24DCCA9E"

UUID_SERVICE2 = "6E400021-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE2_WR = "6E400022-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE2_RD = "6E400023-B5A3-F393-E0A9-E50E24DCCA9E"

connected = False
def on_connected(client):
    global connected
    print(client._service_handle_map)
    connected = True
    ble.client.set_mtu(128)

def on_disconnected(client, conn_handle, addr_type, addr):
    global connected
    connected = False

ble = M5BLE.Device(verbose=True)
ble.client.scan(target_name_prefix="M5")
ble.client.on_connected(on_connected)

try:
    while True:
        if connected:
            ble.client.write("Hello Service 1", UUID_SERVICE1_WR, UUID_SERVICE1)
            ble.client.write("Hello Service 2", UUID_SERVICE2_WR, UUID_SERVICE2)
            time.sleep(0.1)
            print(ble.client.read(UUID_SERVICE1_RD, UUID_SERVICE1))
            print(ble.client.read(UUID_SERVICE2_RD, UUID_SERVICE2))
        time.sleep(1)
except KeyboardInterrupt:
    gc.collect()
    print("\nExiting...")

BLE Server 应用示例:

# server
from m5ble import M5BLE

UUID_SERVICE1 = "6E400011-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE1_WR = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE1_RD = "6E400013-B5A3-F393-E0A9-E50E24DCCA9E"

UUID_SERVICE2 = "6E400021-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE2_WR = "6E400022-B5A3-F393-E0A9-E50E24DCCA9E"
UUID_SERVICE2_RD = "6E400023-B5A3-F393-E0A9-E50E24DCCA9E"

def onReceive(server, client):
    print("onReceive")
    if client.any(UUID_SERVICE1_WR):
        client.write(client.read(UUID_SERVICE1_WR), UUID_SERVICE1_RD)
    if client.any(UUID_SERVICE2_WR):
        client.write(client.read(UUID_SERVICE2_WR), UUID_SERVICE2_RD)

ble = M5BLE.Device(verbose=True)
ble.server.add_service(UUID_SERVICE1, [
    ble.server.create_characteristic(UUID_SERVICE1_RD, notify=True, read=True),
    ble.server.create_characteristic(UUID_SERVICE1_WR, write=True),
])
ble.server.add_service(UUID_SERVICE2, [
    ble.server.create_characteristic(UUID_SERVICE2_RD, notify=True, read=True),
    ble.server.create_characteristic(UUID_SERVICE2_WR, write=True),
])
ble.server.start()
ble.server.on_receive(onReceive)
while True:
    pass

UiFlow2 应用示例:

UiFlow2 server 示例:

M5BLE

构造函数。

class M5BLE.Device(name)

创建一个 M5BLE 对象。

  • name BLE 广播名称,用于设备发现和识别。

UIFLOW2:

init.png

设备方法。

M5BLE.Device.get_mtu()

获取 BLE 连接的最大传输单元(MTU),以字节为单位。

UIFLOW2:

get_mtu.png

M5BLE.Device.deinit()

关闭 BLE 连接并释放资源。

UIFLOW2:

deinit.png

客户端方法

M5BLE.Device.client.on_connected(callback)

设置 BLE 连接成功的回调函数。

  • callback 连接时的回调函数,参数为 callback(M5BLE.Client)。

UIFLOW2:

client_on_connected.png

M5BLE.Device.client.on_disconnected(callback)

设置 BLE 断开连接的回调函数。

  • callback 断开连接时的回调函数,参数为 callback(M5BLE.Client, conn_handle, addr_type, addr)。

UIFLOW2:

client_on_disconnected.png

M5BLE.Device.client.on_server_found(callback)

设置用于 BLE 服务发现的回调函数。

  • callback 用于服务发现的回调函数,参数为 callback(M5BLE.Client, (name, addr_type, addr, adv_type, rssi, adv_data))。

UIFLOW2:

client_on_server_found.png

M5BLE.Device.client.on_scan_finished(callback)

设置 BLE 扫描结束的回调函数。

  • callback 扫描结束时的回调函数,参数为 callback(M5BLE.Client, scan_result=[])。

UIFLOW2:

client_on_scan_finished.png

M5BLE.Device.client.on_read_complete(callback)

设置用于 BLE 读取完成的回调函数。

  • callback 读取完成时的回调函数,参数为 callback(M5BLE.Client, conn_handle, value_handle, char_data)。

UIFLOW2:

client_on_read_complete.png

M5BLE.Device.client.on_notify(callback)

设置用于 BLE 通知的回调函数。

  • callback 通知回调函数,参数为 callback(M5BLE.Client)。

UIFLOW2:

client_on_notify.png

M5BLE.Device.client.scan(timeout=2000, connect_on_found=True, target_name_prefix='M5UiFlow', target_uuid=None)

扫描 BLE 设备。

  • timeout 扫描超时时间,单位为毫秒,默认值为 2000 ms。

  • connect_on_found 是否在找到设备时自动连接,默认为 True。

  • target_name_prefix 目标设备的名称前缀,默认值为 ‘M5UiFlow’。

  • target_uuid 目标设备的 UUID。

UIFLOW2:

client_scan.png

M5BLE.Device.client.connect(addr_type, addr)

连接到 BLE 设备。

  • addr_type 设备地址类型。

  • addr 设备地址。

UIFLOW2:

client_connect.png

M5BLE.Device.client.set_current_service_uuid(service_uuid)

为当前服务设置 UUID,使在使用 any、read、write 时可以省略 service_uuid 参数。

  • service_uuid 服务的 UUID。

UIFLOW2:

client_set_current_service_uuid.png

M5BLE.Device.client.any(char_uuid, service_uuid=None)

检查是否有可供读取的数据;如果有,则返回缓冲区的字节大小。

  • char_uuid 特征的 UUID。

  • service_uuid 服务的 UUID。

UIFLOW2:

client_any.png

M5BLE.Device.client.read(char_uuid, service_uuid=None, sz=None)

从 BLE 设备读取数据。

  • char_uuid 特征的 UUID。

  • service_uuid 服务的 UUID。

  • sz 要读取的字节数,None 表示读取所有可用字节。

UIFLOW2:

client_read.png

M5BLE.Device.client.write(data, char_uuid, service_uuid=None)

将数据写入 BLE 设备。

  • data 要写入的数据。

  • char_uuid 特征的 UUID。

  • service_uuid 服务的 UUID。

UIFLOW2:

client_write.png

M5BLE.Device.client.close()

关闭 BLE 连接。

UIFLOW2:

client_close.png

M5BLE.Device.client.get_services()

获取 BLE 服务列表。

UIFLOW2:

client_get_services.png

M5BLE.Device.client.get_characteristics(service_uuid)

获取 BLE 服务的特征列表。

  • service_uuid 服务的 UUID。

UIFLOW2:

client_get_characteristics.png

M5BLE.Device.client.set_mtu(mtu)

设置 BLE 连接的最大传输单元(MTU),单位为字节。

  • mtu 传输单元大小,单位为字节,范围为 23-517。请注意,并非所有设备都支持大于 23 的 MTU。

UIFLOW2:

set_mtu.png

服务器方法

M5BLE.Device.server.clear_services()

清除已添加的服务。

UIFLOW2:

server_clear_services.png

M5BLE.Device.server.add_service(uuid, characteristics)

添加服务。

  • uuid 服务的 UUID

  • characteristics 服务中包含的特征列表,使用 create_characteristic 创建。

UIFLOW2:

server_add_service.png

M5BLE.Device.server.create_characteristic(uuid, read, write, notify)

创建一个特征值。

  • uuid 特征的 UUID。

  • read 特征值是否可读。

  • 是否可以写入该特征值。

  • notify 特征值是否支持通知。

UIFLOW2:

server_create_characteristic.png

M5BLE.Device.server.start(interval_us)

启动 BLE 服务。

  • interval_us 广播间隔时间,单位为微秒,默认值为 500000 us。

UIFLOW2:

server_start.png

M5BLE.Device.server.on_receive(callback)

设置用于在 BLE 上接收数据的回调函数。

  • callback 数据接收回调函数,参数为 callback(M5BLE.Server, connected_client_handle)。

UIFLOW2:

server_on_receive.png

M5BLE.Device.server.on_connected(callback)

设置 BLE 设备连接成功的回调函数。

  • callback 设备连接时的回调函数,参数为 callback(M5BLE.Server, connected_client_handle)。

UIFLOW2:

server_on_connected.png

M5BLE.Device.server.on_disconnected(callback)

设置 BLE 设备断开连接的回调函数。

  • callback 设备断开连接时的回调函数,参数为 callback(M5BLE.Server, connected_client_handle)。

UIFLOW2:

server_on_disconnected.png

M5BLE.Device.server.get_client(index)

获取已连接的客户端。

  • index 客户端的索引。

UIFLOW2:

server_get_client.png

M5BLE.Device.server.get_clients()

检索已连接客户端列表。

UIFLOW2:

server_get_clients.png

Server - connected_client_handle 方法

connected_client_handle 通过回调函数的参数传入,或使用 get_client 获取。

connected_client_handle.any(uuid)

检查是否有可供读取的数据;如果有,则返回缓冲区的字节大小。

  • uuid 特征的 UUID。

UIFLOW2:

handle_any.png

connected_client_handle.read(uuid, sz=None)

从 BLE 设备读取数据。

  • uuid 特征的 UUID。

  • sz 要读取的字节数,None 表示读取所有可用字节。

UIFLOW2:

handle_read.png

handle_read_all.png

connected_client_handle.write(data, uuid)

将数据写入 BLE 设备。

  • data 要写入的数据。

  • uuid 特征的 UUID。

UIFLOW2:

handle_write.png

connected_client_handle.close()

断开连接链路。

UIFLOW2:

handle_close.png