package red.tooth;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class inicio extends Activity {
private BluetoothDevice dispositivos[] = {null,null,null,null,null,null,null,null,null,null};
private BluetoothDevice dispositivoSeleccionado=null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private OutputStream mmOutStream;
private BluetoothSocket mmSocket;
private InputStream mmInStream;
private boolean botonenable=true;
private boolean botondisable=false;
private Button boton;
private Button boton2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBluetoothAdapter.setName("HTC Hero Debug");
// here I made an array of bonded devices
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
int i=0;
for (BluetoothDevice bluetoothDevice:pairedDevices){
dispositivos[i]=bluetoothDevice;
i++;
}
}
public void seleccionDispositivo(View button){
boton = (Button)findViewById(R.id.button2);
boton.setClickable(botondisable);
boton2 = (Button)findViewById(R.id.button4);
boton2.setClickable(botonenable);
//here I choose the device I know I want to conncet, in this case in the list of bonded devices is the 2º (1º->0, 2º->1)
dispositivoSeleccionado=dispositivos[1];
BluetoothSocket tmp = null;
try {
tmp = dispositivoSeleccionado.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
TextView bluetoothStateText3;
bluetoothStateText3 =(TextView)findViewById(R.id.textView2);
bluetoothStateText3.setText("Exito Conexion");
} catch (IOException connectException) {
try {
mmSocket.close();
TextView bluetoothStateText41;
bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
bluetoothStateText41.setText("No exito,Cerrada Conexion");
} catch (IOException closeException) {
TextView bluetoothStateText4;
bluetoothStateText4 =(TextView)findViewById(R.id.textView2);
bluetoothStateText4.setText("No es exito y Error cerrando Conexion");
}
}
}
public void iniConexion (View button){
TextView bluetoothStateText3;
bluetoothStateText3 =(TextView)findViewById(R.id.textView2);
bluetoothStateText3.setText("Stream Iniciado");
boton = (Button)findViewById(R.id.button2);
boton.setClickable(botonenable);
ConnectedThread(mmSocket);
}
public void Desconectar(View button){
boton2 = (Button)findViewById(R.id.button4);
boton2.setClickable(botondisable);
boton = (Button)findViewById(R.id.button2);
boton.setClickable(botondisable);
TextView bluetoothStateText41;
bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
try {
mmSocket.close();
bluetoothStateText41.setText("Exito desconexion");
} catch (IOException e) {
// TODO Auto-generated catch block
bluetoothStateText41.setText("No exito cerrando conexion");
}
}
public void enviarpaq(View button){
TextView bluetoothStateText41;
bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
String mensaje = " CONSEGUIDO ";
try {
mmOutStream.write(mensaje.getBytes());
bluetoothStateText41.setText("Exito enviando");
} catch (IOException e) {
bluetoothStateText41.setText("No exito enviando null");
}
}
public void 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;
}
}