Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regarding "strange NPE" - it's not strange at all. The reason you get NPE at this line</p> <pre><code>socket = mmServerSocket.accept(); //THIS LINE CAUSES NPE </code></pre> <p>is pretty obvious - variable <em>mmServerSocket</em> equals NULL, and this is the only possible reason to get NPE at this line of code. The reason why <em>mmServerSocket</em> equals NULL is simple too - look at a constructor of your class:</p> <pre><code>public AcceptThread() { BluetoothServerSocket tmp = null; try { tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); } catch (IOException e) { Log.e(TAG, "listen() failed", e); } mmServerSocket = tmp; } </code></pre> <p>You are calling method <code>listenUsingRfcommWithServiceRecord</code> which throws IOException, you catching this exception, as we can clearly see in logcat. Next, <code>tmp</code> variable stays initialized as NULL, same as <code>mmServerSocket</code>, which leads as to NPE mentioned above.</p> <p>The reason why <code>listenUsingRfcommWithServiceRecord</code> is throwing IOException - because bluetooth is turned off, as you mention in your question. Android documentation says nothing about your assumption that this method should automatically turn on bluetooth if it's turned off. I think you should manually check if bluetooth is turned off and turn it on manually, before calling <code>listenUsingRfcommWithServiceRecord</code>.</p> <p>You can find how to check is bluetooth turned on/off and turn it on <a href="https://stackoverflow.com/questions/6546083/how-to-turn-on-bluetooth-on-button-click">here</a> and <a href="https://stackoverflow.com/questions/1975655/enable-android-bluetooth-from-documentation">here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload