Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@mklaber

This comment has been minimized.

mklaber commented Aug 31, 2015

Works like a charm, thanks!

@brego

This comment has been minimized.

brego commented Sep 8, 2015

👍 Thanks!

@a-r-m-a-n

This comment has been minimized.

a-r-m-a-n commented Oct 31, 2015

Thanks! =)

@yatrikp

This comment has been minimized.

yatrikp commented Nov 24, 2015

Thanks.. it works!!!

@ShamilyJegadeesh

This comment has been minimized.

ShamilyJegadeesh commented Jan 8, 2016

Thanks.. It works 👍

@cladera

This comment has been minimized.

cladera commented Jan 27, 2016

Thanks!!!!

@njt1982

This comment has been minimized.

njt1982 commented Jan 28, 2016

Does not work on El Capitain... First command crashed OS X and caused a reboot.

@zakiaziz

This comment has been minimized.

zakiaziz commented Jan 29, 2016

My El Capitain didn't crash or reboot but still did not work.
First command yielded:

(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 1 instance.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).

and second command just turned off my bluetooth without the ability to turn it back on

@ramv-dailymotion

This comment has been minimized.

ramv-dailymotion commented Feb 16, 2016

👍

@aks0

This comment has been minimized.

aks0 commented Feb 19, 2016

@zakiaziz: were you able to figure this out? I'm facing the same issue.

@MauroNigrele

This comment has been minimized.

MauroNigrele commented Mar 5, 2016

Thanks! works fine on Yosemite

@gaizka-allende

This comment has been minimized.

gaizka-allende commented Mar 7, 2016

Thanks!

@mnnit-geek

This comment has been minimized.

mnnit-geek commented Mar 16, 2016

Not working in El Captain
❯❯❯ sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport ⏎ ⬆ ◼ (git:ir_646)
Password:
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel) Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 1 instance.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).

@justinmpier

This comment has been minimized.

justinmpier commented Apr 6, 2016

Thanks!

@BerkeleyTrue

This comment has been minimized.

BerkeleyTrue commented Apr 9, 2016

Works great!

@jruzafa

This comment has been minimized.

jruzafa commented Apr 19, 2016

works fine with 10.10.5 Yosemite

@promiseofcake

This comment has been minimized.

promiseofcake commented Apr 26, 2016

Thanks!

@coke

This comment has been minimized.

coke commented May 3, 2016

WOOT. Thank you.

@kaosbeat

This comment has been minimized.

kaosbeat commented May 19, 2016

perfect! (10.10.5)

@Dilts

This comment has been minimized.

Dilts commented May 27, 2016

worked perfectly (10.10.5)

@ghost

This comment has been minimized.

ghost commented Jul 7, 2016

worked perfectly (10.10.5)

@perlun

This comment has been minimized.

perlun commented Jul 11, 2016

As others have said, fails completely on 10.11.5 (El Capitan).

@ralph-hm

This comment has been minimized.

ralph-hm commented Jul 16, 2016

I couldn't find a similar solution on 10.11 (El Capitan), so i decided to restart the whole Bluetooth Module. Should work in upcoming versions too. See this gist: https://gist.github.com/ralph-hm/a65840c4f5e439b90170d735a89a863f

@maor

This comment has been minimized.

maor commented Jul 21, 2016

As others have said, fails completely on 10.11.5 (El Capitan).

Beware: Failed for me too on El Capitan.

@victorhguillen

This comment has been minimized.

victorhguillen commented Aug 16, 2016

A GUI version:

@AnsonFreeman

This comment has been minimized.

AnsonFreeman commented Aug 24, 2016

Do not work on El Capitan

@todd-alexion

This comment has been minimized.

todd-alexion commented Aug 30, 2016

Worked for me on Yosemite (10.10.5). Thanks.

@vj12354

This comment has been minimized.

vj12354 commented Sep 8, 2016

Awesome..! Worked like a charm..! Thanks.

@AliakseiMat

This comment has been minimized.

AliakseiMat commented Nov 4, 2016

Awesome!

@franciscbalint

This comment has been minimized.

franciscbalint commented Nov 8, 2016

