1

我正在尝试通过蓝牙低功耗 (BLE) 连接将运动数据(加速度计、陀螺仪……)从 IMU 传递到我的计算机。我购买了 FXOS8700 + FXAS21002 来生成运动数据,并使用有线 I2C 连接将该数据传递到我的 BLE 板 nRF52840。I2C 连接有效,我可以通过有线连接从 nRF52840 获取数据。我还设法在我的 BLE 板和我的计算机之间建立了蓝牙连接,并且可以查看 BLE 特性,但我不知道如何对 nRF52840 进行编程以将我的运动数据写入这些特性以通过蓝牙传递它们。

下面是我用来建立 I2C 连接和创建 BLE 连接的代码。缺少的是创建 BLE 特性的代码。

# Import ble libraries
import _bleio
import adafruit_ble
from adafruit_ble.advertising.standard import Advertisement
from adafruit_ble.services.standard.device_info import DeviceInfoService

# Import libraries for the FXOS8700 accelerometer and magnetometer
import time
import board
import busio
import adafruit_fxos8700


# CircuitPython <6 uses its own ConnectionError type. So, is it if available. Otherwise,
# the built in ConnectionError is used.
connection_error = ConnectionError
if hasattr(_bleio, "ConnectionError"):
    connection_error = _bleio.ConnectionError

# PyLint can't find BLERadio for some reason so special case it here
ble = adafruit_ble.BLERadio()  # pylint: disable=no-member


# Initialize I2C bus and device
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_fxos8700.FXOS8700(i2c)

isConnected = 0
while not isConnected:
    print("Scanning...")
    for adv in ble.start_scan(Advertisement, timeout=5):
        name = adv.complete_name
        if not name:
            continue
        isConnected = 1
        print("Connected")
        break

    # Stop scanning whether or not we are connected
    ble.stop_scan()
    print("Stopped scan")
    
while True:
    # Read acceleration and magnetometer
    accel_x, accel_y, accel_z = sensor.accelerometer
    mag_x, mag_y, mag_z = sensor.magnetometer
    
    # Write acceleration and magnetometer data to BLE characteristics...

如何将数据写入 BLE 特征并通过蓝牙发送该数据?我是否需要创建新的 BLE 特性或服务?我觉得我在这里遗漏了一些基本的东西......

附加信息:我正在使用 MATLAB 中的 ble() 函数连接到 nRF52840 并读取 BLE 特征数据。我最初试图通过蓝牙传递每一个测量值(可能需要一个更复杂的带有数据缓冲区的程序),但此时我很高兴能够读取最新的测量值。

由于我使用的 BLE 板非常受欢迎,我希望能找到许多通过蓝牙传输数据的示例,但我没有找到任何使用 BLE 发送数据的示例。有许多使用 BLE-UART 连接(MATLAB 不可见)或向 BLE 板发送数据示例,但没有BLE 板发送数据,除非通过这种 UART 方法(但我找不到访问该方法的方法数据)。

4

0 回答 0