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

Device Name Characteristic (2A00) Write Access Denied - Windows #849

Open
magiced opened this issue Jun 16, 2022 · 12 comments
Open

Device Name Characteristic (2A00) Write Access Denied - Windows #849

magiced opened this issue Jun 16, 2022 · 12 comments
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend

Comments

@magiced
Copy link

magiced commented Jun 16, 2022

  • bleak version: 0.14.3
  • Python version: 3.8.10
  • Operating System: Windows 10 Pro

Description

I am trying to use bleak to write to the device name 00002a00-0000-1000-8000-00805f9b34fb on a Nordic nrf52840.
I can read and write to it using LightBlue, but on windows i keep getting the following errors when using the test script below.

bleak.exc.BleakError: Could not write value b'TEST' to characteristic 0002: Access Denied

The value is reported as writeable when i read it: [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb: (read,write) | Name: , Value: No Value

I think this is a similar error to this one.

I don't think it's an issue with the peripheral's code, as the device name can be changed using the LightBlue app. Is this an issue with bleak/bleak-winrt, or am i doing something wrong?

What I Did

Script

#import bleak (https://pypi.org/project/bleak/)
"""
----------------
XPC Reader
----------------
Operation:
    1) Connects and displays services with read characteristics
    2) If Nordic DFU service is found, returns PASS

"""

import asyncio
import codecs

from bleak import exc
from bleak import BleakClient

DEVICE_NAME_UUID = "00002a00-0000-1000-8000-00805f9b34fb"
DEVICE_NAME_LABEL = "Device Name: "

async def run(address, num_attempts=10, debug=False):

    global result

    print('Connecting...')
    for i in range (num_attempts): # note, this for loop is to avoid the software failing connection issue
        print(f'attempt: {i}')
        try:
            print('Attempting connection')
            async with BleakClient(address) as client:
                print('Connnected')

                for service in client.services:
                    for char in service.characteristics:
                        if "read" in char.properties:
                            if (char.uuid == DEVICE_NAME_UUID):
                                print("Reading...\n")
                                data = await client.read_gatt_char(char.uuid)
                                print(DEVICE_NAME_LABEL,codecs.decode(data, 'UTF-8'))

                                try:
                                    value = bytes(await client.read_gatt_char(char.uuid))
                                except Exception as e:
                                    value = str(e).encode()
                                else:
                                    value = "No Value"
                                print(
                                    f"\t[Characteristic] {char.uuid}: ({','.join(char.properties)}) | Name: {char.description}, Value: {value} ")
                                for descriptor in char.descriptors:
                                    value = await client.read_gatt_descriptor(descriptor.handle)
                                    print(f"\t\t[Descriptor] {descriptor.uuid}: (Handle: {descriptor.handle}) | Value: {bytes(value)} ")

                for service in client.services:
                    for char in service.characteristics:
                        if "write" in char.properties:
                            if (char.uuid == DEVICE_NAME_UUID):
                                print('Writing...\n')
                                data = codecs.encode('TEST', 'UTF-8')
                                await client.write_gatt_char(DEVICE_NAME_UUID, data, response=False)
                                print(f'written: {data} to {DEVICE_NAME_UUID}')
            print('Written, breaking out of for loop')
            break
        except exc.BleakDBusError:
            print('\n>> Bleak ERROR: Software caused connection abort <<\n')
            continue

        if(i == (num_attempts-1)):
            print(f"No connection after {num_attempts} attempts: ABORTING program")
            quit()

    print('disconnecting')    
    await client.disconnect()
        

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    address = 'C7:4A:5E:FD:31:F2'

    asyncio.run(run(address))

Result

Connecting...
attempt: 0
Attempting connection
Connnected
Reading...

Device Name:  T
        [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb: (read,write) | Name: , Value: No Value 
Writing...

Traceback (most recent call last):
  File "c:/Users/ed.bisdee/OneDrive - Enerpac Tool Group/Documents/_code/enerpac_bluetooth_scan/lock_scripts/read_write_dev_name.py", line 84, in <module>
    asyncio.run(run(address))
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 44, in run        
    return loop.run_until_complete(main)
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "c:/Users/ed.bisdee/OneDrive - Enerpac Tool Group/Documents/_code/enerpac_bluetooth_scan/lock_scripts/read_write_dev_name.py", line 62, in run
    await client.write_gatt_char(DEVICE_NAME_UUID, data, response=False)
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\site-packages\bleak\backends\winrt\client.py", line 617, in write_gatt_char
    _ensure_success(
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\site-packages\bleak\backends\winrt\client.py", line 102, in _ensure_success
    raise BleakError(f"{fail_msg}: Access Denied")
bleak.exc.BleakError: Could not write value b'TEST' to characteristic 0002: Access Denied

Here is the output of service_explorer.py

INFO:bleak.backends.winrt.client:Services resolved for BleakClientWinRT (C7:4A:5E:FD:31:F2)
INFO:__main__:Connected: True
INFO:__main__:[Service] 00001800-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Access Profile
INFO:__main__:  [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 2):  (read,write), Value: b'T'
INFO:__main__:  [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 4):  (read), Value: b'\x00\x00'
INFO:__main__:  [Characteristic] 00002a04-0000-1000-8000-00805f9b34fb (Handle: 6):  (read), Value: b'\x18\x00$\x00\x03\x00\x90\x01'
INFO:__main__:  [Characteristic] 00002aa6-0000-1000-8000-00805f9b34fb (Handle: 8):  (read), Value: b'\x01'
INFO:__main__:[Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 10): Generic Attribute Profile
INFO:__main__:[Service] 0000180a-0000-1000-8000-00805f9b34fb (Handle: 11): Device Information
INFO:__main__:  [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb (Handle: 12):  (read), Value: b'MyCompany'
INFO:__main__:  [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb (Handle: 14):  (read), Value: b'XXX-CONFIG:1'
INFO:__main__:  [Characteristic] 00002a25-0000-1000-8000-00805f9b34fb (Handle: 16):  (read), Value: b'0h'
INFO:__main__:  [Characteristic] 00002a27-0000-1000-8000-00805f9b34fb (Handle: 18):  (read), Value: b'REV-1'
INFO:__main__:  [Characteristic] 00002a26-0000-1000-8000-00805f9b34fb (Handle: 20):  (read), Value: b'9.1.4'
INFO:__main__:[Service] 0000180f-0000-1000-8000-00805f9b34fb (Handle: 22): Battery Service
INFO:__main__:  [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb (Handle: 23):  (read,notify), Value: b'd'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 25): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:[Service] bd9d2df5-c6fe-4516-87cf-96e307626be4 (Handle: 26): Unknown
INFO:__main__:  [Characteristic] bd9d2df9-c6fe-4516-87cf-96e307626be4 (Handle: 27):  (read,notify), Value: b'\x06\x00'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 29): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 30): Characteristic Presentation Format) | Value: b'\x0e\x00\x00\x00\x00\x00\x00'
INFO:__main__:  [Characteristic] bd9d2dfb-c6fe-4516-87cf-96e307626be4 (Handle: 31):  (read,notify), Value: b'\x8b'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 33): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 34): Characteristic Presentation Format) | Value: b'\x01\x00\x00\x00\x00\x00\x00'
INFO:__main__:[Service] c730d649-5e58-5643-5444-f74d8b9ba87f (Handle: 35): Unknown
INFO:__main__:  [Characteristic] c730d64a-5e58-5643-5444-f74d8b9ba87f (Handle: 36):  (read,write), Value: b'00000000000000'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 38): Characteristic Presentation Format) | Value: b'\x19\x00\x00\x00\x00\x00\x00'
INFO:__main__:  [Characteristic] c730d64b-5e58-5643-5444-f74d8b9ba87f (Handle: 39):  (read,write), Value: b'1970-01-01T00:00:00Z'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 41): Characteristic Presentation Format) | Value: b'\x19\x00\x00\x00\x00\x00\x00'
INFO:__main__:[Service] f69f1e6b-5259-4dff-b77c-10e04eb2be96 (Handle: 42): Unknown
INFO:__main__:  [Characteristic] f69f1e70-5259-4dff-b77c-10e04eb2be96 (Handle: 43):  (read,notify), Value: b'\x00'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 45): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 46): Characteristic Presentation Format) | Value: b'\x01\x00\x00\x00\x00\x00\x00'
INFO:__main__:  [Characteristic] f69f1e6e-5259-4dff-b77c-10e04eb2be96 (Handle: 47):  (read,notify), Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 49): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 50): Characteristic Presentation Format) | Value: b'\x0e\x00\x00\x00\x00\x00\x00'
INFO:__main__:  [Characteristic] f69f1e6d-5259-4dff-b77c-10e04eb2be96 (Handle: 51):  (read,notify), Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 53): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 54): Characteristic Presentation Format) | Value: b'\x0e\x00\x00\x00\x00\x00\x00'
INFO:__main__:  [Characteristic] f69f1e6c-5259-4dff-b77c-10e04eb2be96 (Handle: 55):  (read,notify), Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 57): Client Characteristic Configuration) | Value: b'\x00\x00'
INFO:__main__:          [Descriptor] 00002904-0000-1000-8000-00805f9b34fb (Handle: 58): Characteristic Presentation Format) | Value: b'\x06\x00\x00\x00\x00\x00\x00'
INFO:__main__:[Service] 0000fe59-0000-1000-8000-00805f9b34fb (Handle: 59): Nordic Semiconductor ASA
INFO:__main__:  [Characteristic] 8ec90003-f315-4f60-9fb8-838830daea50 (Handle: 60):  (write,indicate), Value: None
INFO:__main__:          [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 62): Client Characteristic Configuration) | Value: b'\x00\x00'
@dlech dlech added the Backend: WinRT Issues or PRs relating to the WinRT backend label Jun 16, 2022
@dlech
Copy link
Collaborator