Thank you @nicolasembleton!
I added turn on/off option to this script if someone also needs it.
https://gist.github.com/0xfib/d008370c49a37f7051b1afef8567fefd

@mlindhout

This comment has been minimized.

mlindhout commented Nov 18, 2016

Nice!

@jabrena

This comment has been minimized.

jabrena commented Nov 23, 2016

It works!!!

@j0hnsmith

This comment has been minimized.

j0hnsmith commented Nov 29, 2016

I've got a jagged line through the bluetooth icon in the menu bar, this doesn't work for me unfortunately.

(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 1 instance.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).
@davwilliams

This comment has been minimized.

davwilliams commented Dec 29, 2016

Just what I needed. And it works, too. :-) Thanks!

@maurotdo

This comment has been minimized.

maurotdo commented Jan 30, 2017

Flawless, just what i needed after bluetooth stopped working after a wake up from sleep

@Frozik

This comment has been minimized.

Frozik commented Mar 3, 2017

Works for me, thanks!

@Henryvart

This comment has been minimized.

Henryvart commented Mar 11, 2017

Worked for me! Yosemite 10.10.5. Thank you!

@kierenjohnson

This comment has been minimized.

kierenjohnson commented Mar 28, 2017

Worked on MacOS Sierra 10.12.3. Thanks!

@barnesry

This comment has been minimized.

barnesry commented Apr 5, 2017

Just saved me rebooting even after shift-option click to restart the BT module via debug menu didn't work. Thanks!

@Alfahad

This comment has been minimized.

Alfahad commented Apr 5, 2017

Thanks!

@kayakinwunmi

This comment has been minimized.

kayakinwunmi commented Apr 23, 2017

Thank you!

@netojose

This comment has been minimized.

netojose commented May 21, 2017

Works on Sierra.
Thanks, sometimes the bluetooth crashes

@renatop7

This comment has been minimized.

renatop7 commented May 21, 2017

Worked perfectly for me, thanks!!
I'm using Yosemite 10.10.5

@Snehasispanigrahi

This comment has been minimized.

Snehasispanigrahi commented Jun 16, 2017

Doesn't work on El Captain! Restart needed.

@patricknieto

This comment has been minimized.

patricknieto commented Jun 22, 2017

El Capitan 10.11.6:
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances: (kernel) Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 1 instance. Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).

@blattmann

This comment has been minimized.

blattmann commented Jun 29, 2017

Thanks! Worked perfectly on Yosemite 10.10.5

@lamlai

This comment has been minimized.

lamlai commented Jul 1, 2017

Thank so much!

@DTownSMR

This comment has been minimized.

DTownSMR commented Aug 23, 2017

Thanks much!

Working for me on Sierra 10.12.6.

Plus fixes my magic mouse connection issue instantly. :-)

@designbyadrian

This comment has been minimized.

designbyadrian commented Sep 28, 2017

High Sierra:

(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 1 instance.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).
@SMINT

This comment has been minimized.

SMINT commented Oct 10, 2017

Need solution for high sierra, after sleep Bluetooth dead until restart

@harveyphan

This comment has been minimized.

harveyphan commented Nov 6, 2017

High sierra, pain in the ass. Anyone know this trick for this OS?

@laopunk

This comment has been minimized.

laopunk commented Nov 16, 2017

you saved me a reboot, thanks a lot !

@henning

This comment has been minimized.

henning commented Nov 19, 2017

First command:

$ sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
Password:
(kernel) Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport not found for unload request.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) not found.

on Sierra, 10.12.6, MacBook Pro (13-inch, 2016, Two Thunderbolt 3 ports)

I don't get why this seems to work only for ~50% of people as I see about the same amount of "thanks" as "doesn't work for me" messages here... anyone got an idea?

@henning

This comment has been minimized.

henning commented Nov 19, 2017

Answering myself:

kextfind -loaded | grep -i blue

reveals that on my MacBook there are different dirvers loaded.

Unloading them "works", but I have to explicitly unload IOBluetoothHostControllerUARTTransport and IOBluetoothHostControllerTransport

like

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

