Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Bluetooth Fails to Pair
    primarykey
    data
    text
    <p>I am having a problem getting my devices to pair in Android. If I go into the settings and pair them manually I can get them to connect using the following code:</p> <p><strong><em>Server</em></strong></p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.connect); Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_ENABLE_BT) { if(resultCode == RESULT_CANCELED) { new AlertDialog.Builder(this) .setTitle(R.string.error) .setMessage(R.string.bluetooth_unavailable) .setPositiveButton(android.R.string.ok, AndroidUtils.DismissListener) .create() .show(); } else { mServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("Moo Productions Bluetooth Server", mUUID); mState = State.ACCEPTING; BluetoothSocket socket = mServerSocket.accept(); mServerSocket.close(); connected(socket); } } } </code></pre> <p><strong><em>Client</em></strong></p> <pre><code>Set&lt;BluetoothDevice&gt; pairedDevices = mAdapter.getBondedDevices(); BluetoothSocket socket = null; // Search the list of paired devices for the right one for(BluetoothDevice device : pairedDevices) { try { mState = State.SEARCHING; socket = device.createRfcommSocketToServiceRecord(mUUID); mState = State.CONNECTING; socket.connect(); connected(socket); break; } catch (IOException e) { socket = null; continue; } } </code></pre> <p>But if the devices hadn't already been paired it gets out of the foreach without connecting to a valid socket. In that case I start discovering.</p> <pre><code>// If that didn't work, discover if(socket == null) { mState = State.SEARCHING; mReceiver = new SocketReceiver(); mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); mAdapter.startDiscovery(); } // ... Later ... private class SocketReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { try { // Get the device and try to open a socket BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(mUUID); mState = State.CONNECTING; socket.connect(); // This is our boy, so stop looking mAdapter.cancelDiscovery(); mContext.unregisterReceiver(mReceiver); connected(socket); } catch (IOException ioe) { ioe.printStackTrace(); } } } } </code></pre> <p>But it will never find the other device. I never get a pairing dialog and when I step through I see that it discovers the correct device, but it fails to connect with this exception <code>java.io.IOException: Service discovery failed</code>. Any ideas as to what I'm missing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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