dlech commented Jun 16, 2022

LightBlue runs on mobile devices, not Windows, correct?

The "Access Denied" error also happens when trying to use HID characteristics which it is documented that it is not allowed. Maybe there is a different way that we are supposed to change the device name on Windows? The BluetoothLEDevice.Name property is also read-only. But maybe there is another more generic device name property or method somewhere that I am missing?

@magiced
Copy link
Author

magiced commented Jun 16, 2022

Yes, LightBlue is an android app.

However, i have used the NRF Connect bluetooth program on my windows computer, and it is able to change the Device Name. However it does use an external bluetooth radio, not the computer's built in bluetooth radio

@dlech
Copy link
Collaborator

dlech commented Jun 16, 2022

Yeah, that has it's own Bluetooth stack.

@magiced
Copy link
Author

magiced commented Jun 17, 2022

The device name is in the service 00001800-0000-1000-8000-00805f9b34fb which is the Generic Access Profile. Is there a way of finding out how the library and windows stack treats how this service is treated? Is it possible that it is write protected inside the stack?

I have now run the script on both Windows and LInux (using a rasbperry pi with Rasbian) and have got the results below.

EDIT: rasbian BlueZ version: bluetoothctl: 5.55

Interestingly, the windows script gets access denied trying to write to the Generic Access Profile, and the linux script cannot even see the Generic Access Profile service!