Bluetooth devices /keyboard, trouchpad)stop working as expected.
On loading them again (replacing unload with load in the previous command), screen blacks out and system seems to do a warm restart.
Bluetooth works after that again for keyboard and external touchpad, but the bleutooth speaker, for which I tried toi do this all, still doesn't work, even though it always used to work for months now, and just this morning stopped being available as audio device, while being shown as connected as bluetooth device.

@PabloGBarcelo

This comment has been minimized.

PabloGBarcelo commented Nov 21, 2017

Great! it works like a charm!

@gbmeuk

This comment has been minimized.

gbmeuk commented Nov 22, 2017

@henning, I agree. This used to work without fail but seems to be intermittent now; and comes with the error you're seeing. Haven't done much testing but hopefully people will SEE THIS MESSAGE and post what OS version their running, along with the device (from the  menu > about this mac)

System Version: macOS 10.13.1
Model Name: MacBook Pro
Model Identifier: MacBookPro10,1

@AndrewJHart

This comment has been minimized.

AndrewJHart commented Nov 28, 2017

this is fantastic! Thanks so much 👍

@jhash

This comment has been minimized.

jhash commented Dec 12, 2017

I love you!

@Xyverz

This comment has been minimized.

Xyverz commented Jan 5, 2018

I was having an issue and getting the "failed to unload" error as well. I had my trackpad plugged in via USB though. As soon as I unplugged the trackpad and ran the command again, I got the same error, but the bluetooth services reset themselves and my trackpad connected via Bluetooth again.

@McChubby

This comment has been minimized.

McChubby commented Feb 2, 2018

@gbmeuk & @henning,
my details:
System version: 10.13.2
Model Name: MacBook Pro
Model Identifier: MacBookPro10,1

Issue: Currently experiencing trouble with reconnecting my bluetooth headset and even the bluetooth process on my Mbp looks like it is not finding any other bluetooth devices (tried other devices like my smartphone etc). In most situations (putting my mbp to sleep) the issue of reconnecting my headset starts. Now normally a reboot of my OS is sufficient too re-align the processes in the correct order. But even that won't do the trick atm. I found this script and the alternate version that @0xfib shared. both version don't seem to be working, getting the same return values as reported by other users. Are you guys currently anyway closer to a solution?

Returned values:

./bluetooth_restart.sh
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 2 instances.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).
Bluetooth restarted
@imsrc21

This comment has been minimized.

imsrc21 commented Mar 5, 2018

Getting error on macOS 10.13.3

./restart_bluetooth.sh 1

(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).
Bluetooth is on
@uyjulian

This comment has been minimized.

uyjulian commented Apr 7, 2018

macOS 10.13.4 works fine here to fix my bluetooth speaker connection

$ sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 5 instances.
$ sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

The more you do this, the more the number counts up

$ sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
(kernel) Can't unload kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; classes have instances:
(kernel)     Kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport class BroadcomBluetoothHostControllerUSBTransport has 6 instances.
$ sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

Let's get to 2,147,483,647?

This might also help:

sudo launchctl stop com.apple.bluetoothd;sudo launchctl stop com.apple.diagnosticextensions.osx.bluetooth.helper;sudo launchctl stop com.apple.bluetoothReporter;sudo launchctl stop com.apple.IOBluetoothUSBDFU; sudo launchctl stop com.apple.bluetoothaudiod;sudo kextunload -v 6 -b com.apple.iokit.IOBluetoothFamily;sudo kextunload -v 6 -b com.apple.iokit.IOBluetoothHostControllerTransport;sudo kextunload -v 6 -b com.apple.iokit.IOBluetoothHostControllerUSBTransport;sudo kextunload -v 6 -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
@uyjulian

This comment has been minimized.

uyjulian commented Apr 18, 2018

I found a even better solution.
Turn Bluetooth+Wifi off, then turn Wifi+Bluetooth on.
Works perfectly for me every time :-)

@ajain-bst

This comment has been minimized.

ajain-bst commented May 8, 2018

@uyjulian Thank you! It works!

@kriskhaira

This comment has been minimized.

kriskhaira commented Jun 10, 2018

@uyjulian's solution worked for me on High Sierra 10.13.5. Thank you!

@10fish

This comment has been minimized.

10fish commented Jun 22, 2018

