This app connects Bluetooth printer from 2 activities . First activity works correctly. BT printer reference send to second activity by below mentioned code.
(intent.putExtra("device",mmDevice);) Received by second activity as device = getIntent().getExtras().getParcelable("device"); This one works perfectly in Debug mode but not in Normal run mode. Error at : mmSocket.connect() Activity 2; java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted). public class MainActivity extends Activity { Button button1,button2; OutputStream mOutputStream; BluetoothAdapter mBluetoothAdapter; BluetoothSocket mmSocket; BluetoothDevice mmDevice; OutputStream mmOutputStream; InputStream mmInputStream; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); button2 = (Button)findViewById(R.id.button2); mOutputStream = findBT(); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { sendData("Activity 1",mOutputStream); } catch(Exception ex){ ex.printStackTrace(); } } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Intent intent = new Intent(getApplicationContext(),MainActivity22Activity.class); intent.putExtra("device",mmDevice); startActivity(intent); } catch(Exception ex){ ex.printStackTrace(); } } }); } protected void onPause() { super.onPause(); try { mmOutputStream.close(); mmSocket.close(); } catch(Exception ex){ ex.printStackTrace(); } } void sendData(String msg,OutputStream outputStream) throws IOException { try { msg += "\n"; outputStream.write(msg.getBytes()); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public OutputStream findBT() { try { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Log.e("service", "No bluetooth adapter available"); } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetooth = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { // BMV2 is the name of the bluetooth printer device if (device.getName().equals("BMV2")) { mmDevice = device; break; } } } Log.e("service ",mmDevice.toString()); openBT(); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return mmOutputStream; } // Tries to open a connection to the bluetooth printer device void openBT() throws IOException { Log.e("service","Open BT"); try { // Standard SerialPortService ID UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); Log.e("BT","Bluetooth Opened"); } catch (NullPointerException e) { Log.e("Null pont err","error"); e.printStackTrace(); } catch (Exception e) { Log.e("service","socket error"); e.printStackTrace(); } } } public class MainActivity22Activity extends Activity { Button button ; OutputStream mOutputStream; BluetoothAdapter mBluetoothAdapter; BluetoothSocket mmSocket; BluetoothDevice mmDevice; OutputStream mmOutputStream; InputStream mmInputStream; BluetoothDevice device; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity22); button = (Button)findViewById(R.id.button); device = getIntent().getExtras().getParcelable("device"); try { openBT(device); } catch(Exception ex){ ex.printStackTrace(); } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { sendData("activity 2",mmOutputStream); } catch(Exception ex){ ex.printStackTrace(); } } }); } @Override protected void onResume() { super.onResume(); } protected void onPause() { super.onPause(); } void openBT(BluetoothDevice device) throws IOException { Log.e("service","Open BT"); try { // Standard SerialPortService ID UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = device.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); Log.e("BT","Bluetooth Opened"); } catch (NullPointerException e) { Log.e("Null pont err","error"); e.printStackTrace(); } catch (Exception e) { Log.e("service","socket error"); e.printStackTrace(); } } void sendData(String msg,OutputStream outputStream) throws IOException { try { Log.e("service","Printing going on"); msg += "\n"; Log.e("service",msg); outputStream.write(msg.getBytes()); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } Enter code here... -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/ac47e369-94a7-43b8-8a1c-5c390e6d2bc7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.