Ich weiß die Frage ist etwas dumm. Aber wie finde ich herraus, um welche exception es sich handelt.
Beiträge von gammel
-
-
ich habe jetzt nur die Reihenfolge der funktionen geändert. Dies hat zur Folge dass die App startet und fragt, ob BT angeschaltet werden soll, was auch geschieht. Allerdings wird nicht nach anderen BT-Geräten gesuht und folglich ist kein Verbindungsaufbau möglich.
Liegt das eventl immer noch an der Reihenfolge und ist der Code an sich falsch????LogCat:
09-08 11:04:31.020 5971-5971/com.example.ziel W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40e14438)
09-08 11:05:40.680 7627-7627/com.example.ziel I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010_msm8625_JB_REL_2.0.3_Merge_release_AU (Merge)
Build Date: 10/26/12 Fri
Local Branch:
Remote Branch: quic/jb_rel_2.0.3
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010 + NOTHING -
Code
Alles anzeigenpackage com.example.ziel; import.. public class Main_Activity extends Activity implements OnItemClickListener { private Handler handler; String value; Array parts; ArrayAdapter<String> listAdapter; ListView listView; BluetoothAdapter btAdapter; Set<BluetoothDevice> devicesArray; ArrayList<String> pairedDevices; ArrayList<BluetoothDevice> devices; public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); protected static final int SUCCESS_CONNECT = 0; protected static final int MESSAGE_READ = 1; IntentFilter filter; BroadcastReceiver receiver; String tag = "debugging"; String ring= "525,32.565,."; TextView textview1; TextView textview2; TextView textview3; Button button1; public void aufteilen() { String[] parts = ring.split("\\."); textview2.setText("Temp2 "+parts[0]); textview3.setText("Temp3 "+parts[1]); } Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub Log.i(tag, "in handler"); super.handleMessage(msg); switch(msg.what){ case SUCCESS_CONNECT: // aktionen ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj); Toast.makeText(getApplicationContext(), "CONNECT", 0).show(); String s = "successfully connected"; connectedThread.write(s.getBytes()); Log.i(tag, "connected"); break; case MESSAGE_READ: // HIER byte[] readBuf = (byte[])msg.obj; String string = new String(readBuf); Toast.makeText(getApplicationContext(), string, 0).show(); break; } } }; @Override public void onCreate(Bundle savedInstanceState) { textview2 = (TextView) findViewById(R.id.textView2); textview3 = (TextView) findViewById(R.id.textView3); final Button button1 = (Button) findViewById(R.id.button); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ring = "995,32.757,56"; aufteilen(); }}); super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); if(btAdapter==null){ Toast.makeText(getApplicationContext(), "No bluetooth detected", 0).show(); finish(); } else{ if(!btAdapter.isEnabled()){ turnOnBT(); } getPairedDevices(); startDiscovery(); } } private void startDiscovery() { // TODO Auto-generated method stub btAdapter.cancelDiscovery(); btAdapter.startDiscovery(); } private void turnOnBT() { // TODO Auto-generated method stub Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } private void getPairedDevices() { // TODO Auto-generated method stub devicesArray = btAdapter.getBondedDevices(); if(devicesArray.size()>0){ for(BluetoothDevice device:devicesArray){ pairedDevices.add(device.getName()); } } } private void init() { // TODO Auto-generated method stub listView=(ListView)findViewById(R.id.bConnectedNew); listView.setOnItemClickListener(this); listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0); listView.setAdapter(listAdapter); btAdapter = BluetoothAdapter.getDefaultAdapter(); pairedDevices = new ArrayList<String>(); filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); devices = new ArrayList<BluetoothDevice>(); receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); devices.add(device); String s = ""; for(int a = 0; a < pairedDevices.size(); a++){ if(device.getName().equals(pairedDevices.get(a))){ //hinzufügen s = "(Paired)"; break; } } listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress()); } else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ } else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ } else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){ if(btAdapter.getState() == btAdapter.STATE_OFF){ turnOnBT(); } } } }; registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(receiver, filter); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); unregisterReceiver(receiver); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_CANCELED){ Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show(); finish(); } } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(btAdapter.isDiscovering()){ btAdapter.cancelDiscovery(); } if(listAdapter.getItem(arg2).contains("Paired")){ BluetoothDevice selectedDevice = devices.get(arg2); ConnectThread connect = new ConnectThread(selectedDevice); connect.start(); Log.i(tag, "in click listener"); } else{ Toast.makeText(getApplicationContext(), "device is not paired", 0).show(); } } private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; public ConnectThread(BluetoothDevice device) { BluetoothSocket tmp = null; mmDevice = device; Log.i(tag, "construct"); try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { Log.i(tag, "get socket failed"); } mmSocket = tmp; } public void run() { btAdapter.cancelDiscovery(); Log.i(tag, "connect - run"); try { mmSocket.connect(); Log.i(tag, "connect - succeeded"); } catch (IOException connectException) { Log.i(tag, "connect failed"); try { mmSocket.close(); } catch (IOException closeException) { } return; } } public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } public class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) { mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer; int bytes; while (true) { try { buffer = new byte[1024]; bytes = mmInStream.read(buffer); // sendet zu UI-Activity mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } catch (IOException e) { break; } } } public void write(byte[] bytes) { try { mmOutStream.write(bytes); } catch (IOException e) { } } public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } }
-
Hoffentlich im richtigen Forum, wenn nicht bitte verschieben.
Hallo, ich möchte eine App entwickeln, die eine bluetooth-verbindung aufbaut und Messdaten empfängt. Diese sollen einfach angezeigt werden. Durch einen Klick auf ein Button soll man dann die Werte aktualisieren können, siehe Code. Und alles soll, wenn möglich in einer Aktivität umgesetzt werden.
Leider stürzt die app immer, wenn ich sie auf meinem smartphone starte ab. Kann mir jemand weiter helfen?!!
Danke
-
nächstes problem.
Der code kompiliert und wenn ich die app mit dem emulator testen will, wir die app unerwwartet beendet.
Mein Code:
Java
Alles anzeigenpackage com.example.endlich; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Set; import java.util.UUID;//test import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Array parts; String ring= "525.565"; TextView textview1; TextView textview2; public void aufteilen() { String[] parts = ring.split("."); textview1.setText(parts[0]); textview2.setText(parts[1]); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) findViewById(R.id.textview1); textview2 = (TextView) findViewById(R.id.textview2); aufteilen(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
und logcat gibt den folgenden Fehler aus
Code
Alles anzeigenjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.endlich/com.example.endlich.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at com.example.endlich.MainActivity.aufteilen(MainActivity.java:43) at com.example.endlich.MainActivity.onCreate(MainActivity.java:54) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
ich habe daraus gelesen, dass irgendwas mit der Initialisierung des Arrays nicht stimmt, bekam die "länge 0" aber nicht weg
danke für eure Hilfe
-
okay vielen dank, ihr habt mir schon sehr geholfen, ich werde versuchen eure tipps umzusetzen und das mit den instanzmethoden nachzulesen
-
das ist mein grundproblem bei der ganzen Sache, ich möchte einfach nur das die funktion aufteilen mit dem gegebene string ausgeführt wird. wenn ich z.b.
ring.aufteilen();
probiere, bekomme ich die Fehlermeldung, dass die Funktion aufteilen nicht bekannt ist.
Vielen dank für das beantworten von meiner AnfängerAnfänger frage.
-
Hi, ich hab eine, ich denke mal für Könner eine sehr einfache frage, ich hab ein string und will diesen aufteilen und dann über 2 verschiedene Textviews ausgeben.Wenn ich mein programm auf dem emulator ausprobiere, wird in den textview´s aber der string nicht angezeit. Ich habe keine Ahnung wo mien Fehler seien könnte. Vielen Dank für eure Antworten/HIlfe. Hab da irgendwas Grundlegendes nicht verstanden.
Java
Alles anzeigenpackage com.example.endlich; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Set; import java.util.UUID;//test import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } String ring="x123.y456"; TextView textview1; TextView textview2; protected void aufteilen() { String[] parts = ring.split("."); textview1.setText(parts[0]); textview2.setText(parts[1]); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Code
Alles anzeigen<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textview2" android:layout_below="@+id/textview1" android:layout_alignRight="@+id/textview1" android:layout_alignEnd="@+id/textview1" android:layout_marginTop="183dp" /> </RelativeLayout>
-
Danke für deine Antworten, aber ich finde keinen Anfangspunkt wo ich anfangen kann und wie es grob ausseen sollte.
hab nur mal ein paar Zeilen "Ideensammlung" :
Java
Alles anzeigenprivate Handler handler = new Handler() { handler.handleMessage(); bytes = mmInStream.read(buffer); m.Handler.obtainMessage(Message_Read, bytes, -1, buffer).sendToTarget(); value = bytes; handler.post(new "Klassenname"(){ public void run();{ TextView.set TextView(value); } }
könnte mir irgendjemand weiterhelfen?
-
tschuldigung, ist mir nicht aufgefallen.
-
Hallo,
mein Ziel ist es Messwerte die per Bluetooth von einem Microcontroller kommen zu empfangen und live anzuzeigen.
Bisher habe ich nur den untenstehendne quellcode. Wie muss ich als nächste vorgehen. Bin leider anfänger und verstehe 2 den untenstehenden code( ist aus einem Beispiel) weiß aber nicht wie ich weiter vorgehen soll.DAnke für eure Hilfe
Java
Alles anzeigenpackage com.test.bluetooth; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Set; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class Main_Activity extends Activity implements OnItemClickListener { ArrayAdapter<String> listAdapter; ListView listView; BluetoothAdapter btAdapter; Set<BluetoothDevice> devicesArray; ArrayList<String> pairedDevices; ArrayList<BluetoothDevice> devices; public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); protected static final int SUCCESS_CONNECT = 0; protected static final int MESSAGE_READ = 1; IntentFilter filter; BroadcastReceiver receiver; String tag = "debugging"; Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub Log.i(tag, "in handler"); super.handleMessage(msg); switch(msg.what){ case SUCCESS_CONNECT: // DO something ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj); Toast.makeText(getApplicationContext(), "CONNECT", 0).show(); String s = "successfully connected"; connectedThread.write(s.getBytes()); Log.i(tag, "connected"); break; case MESSAGE_READ: byte[] readBuf = (byte[])msg.obj; String string = new String(readBuf); Toast.makeText(getApplicationContext(), string, 0).show(); break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); if(btAdapter==null){ Toast.makeText(getApplicationContext(), "No bluetooth detected", 0).show(); finish(); } else{ if(!btAdapter.isEnabled()){ turnOnBT(); } getPairedDevices(); startDiscovery(); } } private void startDiscovery() { // TODO Auto-generated method stub btAdapter.cancelDiscovery(); btAdapter.startDiscovery(); } private void turnOnBT() { // TODO Auto-generated method stub Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } private void getPairedDevices() { // TODO Auto-generated method stub devicesArray = btAdapter.getBondedDevices(); if(devicesArray.size()>0){ for(BluetoothDevice device:devicesArray){ pairedDevices.add(device.getName()); } } } private void init() { // TODO Auto-generated method stub listView=(ListView)findViewById(R.id.listView); listView.setOnItemClickListener(this); listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0); listView.setAdapter(listAdapter); btAdapter = BluetoothAdapter.getDefaultAdapter(); pairedDevices = new ArrayList<String>(); filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); devices = new ArrayList<BluetoothDevice>(); receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); devices.add(device); String s = ""; for(int a = 0; a < pairedDevices.size(); a++){ if(device.getName().equals(pairedDevices.get(a))){ //append s = "(Paired)"; break; } } listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress()); } else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ // run some code } else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ // run some code } else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){ if(btAdapter.getState() == btAdapter.STATE_OFF){ turnOnBT(); } } } }; registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(receiver, filter); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); unregisterReceiver(receiver); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_CANCELED){ Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show(); finish(); } } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(btAdapter.isDiscovering()){ btAdapter.cancelDiscovery(); } if(listAdapter.getItem(arg2).contains("Paired")){ BluetoothDevice selectedDevice = devices.get(arg2); ConnectThread connect = new ConnectThread(selectedDevice); connect.start(); Log.i(tag, "in click listener"); } else{ Toast.makeText(getApplicationContext(), "device is not paired", 0).show(); } } private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; public ConnectThread(BluetoothDevice device) { // Use a temporary object that is later assigned to mmSocket, // because mmSocket is final BluetoothSocket tmp = null; mmDevice = device; Log.i(tag, "construct"); // Get a BluetoothSocket to connect with the given BluetoothDevice try { // MY_UUID is the app's UUID string, also used by the server code tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { Log.i(tag, "get socket failed"); } mmSocket = tmp; } public void run() { // Cancel discovery because it will slow down the connection btAdapter.cancelDiscovery(); Log.i(tag, "connect - run"); try { // Connect the device through the socket. This will block // until it succeeds or throws an exception mmSocket.connect(); Log.i(tag, "connect - succeeded"); } catch (IOException connectException) { Log.i(tag, "connect failed"); // Unable to connect; close the socket and get out try { mmSocket.close(); } catch (IOException closeException) { } return; } // Do work to manage the connection (in a separate thread) mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget(); } /** Will cancel an in-progress connection, and close the socket */ public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } private class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) { mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; // Get the input and output streams, using temp objects because // member streams are final try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputStream until an exception occurs while (true) { try { // Read from the InputStream buffer = new byte[1024]; bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI activity mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } catch (IOException e) { break; } } } /* Call this from the main activity to send data to the remote device */ public void write(byte[] bytes) { try { mmOutStream.write(bytes); } catch (IOException e) { } } /* Call this from the main activity to shutdown the connection */ public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } }