简体   繁体   English

Raspberry PI 4 上的 Blurz Rust 库与特定设备的蓝牙连接

[英]Blurz Rust Library on Raspberry PI 4 Bluetooth connection with specific Device

I'm making a project on raspberry pi 4 model b based on Rust, using the Blurz Rust Library .我正在使用Blurz Rust Library在基于 Rust 的raspberry pi 4 模型 b 上制作一个项目。

So this is my code:所以这是我的代码:

let sessionBLE = BluetoothSession::create_session(Option::None).unwrap();
let adapter: BluetoothAdapter = BluetoothAdapter::init(&sessionBLE).unwrap();

let discoverySessionBLE : BluetoothDiscoverySession = BluetoothDiscoverySession::create_session(&sessionBLE, adapter.get_id()).unwrap();


println!("IS DISCOVERABLE {}", adapter.is_discoverable().unwrap());
println!("IS POWERED {}", adapter.is_powered().unwrap());
println!("IS DISCOVERING {}", adapter.is_discovering().unwrap());

adapter.set_discoverable(true);

println!("start discovery");
discoverySessionBLE.start_discovery().unwrap();


thread::sleep(Duration::from_secs(2));
println!("<------------------------------------------>");
println!("IS DISCOVERABLE {}", adapter.is_discoverable().unwrap());
println!("IS POWERED {}", adapter.is_powered().unwrap());
println!("IS DISCOVERING {}", adapter.is_discovering().unwrap());
println!("device list number (count) {}", adapter.get_device_list().unwrap().len());
println!("device list {:?}", adapter.get_device_list().unwrap());


let devices = adapter.get_device_list().unwrap();

println!("Test array.: {}", adapter.get_device_list().unwrap()[1]);
let uuid = adapter.get_uuids().unwrap()[1].to_string();

println!("Connecting..");
let deviceBle : BluetoothDevice = adapter.get_first_device().unwrap();

deviceBle.connect(10000);
thread::sleep(Duration::from_secs(2));
println!("device BluetoothDevice {}", deviceBle.get_address().unwrap());
println!("device RSSI {}", deviceBle.get_rssi().unwrap());
println!("device CONNECETED {}", deviceBle.is_connected().unwrap());
println!("device GATT {:?}", deviceBle.get_gatt_services().unwrap());

Everything works but there's a problem, so I don't understand how to connect to specific device.一切正常,但出现问题,所以我不明白如何连接到特定设备。 This is the list found after scan:这是扫描后找到的列表:

["/org/bluez/hci0/dev_D4_CA_6E_F0_9E_21", "/org/bluez/hci0/dev_00_A0_50_FC_85_B4", "/org/bluez/hci0/dev_7D_A6_E2_28_E2_21", "/org/bluez/hci0/dev_7D_26_EF_98_8A_F4", "/org/bluez/hci0/dev_42_14_C3_01_25_25", "/org/bluez/hci0/dev_75_0C_80_CD_04_1E", "/org/bluez/hci0/dev_52_71_26_A1_52_AA", "/org/bluez/hci0/dev_40_C3_D7_65_CC_BF", "/org/bluez/hci0/dev_72_95_B8_34_82_A3", "/org/bluez/hci0/dev_6B_99_43_81_D0_31", "/org/bluez/hci0/dev_45_66_4A_20_7C_0B", "/org/bluez/hci0/dev_46_99_9C_AA_BD_36", "/org/bluez/hci0/dev_CC_52_AF_CB_49_12", "/org/bluez/hci0/dev_C4_22_C6_95_9C_1A", "/org/bluez/hci0/dev_00_A0_50_FC_18_46", "/org/bluez/hci0/dev_00_A0_50_FD_15_F4"]

I can't connect to a specific device, I need to connect with the third device: "7D_A6_E2_28_E2_21" but from the documentation I don't understand how to, I see there's a single method:我无法连接到特定设备,我需要连接第三个设备:“7D_A6_E2_28_E2_21”但从文档中我不明白如何连接,我看到有一个方法:

let deviceBle : BluetoothDevice = adapter.get_first_device().unwrap();

But it gets only the first device, what can I do to get the third device?但是它只能获得第一个设备,我该怎么做才能获得第三个设备?

使用BluetoothDevice::new非常简单:

let device_3 = BluetoothDevice::new(&sessionBLE, devices[2].clone());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 Raspberry Pi 2 交叉编译 rust-openssl - Cross compile rust-openssl for Raspberry Pi 2 通过使用内联汇编的Raspberry Pi3的Rust启动代码 - Rust startup code for Raspberry Pi3 through use of inline assembly 如何编译Rust代码以在Raspberry Pi 2上运行? - How can I compile Rust code to run on a Raspberry Pi 2? 从Windows交叉编译Raspberry Pi 3 B的裸金属Rust - Cross-compiling baremetal Rust for Raspberry Pi 3 B from Windows 无法使用裸机 Rust 使 Raspberry Pi LED 闪烁 - Unable to blink Raspberry Pi LED using bare metal Rust 无法在 Raspberry Pi 上安装 snips-nlu:找不到 Rust 编译器 - Cannot install snips-nlu on Raspberry Pi: Can not find Rust compiler 在 Raspberry Pi 中使用 gpio-cdev 和 Rust 将标志重置为正常 - Reset flags back to normal using gpio-cdev and Rust in Raspberry Pi 获取交叉、交叉编译 Rust 依赖于 libc 的板条箱,适用于 Raspberry Pi - Getting cross, cross-compiling Rust crate that depends on libc, for Raspberry Pi Rust bindgen 找不到平台特定的库? - Rust bindgen cannot find platform specific library? 将Rust应用程序从macOS交叉编译为Raspberry Pi 2时“与arm-linux-gnueabihf-gcc链接失败” - “linking with arm-linux-gnueabihf-gcc failed” when cross-compiling a Rust application from macOS to a Raspberry Pi 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM