gpt4 book ai didi

rust - 通过 Rumble 从 Bluetooth 5 LE DevBoard 读取串行数据流

转载 作者:行者123 更新时间:2023-11-29 08:31:32 25 4
gpt4 key购买 nike

我正在尝试读取来自低功耗蓝牙开发板的串行数据流。固件注册为 UART 仿真服务(自定义 UUID),并通过 Receive_Characteristic(自定义 UUID)发送数据。发送的串行数据只是递增的数字。

使用 rumble ,我能够与设备建立连接,并读取一些东西,但不是流。以下是一个最小的工作代码示例:

    let manager = Manager::new().unwrap();

let mut adapter = manager
.adapters()
.expect("could not list adapters")
.into_iter()
.find(|a| a.name == self.adapter_name)
.expect("could not find adapter by name");

println!("power cycle adapter");
adapter = manager.down(&adapter).unwrap();
adapter = manager.up(&adapter).unwrap();
println!("connect adapter");

let central = adapter.connect().unwrap();
central.start_scan().unwrap();
println!(
"find desired {:?} peripheral...",
&self.device_name
);

// keep scanning for 10 s
std::thread::sleep(std::time::Duration::from_secs(1));
central.stop_scan().unwrap();

let peripherals = central.peripherals();



let mdevice = central
.peripherals()
.into_iter()
.find(|perf| {
perf.properties()
.local_name
.iter()
.any(|name| name.contains(&self.device_name))
})
.expect("could not find peripheral by name");

std::thread::sleep(std::time::Duration::from_secs(1));

match mdevice.connect() {
Ok(d) => {
println!("mdevice connected");
d
}
Err(err) => {
eprintln!("error connecting to mdevice: {:?}", err);
panic!()
}
};
std::thread::sleep(std::time::Duration::from_secs(1));
println!("discovering characteristics");

for ch in mdevice.discover_characteristics().unwrap().into_iter() {
println!("found characteristic: {:?}", ch);
}
std::thread::sleep(std::time::Duration::from_secs(1));
println!("get desired characteristic");
let receive_characteristic = mdevice
.discover_characteristics()
.unwrap()
.into_iter()
.find(|c| {
RECEIVE_CHARACTERISTIC == c.uuid
})
.expect("could not find given characteristic");


// this is some testing code to print out received data
let (tx, rx) = std::sync::mpsc::channel();

std::thread::spawn(move || loop {
let data = match mdevice.read(&receive_characteristic) {
Ok(d) => d,
Err(err) => { println!("received an error {:?}", err);
Vec::new()}
};
println!("send : {:02?}", data);
match tx.send(data) {
Ok(d) => d,
Err(e) => println!("error {:?}", e)
};
});

loop {
let dd = rx.recv();
println!("received : {:02?}", dd.unwrap());
}

Ok(())

使用 rumble,我能够连接到设备,但是获取流很奇怪。我一直在 vec 中得到相同的数字,但有时得到的数字在增量范围内。读取串行流是否正确?

编辑:我目前正在使用 nRF52840-DK 开发板。固件发出从 0 到 255 的递增数字,然后重复该序列。

最佳答案

解决了。

主要问题是,我没有完全理解 GATT 配置文件以及蓝牙 LE 协议(protocol)。这resource很好地介绍了这个主题。

解决方案是在设备连接后订阅数据(事件)更新并注册一个事件处理程序,该事件处理程序对传入的数据使用react。就这么简单。

// ... same code as before, but only the relevant pieces are shown.
mdevice.connect().expect("Could not connect to device");
std::thread::sleep(std::time::Duration::from_secs(1));

let chars = mdevice.discover_characteristics()
.expect("Discovering characteristics failed");

std::thread::sleep(std::time::Duration::from_secs(1));

let receive_characteristic = chars.clone().into_iter()
.find(|c|
{
// The constant is just a fixed array
RECEIVE_CHARACTERISTIC == c.uuid
}).expect("Could not find given characteristic");

// subscribe to the event
mdevice.subscribe(&receive_characteristic)

mdevice.on_notification(Box::from(move |v : rumble::api::ValueNotification|
{
// do something with the received data
}));

关于rust - 通过 Rumble 从 Bluetooth 5 LE DevBoard 读取串行数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com