dnandha / miauth

Authenticate and interact with Xiaomi devices over BLE
GNU Affero General Public License v3.0
28 stars 10 forks source link

Cannot connect to Xiaomi Yeelight MJYD02YL-A #2

Open paulius2k opened 2 years ago

paulius2k commented 2 years ago

Hello, I found your repo here.

I am trying to connect to Xiaomi Yeelight MJYD02YL-A. I can connect using https://atc1441.github.io/Temp_universal_mi_activate.html tool. However, it fails to "do activation":

Log:
20:49:24: Searching for devices
20:49:29: Connecting to: MJYD2S
20:49:31: Detected Mi Thermometer
20:49:32: Connected
20:49:36: Activating now, please wait...

I thought your tool could help. However I am getting an error (MAC removed):

paulius:~$ miauth xx:xx --register
Using Mi
Connecting
Traceback (most recent call last):
  File "/home/paulius/.local/bin/miauth", line 8, in <module>
    sys.exit(main())
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 122, in main
    mi_main(ble)
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 73, in mi_main
    ble.connect()
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/ble/blue.py", line 79, in connect
    self.p.connect(self.mac, btle.ADDR_TYPE_RANDOM)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 445, in connect
    self._connect(addr, addrType, iface)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral xx:xx, addr type: random

What could be done here? Thanks!

dnandha commented 2 years ago

Hey @paulius2k, please make sure that the Bluetooth adapter on your PC is enabled / turned on. You can check this with bluetoothctl but I recommend using the blueman frontend.

paulius2k commented 2 years ago

@dnandha - yes, it is enabled. I see my device in Blueman and I can connect to it.

dnandha commented 2 years ago

@paulius2k Ok, then please test if you can manually discover LE gatt services:

  1. bluetoothctl
  2. connect <mac>
  3. list-attributes

    Further information

paulius2k commented 2 years ago

Connected.

[MJYD2S]# list-attributes
Invalid command in menu main: list-attributes

Use "help" for a list of available commands in a menu.
Use "menu <submenu>" if you want to enter any submenu.
Use "back" if you want to return to menu main.
paulius2k commented 2 years ago
[MJYD2S]# help
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
monitor                                           Advertisement Monitor Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select <ctrl>                                     Select default controller
devices                                           List available devices
paired-devices                                    List paired devices
system-alias <name>                               Set controller alias
reset-alias                                       Reset controller alias
power <on/off>                                    Set controller power
pairable <on/off>                                 Set controller pairable mode
discoverable <on/off>                             Set controller discoverable mode
discoverable-timeout [value]                      Set discoverable timeout
agent <on/off/capability>                         Enable/disable agent with given capability
default-agent                                     Set agent as the default one
advertise <on/off/type>                           Enable/disable advertising with given type
set-alias <alias>                                 Set device alias
scan <on/off>                                     Scan for devices
info [dev]                                        Device information
pair [dev]                                        Pair with device
cancel-pairing [dev]                              Cancel pairing with device
trust [dev]                                       Trust device
untrust [dev]                                     Untrust device
block [dev]                                       Block device
unblock [dev]                                     Unblock device
remove <dev>                                      Remove device
connect <dev>                                     Connect device
disconnect [dev]                                  Disconnect device
menu <name>                                       Select submenu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program
export                                            Print environment variables
dnandha commented 2 years ago

Ah sorry, after connect <> type: menu gatt, after this you can do the list-attributes command.

paulius2k commented 2 years ago

OK, now I get this:

[MJYD2S]# list-attributes
Primary Service (Handle 0x000a)
    /org/bluez/hci1/dev_XX/service0001
    00001801-0000-1000-8000-00805f9b34fb
    Generic Attribute Profile
Characteristic (Handle 0x23c4)
    /org/bluez/hci1/dev_XX/service0001/char0002
    00002a05-0000-1000-8000-00805f9b34fb
    Service Changed
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service0001/char0002/desc0004
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Primary Service (Handle 0xc8e0)
    /org/bluez/hci1/dev_XX/service000e
    0000180f-0000-1000-8000-00805f9b34fb
    Battery Service
Characteristic (Handle 0x67c4)
    /org/bluez/hci1/dev_XX/service000e/char000f
    00002a19-0000-1000-8000-00805f9b34fb
    Battery Level
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service000e/char000f/desc0011
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Primary Service (Handle 0xc8e0)
    /org/bluez/hci1/dev_XX/service0012
    0000180a-0000-1000-8000-00805f9b34fb
    Device Information