I think this may be a problem with how the backends on each platfom or bleak handle the Generic Access profile.

I have tried writing to a different characteristic in a different service (the UIN_UUID in the results below) and that can be written to on both Windows and linux.

# WINDOWS

python unlockBoard.py -a 
F2:55:AB:9c:cb:71
**************************
unlockBoard.py
**************************
Connecting to Board
attempt: 0
Attempting connection...
Connnected!

Looking for readable services...

        Service UUID: 00001800-0000-1000-8000-00805f9b34fb, description: Generic Access Profile
                Looking for UIN_UUID in 00002a00-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb: (read,write) | Name: , Value: b'SPC' 
DEVICE_NAME_UUID found...
Device Name:  SPC
                Looking for UIN_UUID in 00002a01-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'\x00\x00' 
                Looking for UIN_UUID in 00002a04-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a04-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'\x18\x00$\x00\x03\x00\x90\x01' 
                Looking for UIN_UUID in 00002aa6-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002aa6-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'\x01' 
        Service UUID: 00001801-0000-1000-8000-00805f9b34fb, description: Generic Attribute Profile
        Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, description: Device Information
                Looking for UIN_UUID in 00002a29-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'MyCompany' 
                Looking for UIN_UUID in 00002a24-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'SPC-CONFIG:1' 
                Looking for UIN_UUID in 00002a25-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a25-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'0h' 
                Looking for UIN_UUID in 00002a27-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a27-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'REV-1' 
                Looking for UIN_UUID in 00002a26-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a26-0000-1000-8000-00805f9b34fb: (read) | Name: , Value: b'9.1.4' 
        Service UUID: 0000180f-0000-1000-8000-00805f9b34fb, description: Battery Service
                Looking for UIN_UUID in 00002a19-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb: (read,notify) | Name: , Value: b'd' 
        Service UUID: bd9d2df5-c6fe-4516-87cf-96e307626be4, description: Unknown
                Looking for UIN_UUID in bd9d2df9-c6fe-4516-87cf-96e307626be4...
                [Characteristic] bd9d2df9-c6fe-4516-87cf-96e307626be4: (read,notify) | Name: , Value: b'\x06\x00' 
                Looking for UIN_UUID in bd9d2dfb-c6fe-4516-87cf-96e307626be4...
                [Characteristic] bd9d2dfb-c6fe-4516-87cf-96e307626be4: (read,notify) | Name: , Value: b'Z' 
                Looking for UIN_UUID in c730d64a-5e58-5643-5444-f74d8b9ba87f...
                [Characteristic] c730d64a-5e58-5643-5444-f74d8b9ba87f: (read,write) | Name: , Value: b'11111111000000'
