BluetoothAdapter.getDefaultAdapter() throwing RuntimeException while not in Activity

前端 未结 5 424
天涯浪人
天涯浪人 2021-01-04 08:39

When I\'m trying to get default bluetooth adapter while i\'m NOT in Activity, but in TimerTask (created inside Service) by using:

B         


        
相关标签:
5条回答
  • 2021-01-04 09:09

    Beware of a gotcha that exists in 2.3.x, but which has been fixed in 4.x: if you call BluetoothAdapter.getDefaultAdapter() on any thread other than the main application thread, that thread must call Looper.prepare() and also subsequently Looper.loop().

    Failing to do so will cause at least one problem that I ran into: accept() will succeed the first time you try to connect, but then not succeed on successive attempts, even after using close() on the ServerSocket.

    This happens because in the older implementation of BluetoothAdapter, the cleanup of the SDP entry occurs by way of a message posted to a handler created on the thread where getDefaultAdapter() is called.

    0 讨论(0)
  • 2021-01-04 09:12

    This appears to be a bug in Android and still exists in Android 4.0 (Ice Cream Sandwich)

    To workaround this and be able to call BluetoothAdapter.getDefaultAdapter() from a worker thread (e.g. AsyncTask), all you have to do is call BluetoothAdapter.getDefaultAdapter() once on the main UI thread (e.g. inside the onCreate() of your current activity).

    The RuntimeException is only thrown during initialization, and BluetoothAdapter.getDefaultAdapter() only initializes the first time you call it. Subsequent calls to it will succeed, even in background threads.

    0 讨论(0)
  • 2021-01-04 09:25

    Calling BluetoothAdapter.getDefaultAdapter() in the UI thread works, but is not very practical. I have tried the workaround with a fake Activity, but since I hate such workarounds, I decided to READ what the error message really says and it is nothing more than that the thread didn't call Looper.prepare().

    So calling Looper.prepare() just before calling BluetoothAdapter.getDefaultAdapter() should solve the problem anywhere, not just in a UI thread.

    Works fine for me so far.

    0 讨论(0)
  • 2021-01-04 09:26

    Hi Kocus there is no any method called getDefault() in BluetoothAdapter calss . It should be BluetoothAdapter.getDefaultAdapter();

    follow this link for more information.

    0 讨论(0)
  • Not sure how correct it is, but I added this wrapper function:

    static boolean m_calledLooperAlready = false;
    
    BluetoothAdapter getDefaultBluetoothAdapter() {
        if ( !m_calledLooperAlready ) {
            try  {
                android.os.Looper.prepare();
            } catch ( RuntimeException e ) { e.printStackTrace(); }
            m_calledLooperAlready = true;
        }
        return BluetoothAdapter.getDefaultAdapter();
    }
    

    ... and replaced all occurrences of BluetoothAdapter.getDefaultAdapter() with getDefaultBluetoothAdapter(). This works ok for me on: 2.2.1, 2.3.3, 4.0.4, 4.3

    0 讨论(0)
提交回复
热议问题