Friday, March 29, 2024
spot_img

Fixing Yosemite Bluetooth Issues After Standby

Introduction

Perhaps my 2010 MacBookPro is starting to feel its age? For some reasons it’s had a slew of Wi-Fi and Bluetooth related issues in recent months. The most recent is that after coming out of Sleep (standby) Bluetooth would remain in dreamland and refuse to function. In most cases I can’t turn it On or Off, or select any options or bring up context menus in the Bluetooth Preferences panel. Or if I can use the context menus, Connect and Disconnect don’t do much. It usually reports my external keyboard and trackpad are connected, but nothing works. It seems like the Bluetooth system has gone into a deep coma or died in its sleep.

I am about to install a Bluetooth 4.0 adapter (this is the one I recommend: StarTech.com Bluetooth Adapter – Mini Bluetooth), and perhaps the built-in BT has cottoned on, and is kicking up its feet in disgust? Truth is, if it wasn’t messing with the Wi-Fi connection so much in the past month (seems the two have been at war with each other), I’d not have considered adding an external BT adapter to begin with — more on that in another article. Here’s how to potentially resolve these issues of BT dyeing in its sleep.

Solution for Bluetooth failure after Sleep mode

The best solution I found was to unload and load the KEXT for the Bluetooth hardware. If you don’t know what a KEXT file is, don’t worry. Kind folks have provided an automated (scripted) way to deal with this. First step is to test whether this method actually resolves your Bluetooth failure after system sleep.

NB. In a few places these instructions assume you are using OS X Yosemite, as that’s the OS in which this Bluetooth issue commonly occurs. If you have at least OS X 10.8, you should be fine. The only relevant point is in the way .command files are handled in Finder.

1) Pre-test: First you’ll need to test this out. Run the following two lines of code in Terminal.app

sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

You will be prompted for your password. Type it in (no text will appear when you type it, so just hit Return/Enter key after you type it in).

Now test out your Bluetooth peripherals. If they are working again, then the above solution works for you. So the next step is to automate it.

Automation of the fix:

There’s a few ways to do this. We’ll cover three. If you have Homebrew installed on your system, follow step 2A. If you don’t have Homebrew installed (and perhaps don’t even know what that is, except a DIY way to make beer) follow instructions at 2B. Or, alternatively, if you prefer to use Automator, follow instuctions at 2C.

The first two methods will install a neat little utility called Sleepwatcher. This gives you a means to have scripts, etc., run when your system sleep state changes in any way. The third method, using Automator, is less automated. It gives you a way to manually execute the command line entries you tested in step 1 above.

2 A) HOMEBREW METHOD (with Sleepwatcher)

Create a text file in your user home directory (or, for instance, in your Downloads folder) using a text editor. Paste in the following code:

#!/bin/sh
# Script credit goes to https://gist.github.com/timgws/fc63aeca6a248bbb25ff 
brew install sleepwatcher
 
# https://gist.github.com/jagtesh/de81fa1c6b45fad0ff8e
sudo cp /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchAgents/
sudo cp /usr/local/Cellar/sleepwatcher/2.2/etc/sleepwatcher/rc.* /etc/
 
# Add bluetooth script to /etc/rc.wakeup (the script requires root)
sudo tee -a /etc/rc.wakeup <<EOF

##
## Restart blued & load/unload bluetooth driver.
##

function msg() {
    foo=\$2
    what="\$(tr '[:lower:]' '[:upper:]' <<< \${foo:0:1})\${foo:1}"
    echo ">> \${what}ing \$1...";
}

function __blued() {
    msg "blued" "\$1";
    sudo launchctl \$1 /System/Library/LaunchDaemons/com.apple.blued.plist
}

function __driver() {
    msg "bluetooth driver" "\$1";
    sudo kext\$1 -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
}

__blued "unload"
__driver "unload"

sleep 2;

__driver "load";
__blued "load"

EOF
 
# Load the agent to start sleepwatcher
sudo launchctl load /Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist

Save the file with the name bt-fix.command

Execute the script

In Yosemite (and likely OS X 10.8 or later) you can now double-click .command the file in Finder, and it should execute.

You may need to make the script executable with the following command in Terminal.app

chmod a+x (/path/to/yourscriptname.command)

Unless you know how to change directory in Terminal over to where you saved your script, the easiest way to get the script name in there with the path is to just type the chmod a+x part into Terminal, add one space, and then drag the .command file from Finder to the Terminal window. That will automatically insert a reference to the file, with the full path.

2B) SCRIPT METHOD, NO HOMEBREW (with Sleepwatcher)

Create a text file in your user home directory (or, for instance, in your Downloads folder) using a text editor. Paste in the following code:

#!/bin/bash
# Script credit goes to https://gist.github.com/Tyilo/c92684d277acb62272b5
# Install sleepwatcher
cd /tmp
curl -O http://www.bernhard-baehr.de/sleepwatcher_2.2.tgz
tar -zxvf sleepwatcher_2.2.tgz
cd sleepwatcher_2.2
sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
sudo cp sleepwatcher /usr/local/sbin
sudo cp sleepwatcher.8 /usr/local/share/man/man8
sudo cp config/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchAgents
sudo cp config/rc.* /etc
cd ..
rm -r sleepwatcher_2.2*
 
# Add bluetooth script to /etc/rc.wakeup (the script requires root)
sudo tee -a /etc/rc.wakeup <<EOF
kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
EOF
 
# Load the agent to start sleepwatcher
sudo launchctl load /Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist

Save the file with the name bt-fix.command to a location that makes sense to you.

Execute the script

In Yosemite (and likely OS X 10.8 or later) you can now double-click .command the file in Finder, and it should execute.

You may need to make the script executable with the following command in Terminal.app

chmod a+x (/path/to/yourscriptname.command)

Unless you know how to change directory in Terminal over to where you saved your script, the easiest way to get the scriptname in there with the path is to just type the chmod a+x part into Terminal, add one space, and then drag the .command file from Finder to the Terminal window. That will automatically insert a reference to the file, with the full path.

2C) AUTOMATOR METHOD (without Sleepwatcher)

  1. Use Spotlight to find and run Automator
  2. Click “New Document” at bottom
  3. Choose “Application” for the new file type
  4. Click on “Utilities” from the list
  5. Double-Click “Run AppleScript”
  6. Replace (* Your script goes here *) with:
do shell script "kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; 
kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport" with administrator privileges
  • Press Play to test the automation. You will be propted for your password. Enter it.
  • Once done, save the file to somewhere convenient, such as your Desktop. File -> Save.

The Lazy Way

If you know nothing about Terminal, except that it sounds fatal, and all of the above seems to daunting and esoteric, you can just run the following application. I have bundled up solution 2B into an app that will do what’s required. Use at your own risk (I can’t think of any risks, but obviously the responsibility is yours, and not mine).

Download this App. Run it only once, if successful. Otherwise it will repeat certain steps and make a mess of your SleepWatcher configuration. Not a major to manually clean up, but worth avoiding. There is no reason to run it to completion more than once. It will install SleepWatcher, and give it the necessary commands to reload Bluetooth kexts after your computer comes out of sleep mode.

Credit

Credit for the ideas in this post go to the good folk involved in this discussion on apple.stackexchange.com

 

Want to upgrade Bluetooth on your Mac to v4? Improve performance and range, and avoid the pesky internal bluetooth issues described in this article.
Check out StarTech.com Bluetooth Adapter – Mini Bluetooth on Amazon.com

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox