dalehumby / ESPHome-Apple-Watch-detection

ESPHome BLE Apple Watch presence detection
MIT License
332 stars 17 forks source link

Apple Watch Series 6 #5

Closed adrianmoisey closed 3 years ago

adrianmoisey commented 3 years ago

Hi Dale! Thanks for this, it's really awesome!

I've got an Apple Watch Series 6, which isn't working. Here's the debug output:

[15:19:28][D][ble_adv:058]: Possible Apple Watch? (mac 4F:82:F6:60:B9:2C) rssi -60, unrecognised action code 0x2d

Do you need more than that line?

adrianmoisey commented 3 years ago

Here's more:

[15:30:05][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -59, unrecognised action code 0x21
[15:30:05][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -62, unrecognised action code 0x21
[15:30:07][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -65, unrecognised action code 0x21
[15:30:08][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -49, unrecognised action code 0x21
[15:30:08][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -47, unrecognised action code 0x21
[15:30:09][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -34, unrecognised action code 0x21
[15:30:09][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -34, unrecognised action code 0x21
[15:30:10][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -35, unrecognised action code 0x21
[15:30:11][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -33, unrecognised action code 0x21
[15:30:11][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -35, unrecognised action code 0x21
[15:30:12][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -34, unrecognised action code 0x21
[15:30:12][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -45, unrecognised action code 0x21
[15:30:14][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -66, unrecognised action code 0x21
[15:30:14][D][ble_adv:058]: Possible Apple Watch? (mac 42:77:C1:CC:E1:8A) rssi -64, unrecognised action code 0x21

You can see the signal change as I move my watch closer to the sensor

adrianmoisey commented 3 years ago

And another:

[15:57:11][D][ble_adv:058]: Possible Apple Watch? (mac 40:96:CC:08:25:E2) rssi -71, unrecognised action code 0x2e
[15:57:11][D][ble_adv:058]: Possible Apple Watch? (mac 40:96:CC:08:25:E2) rssi -68, unrecognised action code 0x2e
sct1000 commented 3 years ago

Let's try a more liberal match for unlocked watches by basically including the action code field.

So, replace this:

            if (sf == 0x98) {  // Match Apple Watches
              if (ac <= 0x0F) {
                best_rssi = max(rssi, best_rssi.value_or(rssi));
                ESP_LOGD("ble_adv", "Found Apple Watch (mac %s) rssi %i", x.address_str().c_str(), rssi);
              } else {
                ESP_LOGD("ble_adv", "Possible Apple Watch? (mac %s) rssi %i, unrecognised action code %#04x", x.address_str().c_str(), rssi, ac);
              }
            }

with something like:

            if (sf == 0x98) {  // Match Apple Watches
                ESP_LOGD("ble_adv", "Found Apple Watch (mac %s) rssi %i, action code %#04x", x.address_str().c_str(), rssi, ac);
            }

It would be interesting to capture any false positives from this.

dalehumby commented 3 years ago

Hi @adrianmoisey

Currently the code matches anything where the action code is in the range 0x00 through 0x0F, but as you've shown it looks like it can also be in the range 0x20 through 0x2F? @sct1000's suggestion is really good. There was one small bug in the above suggested code – the best_rssi still needs to be set, so it should be:

Replace:

            if (sf == 0x98) {  // Match Apple Watches
              if (ac <= 0x0F) {
                best_rssi = max(rssi, best_rssi.value_or(rssi));
                ESP_LOGD("ble_adv", "Found Apple Watch (mac %s) rssi %i", x.address_str().c_str(), rssi);
              } else {
                ESP_LOGD("ble_adv", "Possible Apple Watch? (mac %s) rssi %i, unrecognised action code %#04x", x.address_str().c_str(), rssi, ac);
              }
            }

with

            if (sf == 0x98) {  // Match Apple Watches
                best_rssi = max(rssi, best_rssi.value_or(rssi));
                ESP_LOGD("ble_adv", "Found Apple Watch (mac %s) rssi %i, action code %#04x", x.address_str().c_str(), rssi, ac);
            }

As suggested, please let me know if it detects anything that is not you Apple Watch.

dalehumby commented 3 years ago

I've made a change that should find Series 6 watches. If it doesn't work please reopen this ticket.

adrianmoisey commented 3 years ago

Thanks! It's working well now. I appreciate it.