Android bluetooth not asking to enter PIN whille connecting to external hardware device
<p>I am working on an android app in which i have implemented Bluetooth functionality.My app is working fine its detecting active bluetooth devices and connecting with the another cell phones properly.</p>

<p>But the problem is that when i try to connect to external hardware device it din't ask to enter a PIN
or not getting connected to external hardware device.</p>

<p>Here is my code:</p>

<p>This code is used to send connection request to another bluetooth device</p>

<pre><code>protected void connect(BluetoothDevice device) {
        try {
            ConfigClass.isFirstBluetoothSignal=true;
            Log.d(TAG,"connect bluetooth");
            // Create a Socket connection: need the server's UUID number of
            // registered
            Method m = device.getClass().getMethod("createRfcommSocket",
                    new Class[] { int.class });
            socket = (BluetoothSocket) m.invoke(device, 1);
//          socket=device.createRfcommSocketToServiceRecord(MY_UUID_INSECURE);
            socket.connect();
            Log.d(TAG, "&gt;&gt;Client connectted");
            inputStream = socket.getInputStream();
            socket.getOutputStream();
            int read = -1;
            final byte[] bytes = new byte[1024];
            while (true) {
                synchronized (obj1) {
                    read = inputStream.read(bytes);
                    Log.d(TAG, "read:" + read);
                    if (read &gt; 0) {
                        ConfigClass.isFirstBluetoothSignal=false;
                        final int count = read;
                        String str = SamplesUtils.byteToHex(bytes, count);
                        //                      Log.d(TAG, "test1:" + str);
                        String hex = hexString.toString();
                        if (hex == "") {
                            hexString.append("&lt;--");
                        } else {
                            if (hex.lastIndexOf("&lt;--") &lt; hex.lastIndexOf("--&gt;")) {
                                hexString.append("\n&lt;--");
                            }
                        }
                        hexString.append(str);
                        hex = hexString.toString();
                        //                      Log.d(TAG, "test2:" + hex);
                        if (hex.length() &gt; maxlength) {
                            try {
                                hex = hex.substring(hex.length() - maxlength,
                                        hex.length());
                                hex = hex.substring(hex.indexOf(" "));
                                hex = "&lt;--" + hex;
                                hexString = new StringBuffer();
                                hexString.append(hex);
                            }catch (NullPointerException e) {
                                e.printStackTrace();
                            } 
                            catch (Exception e) {
                                e.printStackTrace();
                                Log.e(TAG, "e", e);
                            }
                        }
                        _handler.post(new Runnable() {
                            public void run() {
                                //Toast.makeText(getApplicationContext(), hex, Toast.LENGTH_LONG);
                            }
                        });
                    }
                }
            }

        } catch (Exception e) {
            ConfigClass.isFirstBluetoothSignal=false;
            Log.e(TAG, "&gt;&gt;", e);
            Toast.makeText(getBaseContext(),
                    getResources().getString(R.string.ioexception),
                    Toast.LENGTH_SHORT).show();
            return;
        } finally {
            if (socket != null) {
                try {
                    Log.d(TAG, "&gt;&gt;Client Socket Close");
                    socket.close();
                    socket = null;
                    // this.finish();
                    return;
                } catch (IOException e) {
                    Log.e(TAG, "&gt;&gt;", e);
                }
            }
        }
    }
</code></pre>

<p>The below is the thread class which i implemented to get incoming data from external bluetooth device</p>

<pre><code>public class AcceptThread extends Thread{
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;
        private String mSocketType;
        private BluetoothAdapter mAdapter=BluetoothAdapter.getDefaultAdapter();

        public AcceptThread(boolean secure) {
            BluetoothServerSocket tmp = null;
            mSocketType = secure ? "Secure":"Insecure";

            // Create a new listening server socket
            try {
                if (secure) {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_SECURE);
                } else {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
            }
            mmServerSocket = tmp;
        }

        public void run() {

            BluetoothSocket socket = null;

            // Listen to the server socket if we're not connected
            while (true) {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    socket = mmServerSocket.accept();
                    InputStream ins=socket.getInputStream();

                    BufferedReader r = new BufferedReader(new InputStreamReader(ins));
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null) {
                        total.append(line);
                    }

                } catch (IOException e) {
                    Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
                    break;
                }

                // If a connection was accepted
                if (socket != null) {
                    synchronized (SearchDeviceActivity.this) {

                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }

        }
    }
</code></pre>