UUID_UIN found...
UIN:  11111111000000
                Looking for UIN_UUID in c730d64b-5e58-5643-5444-f74d8b9ba87f...
                [Characteristic] c730d64b-5e58-5643-5444-f74d8b9ba87f: (read,write) | Name: , Value: b'1982-12-12T11:11:11Z'    
        Service UUID: f69f1e6b-5259-4dff-b77c-10e04eb2be96, description: Unknown
                Looking for UIN_UUID in f69f1e70-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e70-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: , Value: b'\x00'
                Looking for UIN_UUID in f69f1e6e-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6e-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: , Value: b'\x00\x00'
                Looking for UIN_UUID in f69f1e6d-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6d-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: , Value: b'\x00\x00'
                Looking for UIN_UUID in f69f1e6c-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6c-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: , Value: b'\x00\x00'
        Service UUID: 0000fe59-0000-1000-8000-00805f9b34fb, description: Nordic Semiconductor ASA
                Looking for UIN_UUID in 8ec90003-f315-4f60-9fb8-838830daea50...
                [Characteristic] 8ec90003-f315-4f60-9fb8-838830daea50: (write,indicate) | Name: , Value: No Value
End of read for loop...

Looking for Writeable services...

        Service UUID: 00001800-0000-1000-8000-00805f9b34fb, description: Generic Access Profile
                Looking for DEVICE_NAME_UUID in 00002a00-0000-1000-8000-00805f9b34fb...
