Buggy code:
private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30 ; private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31 ; private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40 ; private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41 ; private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42 ; private static final int MESSAGE_BLUETOOTH_STATE_CHANGE = 60 ; private static final int MESSAGE_TIMEOUT_BIND = 100 ; private static final int MESSAGE_TIMEOUT_UNBIND = 101 ; private static final int MESSAGE_GET_NAME_AND_ADDRESS = 200 ; private static final int MESSAGE_SAVE_NAME_AND_ADDRESS = 201 ; private static final int MESSAGE_USER_SWITCHED = 300 ; private static final int MAX_SAVE_RETRIES = 3 ; private static final int MAX_ERROR_RESTART_RETRIES = 6 ; private static final int BLUETOOTH_OFF = 0 ; private static final int BLUETOOTH_ON_BLUETOOTH = 1 ; private static final int BLUETOOTH_ON_AIRPLANE = 2 ; private static final int SERVICE_IBLUETOOTH = 1 ; private static final int SERVICE_IBLUETOOTHGATT = 2 ; private static final int SERVICE_IBLUETOOTHQ = 3 ; private final Context mContext ; private String mAddress ; private String mName ; private final ContentResolver mContentResolver ; private final RemoteCallbackList < IBluetoothManagerCallback > mCallbacks ; private final RemoteCallbackList < IQBluetoothManagerCallback > mQCallbacks ; private final RemoteCallbackList < IBluetoothStateChangeCallback > mStateChangeCallbacks ; private IBluetooth mBluetooth ; private IQBluetooth mQBluetooth ; private IBluetoothGatt mBluetoothGatt ; private boolean mBinding ; private boolean mUnbinding ; private boolean mQuietEnable = false ; private boolean mQuietEnableExternal ; private boolean mEnableExternal ; private boolean mEnable ; private int mState ; private final BluetoothManagerService . BluetoothHandler mHandler ; private int mErrorRecoveryRetryCounter ; private final int mSystemUiUid ; private boolean mIsBluetoothServiceConnected = false ; private void registerForAirplaneMode ( IntentFilter filter ) { } private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback . Stub ( ) { @ Override public void onBluetoothStateChange ( int prevState , int newState ) throws RemoteException { } } ; private final BroadcastReceiver mReceiver = new BroadcastReceiver ( ) { @ Override public void onReceive ( Context context , Intent intent ) { } } ; BluetoothManagerService ( Context context ) { } private final boolean isAirplaneModeOn ( ) { } private final boolean isBluetoothPersistedStateOn ( ) { } private final boolean isBluetoothPersistedStateOnBluetooth ( ) { } private void persistBluetoothSetting ( int value ) { } private boolean isNameAndAddressSet ( ) { } private void loadStoredNameAndAddress ( ) { } private void storeNameAndAddress ( String name , String address ) { } public IBluetooth registerAdapter ( IBluetoothManagerCallback callback ) { } public void unregisterAdapter ( IBluetoothManagerCallback callback ) { } public IQBluetooth registerQAdapter ( IQBluetoothManagerCallback callback ) { } public void unregisterQAdapter ( IQBluetoothManagerCallback callback ) { } public void registerStateChangeCallback ( IBluetoothStateChangeCallback callback ) { } public void unregisterStateChangeCallback ( IBluetoothStateChangeCallback callback ) { } public boolean isEnabled ( ) { } public void getNameAndAddress ( ) { } public boolean enableNoAutoConnect ( ) { } public boolean enable ( String callingPackage ) { } public boolean disable ( boolean persist ) { } public void unbindAndFinish ( ) { } public IBluetoothGatt getBluetoothGatt ( ) { } public IQBluetooth getQBluetooth ( ) { } private void sendBluetoothStateCallback ( boolean isUp ) { } private void sendBluetoothServiceUpCallback ( ) { } private void sendBluetoothServiceDownCallback ( ) { } private void sendQBluetoothServiceUpCallback ( ) { } private void sendQBluetoothServiceDownCallback ( ) { } public String getAddress ( ) { } public String getName ( ) { } private class BluetoothServiceConnection implements ServiceConnection { private boolean mGetNameAddressOnly ; public void setGetNameAddressOnly ( boolean getOnly ) { } public boolean isGetNameAddressOnly ( ) { } public void onServiceConnected ( ComponentName className , IBinder service ) { } public void onServiceDisconnected ( ComponentName className ) { } } private BluetoothManagerService . BluetoothServiceConnection mConnection = new BluetoothManagerService . BluetoothServiceConnection ( ) ; private class BluetoothHandler extends Handler { public BluetoothHandler ( Looper looper ) { } @ Override public void handleMessage ( Message msg ) { } } private void handleEnable ( boolean quietMode ) { } boolean doBind ( Intent intent , ServiceConnection conn , int flags , UserHandle user ) { } private void handleDisable ( ) { } private boolean checkIfCallerIsForegroundUser ( ) { } private void bluetoothStateChangeHandler ( int prevState , int newState ) { if ( prevState != newState ) { if ( ( newState == ( BluetoothAdapter . STATE_ON ) ) || ( newState == ( BluetoothAdapter . STATE_OFF ) ) ) { boolean isUp = newState == ( BluetoothAdapter . STATE_ON ) ; sendBluetoothStateCallback ( isUp ) ; if ( isUp ) { if ( mContext . getPackageManager ( ) . hasSystemFeature ( FEATURE_BLUETOOTH_LE ) ) { Intent i = new Intent ( IBluetoothGatt . class . getName ( ) ) ; doBind ( i , mConnection , ( ( Context . BIND_AUTO_CREATE ) | ( Context . BIND_IMPORTANT ) ) , CURRENT ) ; Intent iqc = new Intent ( IQBluetooth . class . getName ( ) ) ; doBind ( iqc , mConnection , ( ( Context . BIND_AUTO_CREATE ) | ( Context . BIND_IMPORTANT ) ) , CURRENT ) ; } } else { if ( ( ! isUp ) && ( canUnbindBluetoothService ( ) ) ) { sendBluetoothServiceDownCallback ( ) ; sendQBluetoothServiceDownCallback ( ) ; unbindAndFinish ( ) ; } } } Intent intent = new Intent ( BluetoothAdapter . ACTION_STATE_CHANGED ) ; intent . putExtra ( EXTRA_PREVIOUS_STATE , prevState ) ; intent . putExtra ( EXTRA_STATE , newState ) ; intent . addFlags ( FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT ) ; intent . addFlags ( FLAG_RECEIVER_FOREGROUND ) ; if ( BluetoothManagerService . DBG ) Log . d ( BluetoothManagerService . TAG , ( ( ( "Bluetooth<seq2seq4repair_space>State<seq2seq4repair_space>Change<seq2seq4repair_space>Intent:<seq2seq4repair_space>" + prevState ) + "<seq2seq4repair_space>-><seq2seq4repair_space>" ) + newState ) ) ; <START_BUG> mContext . sendBroadcastAsUser ( intent , ALL , BluetoothManagerService . BLUETOOTH_PERM ) ; <END_BUG> } } private boolean waitForOnOff ( boolean on , boolean off ) { } private void sendDisableMsg ( ) { } private void sendEnableMsg ( boolean quietMode ) { } private boolean canUnbindBluetoothService ( ) { } private void recoverBluetoothServiceFromError ( ) { } }

Fix:
mContext . sendBroadcastAsUser ( intent , CURRENT , BluetoothManagerService . BLUETOOTH_PERM ) ;