Characteristic (Handle 0x9b14)
    /org/bluez/hci1/dev_XX/service0012/char0013
    00002a29-0000-1000-8000-00805f9b34fb
    Manufacturer Name String
Characteristic (Handle 0x9b14)
    /org/bluez/hci1/dev_XX/service0012/char0015
    00002a24-0000-1000-8000-00805f9b34fb
    Model Number String
Characteristic (Handle 0x9b14)
    /org/bluez/hci1/dev_XX/service0012/char0017
    00002a27-0000-1000-8000-00805f9b34fb
    Hardware Revision String
Characteristic (Handle 0x9b14)
    /org/bluez/hci1/dev_XX/service0012/char0019
    00002a26-0000-1000-8000-00805f9b34fb
    Firmware Revision String
Primary Service (Handle 0xc8e0)
    /org/bluez/hci1/dev_XX/service001b
    0000fe95-0000-1000-8000-00805f9b34fb
    Xiaomi Inc.
Characteristic (Handle 0xc3c4)
    /org/bluez/hci1/dev_XX/service001b/char001c
    00000004-0000-1000-8000-00805f9b34fb
    Unknown
Characteristic (Handle 0xc3c4)
    /org/bluez/hci1/dev_XX/service001b/char001e
    00000010-0000-1000-8000-00805f9b34fb
    UPNP
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service001b/char001e/desc0020
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Characteristic (Handle 0xf078)
    /org/bluez/hci1/dev_XX/service001b/char0021
    00000019-0000-1000-8000-00805f9b34fb
    AVDTP
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service001b/char0021/desc0023
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Characteristic (Handle 0x0388)
    /org/bluez/hci1/dev_XX/service001b/char0024
    00000017-0000-1000-8000-00805f9b34fb
    AVCTP
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service001b/char0024/desc0026
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Characteristic (Handle 0x1b58)
    /org/bluez/hci1/dev_XX/service001b/char0027
    00000018-0000-1000-8000-00805f9b34fb
    Unknown
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service001b/char0027/desc0029
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
Primary Service (Handle 0xc8e0)
    /org/bluez/hci1/dev_XX/service002a
    00000100-0065-6c62-2e74-6f696d2e696d
    Vendor specific
Characteristic (Handle 0x4c74)
    /org/bluez/hci1/dev_XX/service002a/char002b
    00000101-0065-6c62-2e74-6f696d2e696d
    Vendor specific
Characteristic (Handle 0x4c74)
    /org/bluez/hci1/dev_XX/service002a/char002d
    00000102-0065-6c62-2e74-6f696d2e696d
    Vendor specific
Descriptor (Handle 0x0015)
    /org/bluez/hci1/dev_XX/service002a/char002d/desc002f
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
[MJYD2S]# 
dnandha commented 2 years ago

@paulius2k Everything looks fine. You could try btmgmt le on and when using the miauth command I always put the MAC address in quotes and all lower-case, e.g. miauth "12:34:56:ab:cd:ef" - also try with sudo.

paulius2k commented 2 years ago

@dnandha - this is what I get:

paulius:~$ sudo btmgmt le on
Set Low Energy for hci0 failed with status 0x11 (Invalid Index)

For the miauth commands I still get errors:

paulius:~$ miauth "88:ab:bc:ff:11:22" --register
Using Mi
Connecting
Traceback (most recent call last):
  File "/home/paulius/.local/bin/miauth", line 8, in <module>
    sys.exit(main())
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 122, in main
    mi_main(ble)
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 73, in mi_main
    ble.connect()
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/ble/blue.py", line 79, in connect
    self.p.connect(self.mac, btle.ADDR_TYPE_RANDOM)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 445, in connect
    self._connect(addr, addrType, iface)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 88:ab:bc:ff:11:22, addr type: random

and

paulius:~$ sudo miauth "88:ab:bc:ff:11:22" --register
sudo: miauth: command not found
dnandha commented 2 years ago

The first one is interesting. Can you post the output of btmgmt info please?

paulius2k commented 2 years ago
paulius:~$ btmgmt info
Index list with 1 item
hci2:   Primary controller
    addr 08:D4:0C:CF:D3:10 version 8 manufacturer 2 class 0x7c010c
    supported settings: powered connectable fast-connectable discoverable bondable link-security ssp br/edr hs le advertising secure-conn debug-keys privacy configuration static-addr phy-configuration 
    current settings: powered bondable ssp br/edr le secure-conn 
    name ideapad
    short name 
hci2:   Configuration options
    supported options: public-address 
    missing options: 
