toy / blueutil

CLI for bluetooth on OSX: power, discoverable state, list, inquire devices, connect, info, …
https://github.com/toy/blueutil
Other
1.01k stars 53 forks source link

Workflow to reconnect a keyboard to another mac #46

Closed JanC closed 3 years ago

JanC commented 3 years ago

Hey @toy many thanks for this tool and for having it open source 👍

I'm often switching a keyboard from on mac to another. My current workflow that somehow works without using blueutil is to

I'm looking for a way to simplify it and found your tool. However, it seems I have a similar issue to this one https://github.com/toy/blueutil/issues/38

I'm aware the removing/unmpairing completely the device does not seem to be possible as explained in https://github.com/toy/blueutil/issues/31

I try by using a simple disconnect as follows

on mac1

blueutil --info $my_keyboard
address: 11-22-33-44-55-66, connected (master, -47 dBm), not favourite, paired, name: "MyKeyboard", recent access date: 2021-02-12 08:51:45 +0000

blueutil --disconnect $my_keyboard

on mac2

blueutil --info $my_keyboard
address: 11-22-33-44-55-66, not connected, not favourite, paired, name: "MyKeyboard", recent access date: 2021-02-12 09:03:56 +0000

blueutil --connect $my_keyboard
blueutil --info $my_keyboard

address: 11-22-33-44-55-66, not connected, not favourite, not paired, name: "MyKeyboard", recent access date: 2021-02-12 09:07:16 +0000

blueutil --info $my_keyboard
address: 11-22-33-44-55-66, connected (master, -50 dBm), not favourite, not paired, name: "MyKeyboard", recent access date: 2021-02-12 09:07:46 +0000

blueutil --pair $my_keyboard

blueutil --info $my_keyboard
address: 11-22-33-44-55-66, not connected, not favourite, paired, name: "MyKeyboard", recent access date: 2021-02-12 09:08:31 +0000

blueutil --connect $my_keyboard
blueutil --info $my_keyboard
address: 11-22-33-44-55-66, not connected, not favourite, not paired, name: "MyKeyboard", recent access date: 2021-02-12 09:08:45 +0000

It basically seems it's impossible to pair or connect they keyboard on the mac2 unless I manually unpair un from mac1.

Are there any other commands to try to be able to switch a keyboard from one mac to another somewhat easily? Would you consider adding the unapair command that would use this api even if it needs a root access?

toy commented 3 years ago

Hey Jan First thank you for easily understandable issue and for checking previous ones :)

Short version - did you try (temporarily) turning bluetooth off on mac1?

As I understand bluetooth devices will normally try to connect to last connected device. And I've just checked if I can come up with decently reliable way to switch a keyboard between two macs (I'll call them mac1 and mac2 too). Both are paired with the keyboard and have bluetooth turned on.

If I rotate bluetooth power on mac1 using: blueutil -p 0; sleep 5; blueutil -p 1 and press some key on the keyboard, then not very reliably but it connects to mac2, a bit weird is that it seems to be doing it after sleep on mac1 finishes (or it already turns power back on), so decreasing sleep time makes connection happen faster, but also decreases reliability. Then I checked if just starting blueutil --connect "xxx" on mac2 and right after that running blueutil --disconnect "xxx" on mac1 will be enough, but it didn't work for me. What finally worked well was to start blueutil --connect "xxx" on mac2 and right after that running blueutil -p 0; sleep 5; blueutil -p 1 on mac1. Switching commands I was able to jump between computers pretty quickly. Is it an option for you and if yes please check if it works?

About unpairing api - I'm fine to try it, but check if the other solution works for you.

JanC commented 3 years ago

turning the BT off on mac1 (even completely) does not seem to help. I noticed that on mac2, after I issue the connect command, the keyboard is in a kind of limo state where it's non paired but connected

blueutil --pair $my_keyboard
Failed to pair "xyz" with error 0x05 (Authentication Failure)

