Skip to content

Instantly share code, notes, and snippets.

@nicolasembleton
Last active October 21, 2022 20:10
Show Gist options
  • Save nicolasembleton/afc19940da26716f8e90 to your computer and use it in GitHub Desktop.
Save nicolasembleton/afc19940da26716f8e90 to your computer and use it in GitHub Desktop.
Restart Bluetooth Daemon on Mac OS X without restarting
#!/bin/bash
sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
@futursimple
Copy link

futursimple commented Dec 17, 2019

i got the "failed to unload error"... presumably because it was already unloaded and just needed to load via step 2... which #workedforme :D

10.13.6 High Sierra on late-2013 15" Retina MBP

@vis-kid
Copy link

vis-kid commented Jan 26, 2020

My result:
"Bluetooth: Not Available"

@tenly2000
Copy link

First command failed for me because something was "already in use or retained" but I went ahead and ran the second command anyhow, then started Bluetooth - and everything worked! Thank you!

@iexa
Copy link

iexa commented Jul 3, 2020

worked fine. although I might add that I had a stuck (>20% cpu usage in activity monitor and no audio whatsoever playing or connected via bt) bluetoothaudiod service and killed that first.

Then the op first command gave this:
(kernel) Can't remove kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; services failed to terminate - 0xe00002c7. Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (iokit/common) unsupported function.

But the second command went without a hitch and now I have a working bt audio again without restarting. Good. & thanks! mojave latest version.

@aradnom
Copy link

aradnom commented Jul 20, 2020

Still works great on Mojave. Had a similar issue as some other people with unloading the daemon giving me an error, but then loading it again worked fine. Will reboot at some point here but this is really handy when your bluetooth bites the dust and you'd really prefer not to reboot just yet.

@mgguinne
Copy link

I am having this issue nearly every day now, nothing I do works. I have to reboot. Making me think to fix = new mac 👎

➜  ~ sudo kextunload -b com.apple.iokit.BroadcomBluetooth20703USBTransport
sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
Password:
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetooth20703USBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetooth20703USBTransport class BroadcomBluetooth20703USBTransport has 4 instances.
Failed to unload com.apple.iokit.BroadcomBluetooth20703USBTransport - (libkern/kext) kext is in use or retained (cannot unload).
(kernel) Can't remove kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; services failed to terminate - 0xdc008018.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).

The below is the closest that might work, but I might have run some other commands first as Bluetooth now shows "Not Available". Will have to test next time it fails.

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

My Bluetooth devices.

➜  ~ kextfind -loaded | grep -i blue                                               
/System/Library/Extensions/IOBluetoothFamily.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/BroadcomBluetooth20703USBTransport.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/IOBluetoothSerialManager.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/IOBluetoothHostControllerUSBTransport.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/BroadcomBluetoothHostControllerUSBTransport.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/IOBluetoothHostControllerTransport.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/IOBluetoothUSBDFU.kext
/System/Library/Extensions/IOBluetoothFamily.kext/Contents/PlugIns/IOBluetoothPacketLogger.kext
/System/Library/Extensions/AppleTopCase.kext/Contents/PlugIns/AppleHSBluetoothDriver.kext
/System/Library/Extensions/IOBluetoothHIDDriver.kext
uname -a
Darwin MacBook-Pro.local 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul  5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64 x86_64

system_profiler SPSoftwareDataType
Software:

System Software Overview:

  System Version: macOS 10.15.6 (19G73)
  Kernel Version: Darwin 19.6.0
  Boot Volume: Macintosh HD
  Boot Mode: Normal
  User Name: mgguinne
  Secure Virtual Memory: Enabled
  System Integrity Protection: Enabled
  Time since boot: 1 day 23 minutes

@caballerofelipe
Copy link

My bluetooth connected device, for audio, keeps connecting/disconnecting.

I don't have the answer to the problem but I can tell that the process /usr/sbin/bluetoothaudiod keeps restarting (maybe it crashes and restarts) or whatever. I can tell because I'm running while 1; do pgrep bluetoothaudiod; sleep 1; done and there is a PID, then there isn't, then there is another PID, rinse and repeat...

@caballerofelipe
Copy link

caballerofelipe commented Aug 6, 2020

