tilmanginzel / alfred-bluetooth-workflow

Yet another Alfred workflow to connect / disconnect Bluetooth devices
MIT License
189 stars 8 forks source link

Failed notification shown for AirPods #10

Closed tbrek closed 4 years ago

tbrek commented 4 years ago

When trying to connect my AirPods I'm getting an error message, but they connect successfully:

image image
tilmanginzel commented 4 years ago

Hi @tbrek, thanks for the issue. This is a bit weird.

The code where that notification is generated is in blt.sh:

    ../blueutil --connect ${DEVICE}

    if [[ $(../blueutil --is-connected ${DEVICE}) -eq 1 ]]
    then
        ./notify.sh -m "Connected to ${TITLE}" -i success
    else
        ./notify.sh -m "Failed to connect to ${TITLE}. Click to retry." -i failure -c "blt.sh ${DEVICE} \"${TITLE}\""
    fi

So blueutil apparently says that the device is not connected. Can you please check the following:

Is the output 1 or 0?

tbrek commented 4 years ago
@./blueutil --recent
address: 88-64-40-e1-f8-5e, connected (master, -52 dBm), not favourite, paired, name: "Tom’s AirPods", recent access date: 2019-10-07 09:48:19 +0000

$ ./blueutil --is-connected 88-64-40-e1-f8-5e
1

It looks like blueutil sees it as connected.

tbrek commented 4 years ago

I think I fixed it. Adding sleep 1 helps as the script seems to be too fast, so, if blt.sh is:

#!/usr/bin/env bash

PARENT_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)
cd "${PARENT_PATH}"

DEVICE=$1
TITLE=$2

if [[ $(../blueutil --is-connected ${DEVICE}) -eq 1 ]]
then
    ../blueutil --disconnect ${DEVICE}

    if [[ $(../blueutil --is-connected ${DEVICE}) -eq 0 ]]
    then
        ./notify.sh -m "Disconnected from ${TITLE}"
    else
        ./notify.sh -m "Failed to disconnect ${TITLE}. Click to retry." -i failure -c "blt.sh ${DEVICE} \"${TITLE}\""
    fi
else
    ../blueutil --connect ${DEVICE}
    sleep 1
    if [[ $(../blueutil --is-connected ${DEVICE}) -eq 1 ]]
    then
        ./notify.sh -m "Connected to ${TITLE}" -i success
    else
        ./notify.sh -m "Failed to connect to ${TITLE}. Click to retry." -i failure -c "blt.sh ${DEVICE} \"${TITLE}\""
    fi
fi

It works fine for me.

tilmanginzel commented 4 years ago

@tbrek Hm that is interesting. If I am understanding the code behind blueutil correctly, it calls this API: IOBluetoothDevice.openConnection().

Quote from the documentation:

This method is synchronous and will not return until either a connection has been established or the create connection has failed (perhaps timed out).

Afterwards, IOBluetoothDevice.isConnected() is called, which theoretically should yield the same result.

Maybe I should try to not verify the connection via --is-connected, but rather check the exit code of the --connect command. It returns either 0 or 1, so should be easy to check. This would be a) faster, and b) hopefully more stable.

tilmanginzel commented 4 years ago

@tbrek Can you check if the following version does the trick (without any sleep)? :)

Bluetooth Connector.alfredworkflow.zip

avxkim commented 4 years ago

@tilmanginzel your archive complains regards etree.so and doesn't open.

tbrek commented 4 years ago

@tbrek Can you check if the following version does the trick (without any sleep)? :)

Bluetooth Connector.alfredworkflow.zip

Yes. That also fixed the issue. Thanks.

avxkim commented 4 years ago

@tbrek don't you have issues with etree.so with the above workflow?

tbrek commented 4 years ago

@webcoderkz no i don't. Could be Catalina related. I have not upgraded yet.

avxkim commented 4 years ago

Ah, i see, i thought you're on Catalina already.

@tilmanginzel please, update your latest workflow for Catalina. Because older version of workflow works (i just updated it using workflow:update to the latest). If i delete the old workflow, and download 0.5.0 it gives me etree.so error.

tilmanginzel commented 4 years ago

Yes. That also fixed the issue. Thanks.

Perfect! I have pushed the fix and released version 0.6.0 :)

@webcoderkz will comment in #9.