codes above gives following response:

(kernel) Can't remove kext com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport; services failed to terminate - 0xxxxxx.
Failed to unload com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport - (libkern/kext) kext is in use or retained (cannot unload).

but this blueutil works for me on 10.13.5.

@max-lobur

This comment has been minimized.

max-lobur commented Jul 2, 2018

High Sierra 10.13.5 (MBP 13 Early 2015)
https://github.com/max-lobur/dotfiles/blob/master/sh/bt.sh - managed to fix like this

@will-dee

This comment has been minimized.

will-dee commented Jul 3, 2018

One solution which works for me on High Sierra is to kill the bluetooth daemon. It auto restarts which seems to sort it out. Here's a one liner (note it has to be done under sudo because the bluetoothd process isn't owned by $USER):

sudo kill -9 `pgrep bluetoothd`
@charafsalmi

This comment has been minimized.

charafsalmi commented Jul 19, 2018

➜ ~ ./restart_bluetooth.sh
(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).

➜ ~ uname -a
Darwin MacBook-Pro-XXX.local 18.0.0 Darwin Kernel Version 18.0.0: Sun Jun 10 19:23:56 PDT 2018; root:xnu-4903.200.249~5/RELEASE_X86_64 x86_64
➜ ~ system_profiler SPSoftwareDataType
Software:
System Software Overview:
System Version: macOS 10.14 (18A314k)
Kernel Version: Darwin 18.0.0
Boot Volume: Macintosh HD
Boot Mode: Normal
Computer Name: XXX
User Name: XXX
Secure Virtual Memory: Enabled
System Integrity Protection: Enabled
Time since boot: 5 days 1:43

@thehalvo

This comment has been minimized.

thehalvo commented Jul 26, 2018

Thanks!

@Jiff21

This comment has been minimized.

Jiff21 commented Aug 3, 2018

I'm on sierra and was getting the same 'Can't remove ...' error. You can just hold Shift+Option and click on the Bluetooth Icon Top Menu, It will reveal an option called Debug Menu item that has a Reset the Bluetooth Module option.

If you need to do this in terminal for some reason, stackoverflow suggests.

sudo launchctl stop com.apple.blued
sudo launchctl start com.apple.blued
@coolaj86

This comment has been minimized.

coolaj86 commented Aug 18, 2018

A Comprehensive Solution

/usr/local/bin/blueutil -p 0 alone didn't work for me, I did this:

~/.local/bin/restart-bluetooth:

#!/bin/bash

# Docs I read
# https://gist.github.com/nicolasembleton/afc19940da26716f8e90
# https://gist.github.com/ralph-hm/a65840c4f5e439b90170d735a89a863f
# https://github.com/max-lobur/dotfiles/blob/master/sh/bt.sh

# Things I had to do beforehand
#brew install blueutil
# Restart bluetooth every time good ol' lappy wakes up:
#brew install sleepwatcher
#sudo mkdir -p /usr/local/sbin
#sudo chown -R $(whoami):staff /usr/local/sbin/
#brew link sleepwatcher
#/usr/local/sbin/sleepwatcher --verbose -w ~/.local/bin/restart-bluetooth
#brew services start sleepwatcher


# Stop all the bluetooth stuff
sudo kill -9 $(pgrep bluetoothd)
/usr/local/bin/blueutil -p 0
sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
sudo kextunload -b com.apple.iokit.IOBluetoothHostControllerUARTTransport
sudo launchctl stop com.apple.blued

# Start enough of the bluetooth stuff
/usr/local/bin/blueutil -p 1
sudo launchctl start com.apple.blued
sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

It's only sometimes that my bluetooth doesn't reconnect after sleep & wake (may be related to docking to my Thunderbolt Display) and I don't know how to immediately cause the problem, so I don't know what about that process is required. Obviously some of it is superfluous, but if I run that script my bluetooth dies and comes right back, so I know it's working well enough.

If someone can pare it down to just what's necessary I'd love to hear the explanation (ping me @coolaj86).

@BryanBo-Cao

This comment has been minimized.

BryanBo-Cao commented Oct 1, 2018

Awesome!!! Thanks!!!

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