Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I remember correctly the HTC phones had or [have] an issue at a certain API level (maybe 2.1 and below?). The resolution is reflection. </p> <p><strong>Reference</strong></p> <p><a href="https://stackoverflow.com/questions/3031796/disconnect-a-bluetooth-socket-in-android">Disconnect a bluetooth socket in Android</a></p> <p><a href="https://stackoverflow.com/questions/3397071/android-bluetooth-service-discovery-failed-exception">Service discovery failed exception using Bluetooth on Android </a></p> <p><strong>Solution</strong></p> <p>Instead of using</p> <pre><code>tmp = device.createRfcommSocketToServiceRecord(MY_UUID); </code></pre> <p>use</p> <pre><code>Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); tmp = (BluetoothSocket) m.invoke(device, 1); </code></pre> <p>to get your BluetoothSocket on certain HTC phones using a certain API level.</p> <p><strong>Solution Expanded</strong></p> <p>I recently had an application where I had to account for this, and I did not like using this on non-HTC phones, so I had a conditional to check for HTC, if true then use reflection, otherwise dont.</p> <pre><code>public BTConnectThread(BluetoothDevice device) { mmDevice = device; BluetoothSocket tmp = null; // Get a BluetoothSocket for a connection with the given BluetoothDevice if (isAnHTCDevice()) { try { Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); } catch (Exception e) { Log.e(BCTAG, "Error at HTC/createRfcommSocket: " + e); e.printStackTrace(); handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating htc socket: " + e)); } } else { try { UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (Exception e) { Log.e(BCTAG, "Error at createRfcommSocketToServiceRecord: " + e); e.printStackTrace(); handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating socket: " + e)); } } mmSocket = tmp; } </code></pre> <p>isAnHTCDevice():</p> <pre><code>public boolean isAnHTCDevice() { String manufacturer = android.os.Build.MANUFACTURER; if (manufacturer.toLowerCase().contains("htc")) return true; else return false; } </code></pre>
 

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