title: Android 4.0+ Bluetooth connection error to an embedded device: "Permission Denied"
body: <p>I have the following setup:</p>
  
  <p>An Android device uses a 'Client' socket to connect to a remote embedded device, The Android application uses the following code snippet to connect to the embedded device.</p>
  
  <p>On the embedded device uses MindTree BT stack, where server serial socket is prepared according to some properties in the device, which the Android application is familiar with, the connection defined on the embedded device, is not secured!!</p>
  
  <p>The combination of both applications works on:</p>
  
  <ul>
  <li><strong>2 LG phones</strong> different models (version code &lt; 10 <strong>uses the "Normal method"</strong>)</li>
  <li><strong>2 HTC's</strong> different models (version code &lt; 10 <strong>uses the "Workaround method"</strong>)</li>
  <li>Pantech Tablet (version code &lt; 13 <strong>uses the "Workaround method"</strong>)</li>
  </ul>
  
  <p>Today, I've tried the application on Samsung S3, Motorola MB886, and a Nexus 7... 
  All resulted in a "Permission Denied" when calling to <strong>socket.connect()</strong>... (I have the proper permissions in the manifest, otherwise it would not work on the other devices.)</p>
  
  <p>All the new devices I've tested on are version code > 4.0, so I'm wondering:</p>
  
  <p>Does anyone know about any changes in the API?
  Perhaps Android 4.0+ forces security? </p>
  
  <p>It seem that the error occur in the Bonding state, since I can see on the embedded program logs...</p>
  
  <p>Any insights? </p>
  
  <p>The code:</p>
  
  <pre><code>public final synchronized int connectToDevice(int connectingMethod)
          throws BluetoohConnectionException {
      if (socket != null)
          throw new BadImplementationException("Error socket is not null!!");
      connecting = true;
      logInfo("+---+ Connecting to device...");
  
      try {
          lastException = null;
          lastPacket = null;
          if (connectingMethod == BluetoothModule.BT_StandardConnection
                  || connectingMethod == BluetoothModule.BT_ConnectionTBD)
              try {
  
                  socket = fetchBT_Socket_Normal();
                  connectToSocket(socket);
                  listenForIncomingSPP_Packets();
                  onConnetionEstablished();
                  return BluetoothModule.BT_StandardConnection;
              } catch (BluetoohConnectionException e) {
                  socket = null;
                  if (connectingMethod == BluetoothModule.BT_StandardConnection) {
                      throw e;
                  }
                  logWarning("Error creating socket!", e);
              }
          if (connectingMethod == BluetoothModule.BT_ReflectiveConnection
                  || connectingMethod == BluetoothModule.BT_ConnectionTBD)
              try {
                  socket = fetchBT_Socket_Reflection(1);
                  connectToSocket(socket);
                  listenForIncomingSPP_Packets();
                  onConnetionEstablished();
                  return BluetoothModule.BT_ReflectiveConnection;
              } catch (BluetoohConnectionException e) {
                  socket = null;
                  if (connectingMethod == BluetoothModule.BT_ReflectiveConnection) {
                      throw e;
                  }
                  logWarning("Error creating socket!", e);
              }
          throw new BluetoohConnectionException("Error creating RFcomm socket for BT Device:" + this
                  + "\n BAD connectingMethod==" + connectingMethod);
      } finally {
          connecting = false;
      }
  }
  
  protected void onConnetionEstablished() {
      logInfo("+---+ Connection established");
  }
  
  private synchronized void listenForIncomingSPP_Packets() {
      if (socketListeningThread != null)
          throw new BadImplementationException("Already lisening on Socket for BT Device" + this);
      logInfo("+---+ Listening for incoming packets");
      socketListeningThread = new Thread(socketListener, "Packet Listener - " + bluetoothDevice.getName());
      socketListeningThread.start();
  }
  
  private BluetoothSocket fetchBT_Socket_Normal()
          throws BluetoohConnectionException {
      try {
          logInfo("+---+ Fetching BT RFcomm Socket standard for UUID: " + uuid + "...");
          return bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
      } catch (Exception e) {
          throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
      }
  }
  
  private BluetoothSocket fetchBT_Socket_Reflection(int connectionIndex)
          throws BluetoohConnectionException {
      Method m;
      try {
          logInfo("+---+ Fetching BT RFcomm Socket workaround index " + connectionIndex + "...");
          m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
          return (BluetoothSocket) m.invoke(bluetoothDevice, connectionIndex);
      } catch (Exception e) {
          throw new BluetoohConnectionException("Error Fetching BT RFcomm Socket!", e);
      }
  }
  
  private void connectToSocket(BluetoothSocket socket)
          throws BluetoohConnectionException {
      try {
          logInfo("+---+ Connecting to socket...");
          socket.connect();
          logInfo("+---+ Connected to socket");
      } catch (IOException e) {
          try {
              socket.close();
          } catch (IOException e1) {
              logError("Error while closing socket", e1);
          } finally {
              socket = null;
          }
          throw new BluetoohConnectionException("Error connecting to socket with Device" + this, e);
      }
  }
  </code></pre>