paulius2k commented 2 years ago

@dnandha - any ideas how to go further? I'd appreciate your help.

dnandha commented 2 years ago

@paulius2k Yeah, so your hci index is 2 instead of 0 for some reason. In this line, could you try adding the extra argument iface=2 to the constructor and see if it connects?

paulius2k commented 2 years ago

Thank you for your support.

So like this?:

        self.p.connect(self.mac, btle.ADDR_TYPE_RANDOM, iface=3)  

I tried different values (0 to 3): on 0,1,2 it throws error right away, on 3 it tries connecting for some time and then still throws an error:

paulius:~$ miauth "88:ab:bc:ff:11:22" --register
Using Mi
Connecting
Traceback (most recent call last):
  File "/home/paulius/.local/bin/miauth", line 8, in <module>error
    sys.exit(main())
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 122, in main
    mi_main(ble)
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/cli.py", line 73, in mi_main
    ble.connect()
  File "/home/paulius/.local/lib/python3.9/site-packages/miauth/ble/blue.py", line 79, in connect
    self.p.connect(self.mac, btle.ADDR_TYPE_RANDOM, iface=3)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 445, in connect
    self._connect(addr, addrType, iface)
  File "/home/paulius/.local/lib/python3.9/site-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 88:ab:bc:ff:11:22, addr type: random

I see that blueman also uses hci3 when connecting to that device:

Adapter: /org/bluez/hci3

I also noticed that blueman manages to connect to the device first, but after some time it throws a message Disconnected and Pairing failed for 'my_mac'

dnandha commented 1 year ago

It seems that the device is refusing a BLE connection altogether. I would suggest a look into the Homebridge project and then installing one of the available Xiaomi Yeelight plugins.

bwomsm1 commented 1 year ago

Hi @dnandha, I am also facing the same exception error, I was trying to connect to the device with bluetoothctl and then connect <my mac address> but it also fail, Does it means that I cannot connect to the device which I am trying to?

mptei commented 7 months ago

The reason that it can't connect: bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 88:ab:bc:ff:11:22, addr type: random is the addr_type. If I change that to btle.ADDR_TYPE_PUBLIC in miauth/ble/blue.py it connects and tries to register.

But it fails then with other errors.

mptei commented 7 months ago

The best what I can get is:

sudo miauth "C8:5C:CC:9E:2D:50" --register -v 
Namespace(mac='C8:5C:CC:9E:2D:50', m365=False, nb=False, command=None, serial=False, fwver=False, verbose=True, register=True, register_did=None, token_file='./mi_token')
Using Mi
Connecting
enabling notifications for: None
enabling notifications for: 00000010-0000-1000-8000-00805f9b34fb
enabling notifications for: 00000019-0000-1000-8000-00805f9b34fb
Registering
Private Key (Val): 112419275276413116554050944384679419757588627360014611833565850771353991564167
Public Key (Hex): b96d17b6d014d62aba2a6637c52fd4f6a951bc89e6b32fb503eb2824cea6688c60aa77b8d3a6f17cf52d58d75dd3f9de7d1bd4582fa2ba1a1a93b7293204e289
new state: State.RECV_INFO
>> Please press power button within 5 secs after beep
enabling notifications for: None
enabling notifications for: 00000010-0000-1000-8000-00805f9b34fb
enabling notifications for: 00000019-0000-1000-8000-00805f9b34fb
Private Key (Val): 109321794756650374751708509552051624130372354540166114334807301869296931961141
Public Key (Hex): 04699b01187b41c31e5df5a93a2a3d3e299b6ab031dcf8fba6ebe28ed769cb1c71999ac7cbb198ea63ca31bf40186adbbe0ea1716d5ed4b008f33a4a631a7127
new state: State.RECV_INFO
>> Please press power button within 5 secs after beep
^CTraceback (most recent call last):
  File "/home/mike/miauth/bin/miauth", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/mike/miauth/lib/python3.11/site-packages/miauth/cli.py", line 155, in main
    mi_main(ble)
  File "/home/mike/miauth/lib/python3.11/site-packages/miauth/cli.py", line 108, in mi_main
    mc.register(did=args.register_did)
  File "/home/mike/miauth/lib/python3.11/site-packages/miauth/mi/miclient.py", line 273, in register
    return self.register(did=did)  # return because of recursion
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mike/miauth/lib/python3.11/site-packages/miauth/mi/miclient.py", line 270, in register
    time.sleep(5)
KeyboardInterrupt

this goes endless till I press Ctrl-C.