class BLEUARTClient

BLEUARTClient 类是一个 BLE UART 客户端,可以连接到 BLE UART 服务器并与其通信。

MicroPython 应用示例:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from bleuart import *
 9import time
10
11
12ble_central = None
13
14
15nums = None
16i = None
17
18
19def setup():
20    global ble_central, nums, i
21
22    M5.begin()
23    ble_central = BLEUARTClient()
24    ble_central.connect("ble-uart", timeout=2000)
25    while not (ble_central.is_connected()):
26        time.sleep_ms(100)
27    print("Connected")
28    nums = [4, 8, 15, 16, 23, 46]
29    i = 1
30    while True:
31        ble_central.write((str((nums[int(i - 1)]))))
32        i = (i + 1) % len(nums)
33        time.sleep(1)
34        print((str("rx:") + str(((ble_central.read()).decode()))))
35    ble_central.close()
36    ble_central.deinit()
37
38
39def loop():
40    global ble_central, nums, i
41    M5.update()
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

UiFlow2 应用示例:

example.png

atoms3_bleuart_client_example.m5f2

Constructors

class bleuart.BLEUARTClient(name='', rxbuf=100, verbose=False)

创建 BLE UART 客户端。

参数:
  • name (str) – ble 设备的名称。

  • rxbuf (int) – 接收缓冲区的大小。

  • verbose (bool) – 启用详细输出。

UIFLOW2:

init.png

Methods

BLEUARTClient.irq()

ble uart client 的 irq。

BLEUARTClient.is_connected()

检查 ble uart server 是否已连接。

UIFLOW2:

is_connected.png

BLEUARTClient.connect(name, timeout=2000)

连接到 ble uart 服务器。

参数:
  • name (str) – ble 设备的名称。

  • timeout (int) – 连接的超时时间。

UIFLOW2:

connect.png

BLEUARTClient.any() int

检查接收缓冲区中是否有任何数据。

返回:

接收缓冲区中的字节数。

UIFLOW2:

any.png

BLEUARTClient.read(sz=None) bytes

从接收缓冲区读取数据。

参数:

sz (int) – 要读取的字节数。如果未指定,则读取所有数据。

返回:

从接收缓冲区读取的数据。

UIFLOW2:

read_all.png

read_bytes.png

read_raw_data.png

readline.png

BLEUARTClient.write(data: bytes)

将数据写入 BLE UART 服务器。

参数:

data (bytes) – 要写入的数据。

UIFLOW2:

write.png

write1.png

write_line.png

write_list.png

write_raw_data.png

write_raw_data1.png

BLEUARTClient.close()

关闭 ble uart 服务器。

UIFLOW2:

close.png

BLEUARTClient.deinit()

反初始化 ble uart server。

UIFLOW2:

deinit.png