1

I am creating an application which can send data to a Bluetooth device. I used the following code to create and connect socket:

package com.example.bluetooth; import java.io.IOException; import java.util.UUID; import android.os.Bundle; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.util.Log; import android.view.Menu; import android.widget.Toast; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice bd = ba.getRemoteDevice("20:13:10:15:39:84"); Toast.makeText(getApplicationContext(), bd.getName(), Toast.LENGTH_SHORT).show(); BluetoothSocket bs = null; try{ bs = bd.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch(IOException io){ Toast.makeText(getApplicationContext(), "Socket Create : " + io.toString() , Toast.LENGTH_SHORT).show(); } try{ ba.cancelDiscovery(); bs.connect(); } catch(IOException io){ Log.e("Socket Connect", io.toString()); Toast.makeText(getApplicationContext(), "Socket Connect : " + io.toString() , Toast.LENGTH_LONG).show(); } } } 

My problem is that socket is not being connected. The message displayed is "java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted)."

I am using android 4.2 Lenovo Device.

The Bluetooth Module used is HC-05 and microcontroller is Arduino-Uno.

I have have referred to similar posts but none could solve my problem.

Thanks in Advance

3 Answers 3

4

I have used this piece of code to make my connectivity to Bluetooth device(i.e. Bluetooth printer) stable. Now it connect 9.9 times out of 10. If still some error occurred i reset my Bluetooth programmatically again call this piece of code then it connect 10 times out of 10.

public boolean connectToPrinter(String printerName) throws IOException { BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); BluetoothDevice device = getPrinterByName(printerName); if (bluetoothSocket != null) { bluetoothSocket.close(); } try { Method m=device.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); bluetoothSocket= (BluetoothSocket) m.invoke(device, 1); } catch (Exception e) { e.printStackTrace(); } if (bluetoothSocket == null) return false; bluetoothSocket.connect(); return true; } 

here is the code of getPrinterByName():

private BluetoothDevice getPrinterByName(String printerName) { Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices(); for (BluetoothDevice device : pairedDevices) { Log.e("","device name: "+device.getName()); if (device.getName() == null) continue; if (device.getName().contains(printerName)) { remoteDevice = device; // pairPrinter(printerName); return remoteDevice; } } return null; } 
Sign up to request clarification or add additional context in comments.

2 Comments

I have edited my answer.. I have pasted all the solution of the link. if you still has any problem then please feel free to ask. Please Up Vote if like my post. Thanks.
Yep, that now looks fine.
3

I first restarted my tablet and then tried to connect Bluetooth Socket using my code. I just had to use:

public void onStop(){ super.onStop(); mSocket.close(); mOutputStream.close(); } 

at the end.

It worked!

The problem was that I never tried to close socket at end.

4 Comments

'The problem was that I never tried to close socket at end.'.Do you mean you handn't done mSocket.close() or mOutputStream.close() ?
Yes, exactly that was my problem.
Uhm I don't understand how that could solve your problem. I guess with mSocket.close(); you meant bs.close(), but for what do you need the mOutputSteam? Or is there a part of your code you haven't included above?
I believe what he was trying to say is that when the BT connection is no longer needed the application MUST close it. If the BT socket isn't closed for any reason like an app crash or an app restart while debugging, the BT stack will keep the connection open and a second connection to the same device is usually not allowed. The effect of this behaviour is that the BT connection will only work the "first time". Restart of the BT device or Android BT will "fix" this. I can reproduce this on a device with Android 4.0.4 while a Nexus 5 with Android 6.0.1 seems to handle this correctly.
0

Pair the arduino bluetooth from your phone settings first. Then try to connect to the bluetooth from your app. Try below code:

 private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBluetoothDevice; private BluetoothSocket mBluetoothSocket; private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); try{ if(mBluetoothAdapter == null){ Log.d("bluetooth:", "device does not support bluetooth"); } if(!mBluetoothAdapter.isEnabled()){ Intent enableBt = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); enableBt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(enableBt); } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } 

For connection :

 mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("xx:xx:xx:xx:xx:xx"); try { mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid); mBluetoothSocket.connect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

1 Comment

I first paired device and tried to connect then too it did not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.