A few notes about the topic that might be some help to somebody:

  • Maybe Music (formerly iTunes) or iTunes, depending on your OS version, is causing the problem. I've been struggling for about an hour now with this and after quitting Music the problem is gone. Although note that I did try a bunch of commands before but it seemed that none worked, sound to BT was restored once I closed Music.
  • I just found out that bluetoothaudiod is only live while an audio device is connected. When disconnecting the device pgrep bluetoothaudiod doesn't return anything.
  • Along with the previous bullet, another thing that might have worked (I doubt it) is that I opened 'System Information' (Apple Menu > About This Mac > System report).
  • I don't want to restart my Mac but... when I've tried, the problems goes away. So I would love to have a solution. The use case is as follows: you're using your mac to work (or whatever) and want to hear some music, BT keeps disconnecting so you have to either not use BT or restart, which will kill your workflow.
  • Even though audio connects/disconnects my Magic Mouse keeps working.
  • This might help How to fix Bluetooth headset audio issues on macOS Catalina?, read the link but basically the options here are:
    • Disable Ask Siri and Turn off Voice Control feature
    • Reset NVRAM

I hope there is some straightforward solution to this... 😕

@rogerluan
Copy link

rogerluan commented Aug 19, 2020

macOS Catalina, NONE of these solutions are working:

And the symptoms are: when I try to connect to my AirPods, it connects, but the audio doesn't connect. After exactly 10 seconds, the AirPods disconnect (bluetooth still on, audio still goes through the internal speakers).

I second @cabellerofelipe: rebooting the mac does the trick, but it's a pain in the ass 😞

Does anyone have any other ideas? 😬

@rogerluan
Copy link

I realized there was a macOS Catalina update available and decided to update https://support.apple.com/en-us/HT210642
It supposedly fixed:

Resolves an issue where certain USB mouse and trackpads may lose connection

Which I'd probably understand that it was actually issues with bluetooth (of which they forgot to mention sound devices).

I was forced to reboot, so the issue is gone for me but I'll definitely come back here if the bluetooth issue arrises again.

@rogerluan
Copy link

rogerluan commented Aug 19, 2020

I took a stab at fixing this by unloading and re-loading all the kext that is related to audio and bluetooth. I have no understanding of kext, audio or bluetooth, though, so bear with my solution:

function xgh() {
    shit_to_reboot=($(kextstat | grep -i "blue" | awk '{print $6}'))
    echo "Found ${#shit_to_reboot[@]} shit to reboot:"
    for shit in ${shit_to_reboot[@]}; do
        echo "Rebooting $shit"
        sudo kextunload -b "$shit"
        sleep 5
        sudo kextload -b "$shit"
        echo "Rebooted $shit"
    done

    shit_to_reboot=($(kextstat | grep -i "audio" | awk '{print $6}'))
    echo "Found ${#shit_to_reboot[@]} shit to reboot:"
    for shit in ${shit_to_reboot[@]}; do
        echo "Rebooting $shit"
        sudo kextunload -b "$shit"
        sleep 5
        sudo kextload -b "$shit"
        echo "Rebooted $shit"
    done
}

kextunload man: https://www.manpagez.com/man/8/kextunload/osx-10.4.php

However, the code above doesn't work because all those kext have dependencies that you can check by calling kextstat.

Next, I was going to recursively unload all the dependencies before attempting to unload a kext that has dependencies alive. That would be my next move.

Before doing that, I realized that there was a macOS update available https://support.apple.com/en-us/HT210642 that supposedly fixed an issue where certain USB mouse and trackpads may lose connection. I'm gonna assume that that issue was affecting all bluetooth connections, so speakers could be involved.

I decided to update my mac to the latest version, which required a reboot, so the problem stopped occurring (as stated in my last post above).

I'll definitely come back to this thread if the problem happens again though. So far, several hours have lapsed without facing the issue.

Hope this helps someone.

@caballerofelipe
Copy link

caballerofelipe commented Aug 27, 2020

I just started having choppy sound and tried this, which apparently worked, no problem for 5 minutes so far.

  1. Closed Music (iTunes)
  2. Turned off headset (Bose QC35 ii)
  3. Turned headset on, macOS wasn't detecting it (iOS did connect automatically)
  4. Turned off bluetooth
  5. Turned on bluetooth
  6. macOS wasn't automatically connecting headset
  7. Manually connected headset
  8. Opened Music
  9. Now listening to music for 5 minutes.

I'm using macOS 10.15.6 (19G2021)

@rogerluan
Copy link

rogerluan commented Aug 27, 2020

I'll definitely come back to this thread if the problem happens again though. So far, several hours have lapsed without facing the issue.

