[英]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.