Ver Mensaje Individual
  #4 (permalink)  
Antiguo 30/06/2012, 13:17
Avatar de diegovazgonz
diegovazgonz
 
Fecha de Ingreso: octubre-2010
Ubicación: Ourense
Mensajes: 184
Antigüedad: 13 años, 7 meses
Puntos: 11
Respuesta: problemas envio bluetooht

E encontrado este codigo,pero como puede recortarlo para la funcion que dige al principio

Código C:
Ver original
  1. package red.tooth;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.Set;
  6. import java.util.UUID;
  7. import android.app.Activity;
  8. import android.bluetooth.BluetoothAdapter;
  9. import android.bluetooth.BluetoothDevice;
  10. import android.bluetooth.BluetoothSocket;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15.  
  16.  
  17. public class inicio extends Activity {
  18.  
  19.     private BluetoothDevice dispositivos[] = {null,null,null,null,null,null,null,null,null,null};
  20.     private BluetoothDevice dispositivoSeleccionado=null;
  21.     private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  22.     private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  23.     private OutputStream mmOutStream;
  24.     private BluetoothSocket mmSocket;
  25.     private InputStream mmInStream;
  26.     private boolean botonenable=true;
  27.     private boolean botondisable=false;
  28.     private Button boton;
  29.     private Button boton2;
  30. /** Called when the activity is first created. */
  31.     @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.main);
  35.        
  36.         mBluetoothAdapter.setName("HTC Hero Debug");
  37.  
  38.         // here I made an array of bonded devices
  39.           Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  40.         int i=0;
  41.         for (BluetoothDevice bluetoothDevice:pairedDevices){
  42.         dispositivos[i]=bluetoothDevice;
  43.         i++;
  44.         }
  45.        
  46.            
  47.     }
  48. public void seleccionDispositivo(View button){
  49.     boton = (Button)findViewById(R.id.button2);
  50.     boton.setClickable(botondisable);
  51.     boton2 = (Button)findViewById(R.id.button4);
  52.     boton2.setClickable(botonenable);
  53.    
  54.    
  55.     //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)
  56.     dispositivoSeleccionado=dispositivos[1];
  57.        
  58.     BluetoothSocket tmp = null;
  59.     try {
  60.         tmp = dispositivoSeleccionado.createRfcommSocketToServiceRecord(MY_UUID);
  61.     } catch (IOException e) { }
  62.     mmSocket = tmp;
  63.     try {
  64.         // Connect the device through the socket. This will block
  65.         // until it succeeds or throws an exception
  66.         mmSocket.connect();
  67.         TextView bluetoothStateText3;
  68.         bluetoothStateText3 =(TextView)findViewById(R.id.textView2);
  69.         bluetoothStateText3.setText("Exito Conexion");
  70.     } catch (IOException connectException) {
  71.            try {
  72.             mmSocket.close();
  73.            TextView bluetoothStateText41;
  74.             bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
  75.             bluetoothStateText41.setText("No exito,Cerrada Conexion");
  76.         } catch (IOException closeException) {
  77.             TextView bluetoothStateText4;
  78.             bluetoothStateText4 =(TextView)findViewById(R.id.textView2);
  79.             bluetoothStateText4.setText("No es exito y Error cerrando Conexion");
  80.             }
  81.     }
  82.   }
  83. public void iniConexion (View button){
  84.     TextView bluetoothStateText3;
  85.     bluetoothStateText3 =(TextView)findViewById(R.id.textView2);
  86.     bluetoothStateText3.setText("Stream Iniciado");
  87.     boton = (Button)findViewById(R.id.button2);
  88.     boton.setClickable(botonenable);
  89.     ConnectedThread(mmSocket);
  90.     }
  91. public void Desconectar(View button){
  92.    
  93.     boton2 = (Button)findViewById(R.id.button4);
  94.     boton2.setClickable(botondisable);
  95.     boton = (Button)findViewById(R.id.button2);
  96.     boton.setClickable(botondisable);  
  97.         TextView bluetoothStateText41;
  98.         bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
  99.        try {
  100.         mmSocket.close();
  101.         bluetoothStateText41.setText("Exito desconexion");
  102.     } catch (IOException e) {
  103.         // TODO Auto-generated catch block
  104.        
  105.         bluetoothStateText41.setText("No exito cerrando conexion");
  106.     }
  107.        
  108.    }
  109. public void enviarpaq(View button){
  110.        TextView bluetoothStateText41;
  111.         bluetoothStateText41 =(TextView)findViewById(R.id.textView2);
  112.        String mensaje = " CONSEGUIDO  ";
  113.             try {
  114.                 mmOutStream.write(mensaje.getBytes());
  115.                 bluetoothStateText41.setText("Exito enviando");
  116.             } catch (IOException e) {
  117.                 bluetoothStateText41.setText("No exito enviando null");
  118.             }
  119.         }
  120. public void ConnectedThread (BluetoothSocket socket) {
  121.         mmSocket = socket;
  122.         InputStream tmpIn = null;
  123.         OutputStream tmpOut = null;
  124.  
  125.         // Get the input and output streams, using temp objects because
  126.         // member streams are final
  127.         try {
  128.             tmpIn = socket.getInputStream();
  129.             tmpOut = socket.getOutputStream();
  130.         } catch (IOException e) { }
  131.  
  132.         mmInStream = tmpIn;
  133.         mmOutStream = tmpOut;
  134.     }  
  135. }
__________________
El mejor vino Ribeiro http://www.pedeperdiz.com