Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bleak pairing not working gives Failed to pair: org. bluez.Error.AuthenticationRejected error on raspberry pi #1634

Open
wahadatjan opened this issue Aug 27, 2024 · 3 comments
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend

Comments

@wahadatjan
Copy link

wahadatjan commented Aug 27, 2024

bleak version: 0.22.2
Python version:3.11.6
BlueZ version:5.68

Description

I used bleak library to pair the ble devices but get error like

  | Attempting to pair with EA:A0:01:2B:32:A9
-- | --
  | Failed to pair: org. bluez.Error.AuthenticationRejected

What I Did

import asyncio
               
from bleak import BleakScanner, BleakClient

Wahdat_name = "SM-DOM2"
characteristic_uuid = "00002A37-0000-1000-8000-00805f9b34fb"
async def scan_and_pair():
    # Scan for devices
    print("Scanning for BLE devices...")
    devices = await BleakScanner.discover()
    
    if not devices:
        print("No BLE devices found.")
        return
    
    # List found devices
    print("Found devices:")
    for i, device in enumerate(devices):
        print(f"{i}: {device.name} - {device.address}")

    # Check if Wahdat's Android Watch is found by name
    device_found = False
    for device in devices:
        if device.name == Wahdat_name:
            device_found = True
            selected_device = device.address
            break
    
    print("SELECTED",selected_device)
    if not device_found:
        print(f"{Wahdat_name} not found.")
        return
    
    # Pair with Wahdat's Android Watch
    async with BleakClient(selected_device) as client:
        try:
            print(f"Attempting to pair with {selected_device} ({Wahdat_name})...")
            paired = await client.pair()
            if paired:
                value = await client.read_gatt_char(characteristic_uuid)
                print("Read value:", value)
                print(f"Successfully paired with {selected_device} ({Wahdat_name})")
            else:
                print(f"Failed to pair with {selected_device} ({Wahdat_name})")
        except Exception as e:
            print(f"Error during pairing: {e}")

asyncio.run(scan_and_pair())
Screenshot 2024-08-26 at 4 41 24 PM
@dlech
Copy link
Collaborator

dlech commented Aug 27, 2024

If you run agent on in bluetootctl before pair ... does it prompt you for a passkey?

@dlech dlech added the Backend: BlueZ Issues and PRs relating to the BlueZ backend label Aug 27, 2024
@wahadatjan
Copy link
Author

@dlech agent on and while pairing still getting same error
Screenshot 2024-08-28 at 2 11 41 PM

@dlech
Copy link
Collaborator

dlech commented Aug 28, 2024

If BlueZ can't pair, then Bleak isn't going to be able to either. You could try logging Bluetooth packets to see what is going on behind the scenes to try to figure out what the actual problem is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend
Projects
None yet
Development

No branches or pull requests

2 participants