I have faced the problem again several times. However, after the update, running:

sudo kill `pgrep coreaudiod`

seems to be actually killing the audio system (which wasn't happening before), which actually resolves the problem. So, the problem still arrises, however it is fixable without rebooting 😬 (so far, anyway).

@iexa
Copy link

iexa commented Sep 2, 2020

I found that killing the bluetoothaudiod service helps if audio does not seem to be working. This case must be similar to the coreaudiod one. Thx.

@rogerluan
Copy link

rogerluan commented Oct 15, 2020

I filed a radar about this. Then added it to open radar: https://openradar.appspot.com/FB8801301

@igorrs
Copy link

igorrs commented Nov 4, 2020

The wifi+bluetooth trick worked for me. Thanks.

@agate
Copy link

agate commented Mar 19, 2021

#!/usr/bin/env bash

pgrep audio | xargs sudo kill
pgrep bluetooth | xargs sudo kill

sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl start
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl start

I use this script and fixed my problem :D

@dmitrym0
Copy link

```shell
#!/usr/bin/env bash

pgrep audio | xargs sudo kill
pgrep bluetooth | xargs sudo kill

sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl start
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl start

I use this script and fixed my problem :D

Looks like you can't react to gists so 👍

@xab3r
Copy link

xab3r commented Apr 14, 2021

macOS Catalina, NONE of these solutions are working:

And the symptoms are: when I try to connect to my AirPods, it connects, but the audio doesn't connect. After exactly 10 seconds, the AirPods disconnect (bluetooth still on, audio still goes through the internal speakers).

I second @cabellerofelipe: rebooting the mac does the trick, but it's a pain in the ass 😞

Does anyone have any other ideas? 😬

I had a similar issue with BT on my MacBook Pro 13" 2017 for some period of time. Seems it was a software problem because it's gone over time, I assume with some of the MacOS updates.
My solution was to plug an external BT dongle to the laptop, make sure OS recognized it and then unplug it. Sometimes I tried to connect/disconnect the headset via that external adapter. This trick always worked. That sounds crazy but I had to keep the "rescue" BT adapter in my backpack.

@rogerluan
Copy link

Latest update from me, who initially wrote this: https://gist.github.com/nicolasembleton/afc19940da26716f8e90#gistcomment-3422499

I've been running macOS 11.2 since Feb 8th and since then the bluetooth problems decreased from like "a few times a day" to "maybe once a week" or even less often than that.

@opsforce
Copy link

opsforce commented Jul 9, 2021

just $ sudo pkill bluetoothd
then daemon bluetooth service will restart self (tested success on macOS 10.14+).

@paulbargaoanu
Copy link

#!/usr/bin/env bash

pgrep audio | xargs sudo kill
pgrep bluetooth | xargs sudo kill

sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl start
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl start

I use this script and fixed my problem :D

Seems to be working on a Mac Mini 2018 running Mac OS Big Sur 11.5.2 (20G95)

Thank you very much, kind stranger! 🙏

@agate
Copy link

agate commented Aug 27, 2021

@paulbargaoanu you are welcome. I am glad it works :D

@dobando-dev
Copy link

#!/usr/bin/env bash

pgrep audio | xargs sudo kill
pgrep bluetooth | xargs sudo kill

sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl start
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl start

I use this script and fixed my problem :D

Works for BigSur. Thanks!

@AnaatGitHub
Copy link

#!/usr/bin/env bash

pgrep audio | xargs sudo kill
pgrep bluetooth | xargs sudo kill

sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i blue | awk '{ print $3 }' | xargs sudo launchctl start
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl stop
sudo launchctl list | grep -i audio | awk '{ print $3 }' | xargs sudo launchctl start

After months of searching and trying stuff, this seems to work on Catalina OS (10.15.7) on a MacBook Pro 8,1. As soon as I ran the script, the bluetooth started working again. No rebooting or resetting the SMC module needed so far. The bluetooth conection will still drop out after a while, but the script is definitely more convenient than the rebooting every 30 minutes like previously.

Thanks a bunch, @agate ! Do you have any idea as to why this works and if there is a more permanent fix?

Cheers.

@agate
Copy link

agate commented Feb 8, 2022

@AnaatGitHub so I just violently killed all the Bluetooth and Audio related processes. I guess MacOS is smart enough to restart all those background processes if you are trying to use them later. You know "reboot fixes almost 99 percent problem" lol.

BTW, I am not sure if there is any other way / app for doing such thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment