title: Should I leave the bluetooth reflection hack in production code?
body: <p>I'm working in a project where I need to connect via bluetooth to a printer. The printer manufacturer states that only android phones having SPP (Serial Port Profile) are going to be able to connect with the printer.</p>
  
  <p>This is how I opened the connection initially:<br></p>
  
  <pre><code>        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //SPP long UUID
          BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
  </code></pre>
  
  <p>Using UUIDs is the only way to open RFCOMM connections using Android public API as of JellyBean. Having worked before with SPP connections in BlackBerry and JavaME, where UUIDs are not required, I found this a bit odd. UUIDs are about service discovery, that is, using SDP to query about the services present in the device. I don't really need to initiate a discovery, since I have my printer paired in advance, and I know it supports SPP. However that is exactly what the <code>BluetoothDevice.createRfcommSocketToServiceRecord</code> method and the insecure version do. This is the SPP stack, where we can see how SDP is a different protocol at the same layer, and thus it should be possible to use RFCOMM without initiating a discovery first:</p>
  
  <pre><code>        -----------------------------------
          |        My Application           |
          ----------------------------------- 
          |     Serial Port Emulation       |
          |          or other API           |
          -----------------------------------
          |      RFCOMM          |    SDP   |
          -----------------------------------
          |  LMP   |   L2PCAP               |
          -----------------------------------
          |           Baseband              |
          -----------------------------------
  </code></pre>
  
  <p>I started testing my app in a few old HTC devices without problems. Later, testing on Samsung phones, several devices were unable to open a connection. <strike>These phones allegedly do not support SPP profile, according to both manufacturer and 3rd party specs</strike>(<em>EDIT: 3rd party specs list SPP as supported, and the manufacturer specs are not accurate enough</em>). An IOException (Service Discovery failed) was thrown, and I followed the approach shown in this question:</p>
  
  <p><a href="https://stackoverflow.com/q/3397071/813951">Service discovery failed exception using Bluetooth on Android </a></p>
  
  <p>The solution proposed there is to use a reflection hack, as follows:</p>
  
  <pre><code>        Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
          BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);
  </code></pre>
  
  <p>The hack worked for me. Amazingly, this method in <code>BluetoothDevice</code> class is public, but it is removed from the API by means of the @hide annotation. This is the source code as of JellyBean:</p>
  
  <pre><code>        /**
           * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
           * outgoing connection to this remote device on given channel.
           * &lt;p&gt;The remote device will be authenticated and communication on this
           * socket will be encrypted.
           * &lt;p&gt; Use this socket only if an authenticated socket link is possible.
           * Authentication refers to the authentication of the link key to
           * prevent man-in-the-middle type of attacks.
           * For example, for Bluetooth 2.1 devices, if any of the devices does not
           * have an input and output capability or just has the ability to
           * display a numeric key, a secure socket connection is not possible.
           * In such a case, use {#link createInsecureRfcommSocket}.
           * For more details, refer to the Security Model section 5.2 (vol 3) of
           * Bluetooth Core Specification version 2.1 + EDR.
           * &lt;p&gt;Use {@link BluetoothSocket#connect} to initiate the outgoing
           * connection.
           * &lt;p&gt;Valid RFCOMM channels are in range 1 to 30.
           * &lt;p&gt;Requires {@link android.Manifest.permission#BLUETOOTH}
           *
           * @param channel RFCOMM channel to connect to
           * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
           * @throws IOException on error, for example Bluetooth not available, or
           *                     insufficient permissions
           * @hide
           */
          public BluetoothSocket createRfcommSocket(int channel) throws IOException {
              return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
                      null);
          }
  </code></pre>
  
  <p>I can't understand why a public method is removed from the API in this manner. But letting this appart, both this method and the officially supported one using UUIDs are thin envelopes calling the same <code>BluetoothSocket</code> constructor with different parameters:</p>
  
  <pre><code>        public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
              return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
                      new ParcelUuid(uuid));
          }
  </code></pre>
  
  <p>Digging a bit more in the sources I realized that both open a RFCOMM connection, but the UUID method initiates a discovery and the hidden one not.</p>
  
  <p>All in all, the reflection hacks work flawlessly in every device I've tested it, from OS 2.2 to 4.1. <em>EDIT: The "faulty" devices do support SPP, it is just that their custom implementation of BT stack is messing up with the discovery process; there are also other bugs like the the one showing pairing dialog for already paired devices in ICS. Calling this hidden API using reflection enables a workaround to all these bugs or different behaviour introduced by the different manufacturers</em>.</p>
  
  <p><strong>Should I keep the hack in production code? Is there a way to achieve the same with the public API?</strong></p>
  
  <p>Thanks in advance.</p>