blueutil --info $my_keyboard
connected (master, -41 dBm), not favourite, not paired, name: "MyKeyboard", recent access date: 2021-02-15 09:25:35 +0000

blueutil --info $my_keyboard
not connected, not favourite, not paired, name: "MyKeyboard", recent access date: 2021-02-15 09:25:35 +0000

In the System Preferences, it appears in the device list without the "Connect" button but also without a subtitle that indicates whether it's connected or not.

Screenshot_2021-02-15_at_10_28_47

It all works flawlessly when I manually unpair the keyboard on mac1 and then on mac2:

blueutil --pair $my_keyboard
blueutil --connect $my_keyboard
blueutil --info $my_keyboard
address: xyz, connected (master, -41 dBm), not favourite, paired, name: "MyKeyboard", recent access date: 2021-02-15 09:34:10 +0000

I understand that this is more a Bluetooth / macOS question so feel free to close this ticket.

toy commented 3 years ago

I noticed in your original post that paired state was jumping which looks weird. Maybe it is worth trying to reset bluetooth module and/or erasing paired devices at least on mac2. One way of doing it is from Debug menu in bluetooth menu item if you open it while pressing ⌥⇧.

toy commented 3 years ago

After having no luck at all with the method described on stackoverflow:

I then stumbled upon an undeclared method 🎉 It will be great if you can try the changes in branch unpair

JanC commented 3 years ago

hey, thanks for looking into this.

it seems to work nicely:

on mac1:

./blueutil --unpair $my_keyboard

on mac2:

./blueutil --pair $my_keyboard
./blueutil --connect $my_keyboard

👍 🎈

I'd suggested wrapping this line in a if respondsToSelector to avoid crashes

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
IOBluetoothDevice *device = get_device(args->device_id);
if ([device respondsToSelector:@selector(remove)]) {
    [device performSelector:@selector(remove)];
    return EXIT_SUCCESS;
} else {
    return EXIT_FAILURE;
}
#pragma clang diagnostic push
toy commented 3 years ago

I'd suggested wrapping this line in a if respondsToSelector to avoid crashes

Thanks!

toy commented 3 years ago

Just released v2.9.0 which includes unpairing.

wds15 commented 2 years ago

Just found this and started using it...works like a charm. Thanks!

BhavanaPrabhakar commented 2 years ago

i'm trying to pair iPhone device with Mac mini by using following command

blueutil --pair xx-xx-xx-xx-xx-xx

but i'm getting - this error

Does "iPhone-QA1" (xx-xx-xx-xx-xx-xx) display number 384659 (yes/no)? YES Failed to pair "xx-xx-xx-xx-xx-xx" with error 0x05 (Authentication Failure)

is there anyway, i can remotely connect bluetooth between iphone and mac mini?

toy commented 2 years ago

@BhavanaPrabhakar Is failure appearing instant after confirming or after some time? I think iPhone may also be asking for confirmation even though it didn't initiate pairing.

BhavanaPrabhakar commented 2 years ago

@toy, it appears instantly. i tried with yes and no, multiple times but, i get the same error all the time.

i usually connect to iPhone remotely using Vysor. But at first, it needs to be connected to Bluetooth and this is one time activity. But due to system restart or something bluetooth connection is disconnected and i'm trying to connect using blueutil via terminal

toy commented 2 years ago

@BhavanaPrabhakar Do you need to re-pair iPhone or re-connect? If it is already paired then blueutil --connect xx-xx-xx-xx-xx-xx

BhavanaPrabhakar commented 2 years ago

currently it says "connection failed"(screenshot below)

Screen Shot 2022-08-02 at 3 53 48 PM

I'm trying to pair and connect but pair is giving me this error -

Does "iPhone-QA1" (xx-xx-xx-xx-xx-xx) display number 384659 (yes/no)? YES Failed to pair "xx-xx-xx-xx-xx-xx" with error 0x05 (Authentication Failure)

toy commented 2 years ago

The screenshot seems to suggest that the device is already paired, is it not?