DEVICE_NAME_UUID found!
Unlocking Flash...
Attempting to write b'11111111000000' to 00002a00-0000-1000-8000-00805f9b34fb
Device connection error: main
Traceback (most recent call last):
  File "unlockBoard.py", line 183, in <module>
    asyncio.run(run(address))
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete  
    return future.result()
  File "unlockBoard.py", line 137, in run
    await client.write_gatt_char(DEVICE_NAME_UUID, data)
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\site-packages\bleak\backends\winrt\client.py", line 617, in write_gatt_char
    _ensure_success(
  File "C:\Users\ed.bisdee\AppData\Local\Programs\Python\Python38\lib\site-packages\bleak\backends\winrt\client.py", line 102, in _ensure_success
    raise BleakError(f"{fail_msg}: Access Denied")
bleak.exc.BleakError: Could not write value b'11111111000000' to characteristic 0002: Access Denied
Script complete

#========================================================
# Linux (Raspberry Pi, Rasbian)

pi@devopspi:~/Desktop/lock_unlock_Board $ python unlockBoard.py -a F2:55:AB:9c:cb:71
**************************
unlockBoard.py
**************************
Connecting to Board
attempt: 0
Attempting connection...
Connnected!

Looking for readable services...

        Service UUID: 00001801-0000-1000-8000-00805f9b34fb, description: Generic Attribute Profile
        Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, description: Device Information
                Looking for UIN_UUID in 00002a29-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb: (read) | Name: Manufacturer Name String, Value: b'MyCompany'
                Looking for UIN_UUID in 00002a24-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb: (read) | Name: Model Number String, Value: b'SPC-CONFIG:1'
                Looking for UIN_UUID in 00002a25-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a25-0000-1000-8000-00805f9b34fb: (read) | Name: Serial Number String, Value: b'0h' 
                Looking for UIN_UUID in 00002a27-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a27-0000-1000-8000-00805f9b34fb: (read) | Name: Hardware Revision String, Value: b'REV-1' 
                Looking for UIN_UUID in 00002a26-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a26-0000-1000-8000-00805f9b34fb: (read) | Name: Firmware Revision String, Value: b'9.1.4' 
        Service UUID: 0000180f-0000-1000-8000-00805f9b34fb, description: Battery Service
                Looking for UIN_UUID in 00002a19-0000-1000-8000-00805f9b34fb...
                [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb: (read,notify) | Name: Battery Level, Value: b'd' 
        Service UUID: bd9d2df5-c6fe-4516-87cf-96e307626be4, description: Unknown
                Looking for UIN_UUID in bd9d2df9-c6fe-4516-87cf-96e307626be4...
                [Characteristic] bd9d2df9-c6fe-4516-87cf-96e307626be4: (read,notify) | Name: Unknown, Value: b'\x06\x00' 
                Looking for UIN_UUID in bd9d2dfb-c6fe-4516-87cf-96e307626be4...
                [Characteristic] bd9d2dfb-c6fe-4516-87cf-96e307626be4: (read,notify) | Name: Unknown, Value: b'Z' 
        Service UUID: c730d649-5e58-5643-5444-f74d8b9ba87f, description: Unknown
                Looking for UIN_UUID in c730d64a-5e58-5643-5444-f74d8b9ba87f...
                [Characteristic] c730d64a-5e58-5643-5444-f74d8b9ba87f: (read,write) | Name: Unknown, Value: b'11111111000000' 
UUID_UIN found...
UIN:  11111111000000
                Looking for UIN_UUID in c730d64b-5e58-5643-5444-f74d8b9ba87f...
                [Characteristic] c730d64b-5e58-5643-5444-f74d8b9ba87f: (read,write) | Name: Unknown, Value: b'1982-12-12T11:11:11Z'
        Service UUID: f69f1e6b-5259-4dff-b77c-10e04eb2be96, description: Unknown
                Looking for UIN_UUID in f69f1e70-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e70-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: Unknown, Value: b'\x00'
                Looking for UIN_UUID in f69f1e6e-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6e-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: Unknown, Value: b'\x00\x00'        
                Looking for UIN_UUID in f69f1e6d-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6d-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: Unknown, Value: b'\x00\x00'        
                Looking for UIN_UUID in f69f1e6c-5259-4dff-b77c-10e04eb2be96...
                [Characteristic] f69f1e6c-5259-4dff-b77c-10e04eb2be96: (read,notify) | Name: Unknown, Value: b'\x00\x00'        
        Service UUID: 0000fe59-0000-1000-8000-00805f9b34fb, description: Nordic Semiconductor ASA
                Looking for UIN_UUID in 8ec90003-f315-4f60-9fb8-838830daea50...
                [Characteristic] 8ec90003-f315-4f60-9fb8-838830daea50: (write,indicate) | Name: Buttonless DFU, Value: No Value 
End of read for loop...

Looking for Writeable services...

        Service UUID: 00001801-0000-1000-8000-00805f9b34fb, description: Generic Attribute Profile
        Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, description: Device Information
                Looking for DEVICE_NAME_UUID in 00002a29-0000-1000-8000-00805f9b34fb...
                Looking for DEVICE_NAME_UUID in 00002a24-0000-1000-8000-00805f9b34fb...
                Looking for DEVICE_NAME_UUID in 00002a25-0000-1000-8000-00805f9b34fb...
                Looking for DEVICE_NAME_UUID in 00002a27-0000-1000-8000-00805f9b34fb...
                Looking for DEVICE_NAME_UUID in 00002a26-0000-1000-8000-00805f9b34fb...
        Service UUID: 0000180f-0000-1000-8000-00805f9b34fb, description: Battery Service
                Looking for DEVICE_NAME_UUID in 00002a19-0000-1000-8000-00805f9b34fb...
        Service UUID: bd9d2df5-c6fe-4516-87cf-96e307626be4, description: Unknown
                Looking for DEVICE_NAME_UUID in bd9d2df9-c6fe-4516-87cf-96e307626be4...
                Looking for DEVICE_NAME_UUID in bd9d2dfb-c6fe-4516-87cf-96e307626be4...
        Service UUID: c730d649-5e58-5643-5444-f74d8b9ba87f, description: Unknown
                Looking for DEVICE_NAME_UUID in c730d64a-5e58-5643-5444-f74d8b9ba87f...
                Looking for DEVICE_NAME_UUID in c730d64b-5e58-5643-5444-f74d8b9ba87f...
        Service UUID: f69f1e6b-5259-4dff-b77c-10e04eb2be96, description: Unknown
                Looking for DEVICE_NAME_UUID in f69f1e70-5259-4dff-b77c-10e04eb2be96...
                Looking for DEVICE_NAME_UUID in f69f1e6e-5259-4dff-b77c-10e04eb2be96...
                Looking for DEVICE_NAME_UUID in f69f1e6d-5259-4dff-b77c-10e04eb2be96...
                Looking for DEVICE_NAME_UUID in f69f1e6c-5259-4dff-b77c-10e04eb2be96...
        Service UUID: 0000fe59-0000-1000-8000-00805f9b34fb, description: Nordic Semiconductor ASA
                Looking for DEVICE_NAME_UUID in 8ec90003-f315-4f60-9fb8-838830daea50...
End of Write for loop...


Connection success, breaking out of Attempt for loop

disconnecting
Script complete

@dlech
Copy link
Collaborator

dlech commented Jun 17, 2022

and the linux script cannot even see the Generic Access Profile service!

BlueZ like to hide services and characteristics that it uses internally completely. Like Windows, there is a Name property in BlueZ that can be used instead, but unlike Windows, I think it is actually writable.

@magiced
Copy link
Author

magiced commented Jun 22, 2022

I've been doing various fault finding, and it occured to me that the problem may be that i'm trying to access a 16-bit UUID with a 128bit UUID, e.g. "00002a00-0000-1000-8000-00805f9b34fb" rather than "2A00"

Could this be causing the issue?

@dlech
Copy link
Collaborator

dlech commented Jun 22, 2022

No, on Windows, all UUIDs must be converted to a 128-bit GUID before being passed to the Windows APIs.

@jochenjagers
Copy link
Contributor

I had a very similar problem while writing data to a Nordic UART service. I had many access denies errors when I started with bleak version 0.13 and after updating to 0.15 it change more to "The object is already closed" or "A method was called at an unexpected time" (don't know the exact error messages as my system reported them in German)
The problem for me was that the device kept updating the service list during service discovery. I added a gatt_services_changed listener to the WinRT client and started over with service discovery if it is was currently running or started a new discovery before accessing the services list the next time.
The documentation of the GattServicesChanged event (https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.gattserviceschanged) says:

This event is raised when the remote device changes its services, or an unpaired device is disconnecting. All services are cleared because unpaired device services can't be cached between connections.
The object parameter in this event is null for every event that is raised.
In your handler for this event, do the following in order to get the services available. Call BluetoothLEDevice.GetGattServicesAsync on the BluetoothLEDevice that's passed to your handler. When calling GetGattServicesAsync, use the Cached option. This can be used to refresh the list of services and re-initialize the services on the device if they are gone.

For my device I get the event 11 times during the first service discovery and if I reload them after I got the event everything works as expected.
I can open a PR in the next days with a suggestion how to deal with this issue.

@dlech
Copy link
Collaborator

dlech commented Sep 13, 2022

It would also be helpful to see a wireshark log of what the device is actually doing.

@jochenjagers
Copy link
Contributor

Here is a extract of the log at the moment the failure happend.
While pairing the device sends a "Service Changed" message (at point 1) and Windows responds with the confirmation (at point 2) and starts to read the services again. The device responds to this (at point 3) round about 1.7 seconds later.
In between this time the program tries to write to the device and gets an OSError:

2022-09-14 10:52:01.588753
Query notify characteristic: 6e400003-b5a3-f393-e0a9-e50e24dcca9e (Handle: 19): Nordic UART TX
Query write characteristic: 6e400002-b5a3-f393-e0a9-e50e24dcca9e (Handle: 17): Nordic UART RX
[error]: OSError: [WinError -2147483629] Das Objekt wurde geschlossen

I expect Windows to clear all services as described in the documentation as soon as the "Service Changed" event is received and they are not available until the new service service discovery is completed.

I can send you the full wireshark log via e-mail if you are interested in.

image

@dlech dlech changed the title Characteristic Write Access Denied - Windows Device Name Characteristic (2A00) Write Access Denied - Windows Sep 22, 2022
@dlech
Copy link
Collaborator

dlech commented Sep 22, 2022

Windows is protective of some well-known services/characteristics and won't allow accessing them. Similarly, BlueZ hides the GAP Service completely, making the device name inaccessible. Furthermore, the device name displayed by the operating system may not be the same one as read by this characteristic - many OSes allow "changing the name" but it really means providing a local alias for the device and not actually writing that name to the device.

I think the ServicesChanged issue is a separate issue and we should start a new issue for that.

For the device name issue, I think we should have a property to get the device name instead of reading a characteristic. I don't think writing the device name will be possible cross-platform. And as mentioned already, this name may be a local OS alias rather than the Device Name characteristic.

If you control the device firmware, you could add a custom attribute to update the device name if you really need to update the name on the device.

@Takahisa-Ishikawa
Copy link

I had a similar issue.
I could not write value on a custom characteristic added to "Device Information" service from Windows PC while I could do it from Android phone.

After seeing the discussion above, I added the custom characteristic to the custom service insted of the exisiting service, and I no longer get Accedd Denied.

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

No branches or pull requests

4 participants