deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't connect peripheral on Windows

dongjian opened this issue · comments

Describe the bug
Can't connect peripheral on windows .

log : Error connecting to peripheral, skipping: Error { code: 0x8000FFFF, message: 灾难性故障
 }

After manually connect bluetooth in the system control panel
The log turn to :

log : Error connecting to peripheral, skipping: Error Not Connect

then I change (Uncached to Cached)
let service_result = self.get_gatt_services(BluetoothCacheMode::Cached).await?;
everything works fine
log:

Connecting to peripheral "RY KB OTA"...
Discover peripheral "RY KB OTA" services...
Service UUID 00001800-0000-1000-8000-00805f9b34fb, primary: true
  Characteristic { uuid: 00002a00-0000-1000-8000-00805f9b34fb, service_uuid: 00001800-0000-1000-8000-00805f9b34fb, properties: READ }
Service UUID 49535343-fe7d-4ae5-8fa9-9fafd205e455, primary: true
  Characteristic { uuid: 49535343-1e4d-4bd9-ba61-23c647249616, service_uuid: 49535343-fe7d-4ae5-8fa9-9fafd205e455, properties: NOTIFY }
  Characteristic { uuid: 49535343-8841-43f4-a8d4-ecbe34729bb3, service_uuid: 49535343-fe7d-4ae5-8fa9-9fafd205e455, properties: WRITE_WITHOUT_RESPONSE | WRITE }

And I test on Mac . No need to manually connect device to system. and everything works perfect

Expected behavior
No need to manually connect device to system . Can connect peripheral normally

Actual behavior
image

Have to manually connect deivce to system or it will be err code: 0x8000FFFF, message: 灾难性故障

Additional context
I have to change the code in src/winrtble/ble/device.rs line 83: BluetoothCacheMode::Uncached to Cached . to make it work, and i dont know why.
I tested in another app "Bluetooth LE Explorer". it don't need to manually connect device . When I click "pair" its auto connected to the system .
I tested same code on Mac . and everything works perfect

test code :

    let manager = Manager::new().await.unwrap();
    let adapter_list = manager.adapters().await.unwrap();
    if adapter_list.is_empty() {
        eprintln!("No Bluetooth adapters found");
    }
    for adapter in adapter_list.iter() {
        println!(
            "Starting scan on {}...",
            adapter.adapter_info().await.unwrap()
        );
        adapter
            .start_scan(ScanFilter::default())
            .await
            .expect("Can't scan BLE adapter for connected devices...");
        tokio::time::sleep(Duration::from_secs(5)).await;
        let peripherals = adapter.peripherals().await.unwrap();
        if peripherals.is_empty() {
            eprintln!("->>> BLE peripheral devices were not found, sorry. Exiting...");
        } else {
            for peripheral in peripherals.iter() {
                let properties = peripheral.properties().await.unwrap();
                let is_connected = peripheral.is_connected().await.unwrap();
                let local_name = properties
                    .unwrap()
                    .local_name
                    .unwrap_or(String::from("(peripheral name unknown)"));
                println!(
                    "Peripheral {:?} is connected: {:?}",
                    local_name, is_connected
                );
                if local_name != "RY KB OTA" {
                    continue;
                }
                println!("Connecting to peripheral {:?}...", &local_name);
                if let Err(err) = peripheral.connect().await {
                    eprintln!("Error connecting to peripheral, skipping: {}", err);

                }
                peripheral.discover_services().await.unwrap();
                println!("Discover peripheral {:?} services...", &local_name);
                for service in peripheral.services() {
                    println!(
                        "Service UUID {}, primary: {}",
                        service.uuid, service.primary
                    );
                    for characteristic in service.characteristics {
                        println!("  {:?}", characteristic);
                    }
                }
                if is_connected {
                    println!("Disconnecting from peripheral {:?}...", &local_name);
                    peripheral
                        .disconnect()
                        .await
                        .expect("Error disconnecting from BLE peripheral");
                }
                break;
            }
        }
    }
commented

This is gonna put us in direct conflict with the bug where having it set to cached caused update issues on some devices, so I'm not sure there's an easy fix here. #155

Thanks for you reply. and another question need help as mentioned above. I found on windows device need to connect to the system first . then you can connect it correctly. or it will be err { code: 0x8000FFFF, message: catastrophic failure
} . Is there any way can make the device auto connect to the system when call peripheral.connect().awit on windows ?