0

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!

Here is the relevant code:

public void connect() {
// I know I should be using an intent here...
while (!bluetoothAdapter.isEnabled()) {
  bluetoothAdapter.enable();
}

pairedDevices = bluetoothAdapter.getBondedDevices();

if (pairedDevices.size() > 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();
}

}

UPDATE:

I now also tried to replace this part:

if (pairedDevices.size() > 0) {
  for (BluetoothDevice device : pairedDevices) {
    if (device.getName().equals("FireFly-8AAD")) {
      mDevice = device;
    }
  }
}

with this:

if (pairedDevices.size() > 0) {
  for (BluetoothDevice device : pairedDevices) {
    if (device.getName().equals("FireFly-8AAD")) {
      mDevice = bluetoothAdapter.getRemoteDevice(device.getAddress());
    }
  }
}

as suggested here: Android Bluetooth accept() / connect() with already paired devices

But it still doesn't work...

UPDATE 2:

I replaced

device.getName().equals("FireFly-8AAD")

with

device.getAddress().equals("MAC-Address here")

Still same problem.

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.

UPDATE 3:

I thought it might help if I also provide the code that establishes a connection with which I can send/receive data:

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);
    }
  }
};
Community
  • 1
  • 1
Huxley
  • 1
  • 3

1 Answers1

1

With bluesmirf paired early, I do "cancelDiscovery()" before connect(), like this:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = adapter.getRemoteDevice(macAddr);
if (adapter.isDiscovering()) 
     adapter.cancelDiscovery();
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });  
btSocket = (BluetoothSocket) m.invoke(device, 1);

but sometimes, the bluetooth module remains connected after closing stream and socket, and the green led remains active after tablet power off too. It is very strange, I think it is a hardware problem.