Note that there are some explanatory texts on larger screens.

plurals
  1. POBluetooth connection (Android/Bluesmirf) only works correctly if not paired already
    primarykey
    data
    text
    <p>I am trying to communicate via Bluetooth between an Android tablet and an Arduino with a BlueSmirf Bluetooth dongle. In my Android code, I first check the list of already paired devices for the one I am looking for. If it is not in there, I start discovering. Both possibilities work to the point where the status LED on the BlueSmirf turns green to indicate a successfull connection. But only if the device was not paired before I started the app, I am also able to send/receive data via Bluetooth. If the device was paired before, the connection is established more reliably and also faster, but no data can be send or received. Do you have any idea why that could be? Many thanks in advance!</p> <p>Here is the relevant code:</p> <pre><code>public void connect() { // I know I should be using an intent here... while (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() &gt; 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().equals("FireFly-8AAD")) { mDevice = device; } } } if (mDevice == null) { // BroadcastReceiver registered with IntentFilter(BluetoothDevice.ACTION_FOUND) registerReceiver(mReceiver, find); if (!bluetoothAdapter.isDiscovering ()) { bluetoothAdapter.startDiscovery(); } } while (mDevice == null) { // Wait for BroadcastReceiver to find the device and // connect } if (mDevice != null) { // Create socket connection in a new thread Connect connection = new Connect(); new Thread(connection).start(); } while (mSocket == null) { // Wait for successfull socket connection } if (mSocket != null) { // Get input/ouputstream Communication communicate = new Communication(); new Thread(communicate).start(); } </code></pre> <p>}</p> <p>UPDATE:</p> <p>I now also tried to replace this part:</p> <pre><code>if (pairedDevices.size() &gt; 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().equals("FireFly-8AAD")) { mDevice = device; } } } </code></pre> <p>with this:</p> <pre><code>if (pairedDevices.size() &gt; 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().equals("FireFly-8AAD")) { mDevice = bluetoothAdapter.getRemoteDevice(device.getAddress()); } } } </code></pre> <p>as suggested here: <a href="https://stackoverflow.com/questions/9282867/android-bluetooth-accept-connect-with-already-paired-devices">Android Bluetooth accept() / connect() with already paired devices</a></p> <p>But it still doesn't work...</p> <p>UPDATE 2:</p> <p>I replaced</p> <pre><code>device.getName().equals("FireFly-8AAD") </code></pre> <p>with</p> <pre><code>device.getAddress().equals("MAC-Address here") </code></pre> <p>Still same problem.</p> <p>I also tried running the whole connection process in a new thread opposed to only running socket.connect() and socket.getInput/OuputStream in their own, but this doesn't help either.</p> <p>UPDATE 3:</p> <p>I thought it might help if I also provide the code that establishes a connection with which I can send/receive data:</p> <pre><code>private class mBroadcastReceiver extends BroadcastReceiver { private String discoveredDeviceName; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); } if (discoveredDeviceName.equals("FireFly-8AAD")) { bt_device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); } } }; </code></pre>
    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