简体   繁体   English

在Android上通过蓝牙读取信息

[英]Reading information via bluetooth on Android

I hope my question is not repeated at all, i tried reading another people that asks for similar information but couldnt fix my problem. 我希望我的问题不再重复,我试图阅读另一个要求类似信息但无法解决我问题的人。

The thing here is that i have an android app that uses bluetooth , i have succesfully set the connection , i know that my app is sending a message when it gets connected to another device, but the problem here is that when i receive a message i would like to make it appear in a text view , or in a toast , but it is not reading properly. 这里的问题是我有一个使用蓝牙的android应用程序,我已经成功设置了连接,我知道我的应用程序在连接到另一台设备时正在发送消息,但是这里的问题是当我收到消息时想要使其显示在文本视图或Toast中,但无法正确阅读。

Hope you guys can help me ill give you my code 希望你们能帮我给我我的代码

package com.example.cesar.drcarobd2;



public class MainActivity extends AppCompatActivity implements SensorEventListener, AdapterView.OnItemClickListener {

private static final int CONEXION_EXITOSA = 0 ;
private static final int MESSAGE_READ = 1;
public static final int VELOCIDAD_ESCRITO = 2;
private static final int RPM_ESCRITO = 3 ;
// device sensor manager
private SensorManager mSensorManager;
ArrayAdapter<String> listAdapter;
ListView listView;
TextView brujula;
TextView velocidad;
BluetoothAdapter btAdapter; //creas un Adaptador bluetooth
Set<BluetoothDevice> ArregloDispositivos;
ArrayList<String> dispositivosApareados;
ArrayList<BluetoothDevice> dispositivos;
IntentFilter filtro;
BroadcastReceiver receptor;
private ArrayAdapter<String> Adapterdeconversacion;
String tag = "debugging";
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") ;  //UUID de conexion bt
Handler mhandler = new Handler(){


    @Override
    public void handleMessage(Message msg){
        ConnectedThread threadEscritura = new ConnectedThread((BluetoothSocket)msg.obj);
       // threadEscritura.run();
       // ConnectedThread threadConectado = new ConnectedThread((BluetoothSocket)msg.obj);
        Log.i(tag, "en el handler");
       // threadConectado.run();
        super.handleMessage(msg);
        switch(msg.what){
            case CONEXION_EXITOSA:
                    //Hacer algo
                Toast.makeText(getApplicationContext(), "Conexión", Toast.LENGTH_SHORT).show();
                Log.i(tag, "Conectado");
                String s = "Conectado Satisfactoriamente ";
                threadEscritura.write(s.getBytes());
                Log.i(tag, "Mensaje de Conexion Enviado");
               // threadConectado.run();
                break;
            case MESSAGE_READ:
                byte[] bufferleido = (byte[])msg.obj;
                String texto = new String(bufferleido);
                System.out.println(texto);
                Toast.makeText(getApplicationContext(), texto, Toast.LENGTH_SHORT).show();
                Log.i(tag, "Haciendo el Toast");
                velocidad.setText(texto);
                Log.i(tag, "Haciendo el Texto");
            case VELOCIDAD_ESCRITO:
                String mensajeVelocidad = "010D" + '\r';
                threadEscritura.write(mensajeVelocidad.getBytes());
                Log.i(tag, "Enviando Mensaje Velocidad");
                break;
            case RPM_ESCRITO:
                String mensajeRPM = "010C" + '\r';
                threadEscritura.write(mensajeRPM.getBytes());
                break;
        }//fin switch
    }//fin HandleMessage
};//fin handler



//TODO    HACER METODO QUE  ENVÍE EL MENSAJE DE LA VELOCIDAD

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();//llamas la inicializacion de todos
    if(btAdapter==null){//vamos a revisar si hay bluetooth en el dispositivo en caso de que sea null avisamos que no hay bluetooth
        Toast.makeText(getApplicationContext(), "No se detecto bluetooth", Toast.LENGTH_SHORT).show();
        finish();
    }//fin if
    else{//en caso de que si hay bluetooth vamos a continuar revisando que este o no activado
        if(!btAdapter.isEnabled()){//en caso de que no este activado
           encenderBt();//llamamos metodo enceder bluetooth
        }

        getDispositivosSincronizados(); //Vamos a revisar los dispositivos sincronizados , los vamos a meter en un array
        iniciarDescubrimiento(); //y despues vamos a iniciar el descubrimiento y vamos a poblar esa lista
    }//fin else


    // Informamos el nombre del texto view donde despliego la brujula
    brujula = (TextView) findViewById(R.id.brujula_textview);
    //Text View de la velocidad
    velocidad = (TextView) findViewById(R.id.velocidad_textview);
    // initialize your android device sensor capabilities
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


}




private void iniciarDescubrimiento() {
    btAdapter.cancelDiscovery();
    btAdapter.startDiscovery();
}

private void encenderBt() {//enciende el bt del dispositivo
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);//Haremos una peticion para activarlo
    startActivityForResult(intent, 1);
}

private void getDispositivosSincronizados() {

    ArregloDispositivos = btAdapter.getBondedDevices();
    if(ArregloDispositivos.size()>0){//si tenemos mas de un dispositivop apareado en el telefono
        for(BluetoothDevice device:ArregloDispositivos){//Para cada uno de los dispositivos apareados los vamos a incluir en nuestra lista
            dispositivosApareados.add(device.getName());

        }//fin for
    }//fin if
}

private void init() {//usado para bt , no interfiere con brjula  Aqui inicializamos

    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(); //inicias un adaptador bluetooth
    dispositivosApareados = new ArrayList<String>();
    filtro = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    dispositivos = new ArrayList<BluetoothDevice>();
    receptor = new BroadcastReceiver() { //hacemos broadcast para encontrar nuevos dispositivos
        @Override
        public void onReceive(Context context, Intent intent) {
            String Accion = intent.getAction();//realizamos una accion

            if(BluetoothDevice.ACTION_FOUND.equals(Accion)){//Si encontramos un dispositivo Bluetooth entonces lo añadimos a la lista
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                dispositivos.add(device);
               // listAdapter.add(device.getName()+"\n"+device.getAddress()); ----------------------------------------------------------------------------------probable eliminacion

                String s = "";
                for(int a=0 ; a< dispositivosApareados.size(); a++){
                    if(device.getName().equals(dispositivosApareados.get(a))){//si algun elemento de los dispositivos apareados del celular y de la lista de dispositivos son iguales, sabremos que ya estaban apareados
                        s = "(Apareado)";  //una vez que sabemos que estan apareados ese valor se concatena con la direccion del dispositivo bluetooth en cuestion
                        break;
                    }//fin if
                }//fin for


                listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress()); //se toma el nombre del dispositivo, se le añade la marca de Apareado y se concatena su direccion tambien
            }//fin if

            else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(Accion)){//comparar los strings del filtro con los strings de las acciones

            }//fin elseif started
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(Accion)){//comparar los strings del filtro con los strings de las acciones

            }//fin elseif finished
            else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(Accion)){//comparar los strings del filtro con los strings de las acciones
                if(btAdapter.getState() == btAdapter.STATE_OFF){//en caso de que haya un cambio de estado a apagado prendemos de nuevo
                    encenderBt();
                }//fin if state of
            }//fin elseif changed

        }//fin funcion onReceive
    };//fin broadcast receiver

    registerReceiver(receptor, filtro);
    filtro = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receptor, filtro);
    filtro = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receptor, filtro);
    filtro = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(receptor, filtro);
}

@Override
protected void onPause(){// Estado de la aplicacion al ser Pausada
    super.onPause();
    unregisterReceiver(receptor);//debemos quitar el registro de los dispositivos bluetooth en caso de pausar la app
    // este si es para la brujula, si se pausa la app detenemos los sensores para guardar bateria
    mSensorManager.unregisterListener(this);
}//fin del metodo on Pause


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_CANCELED){
        Toast.makeText(getBaseContext(), "El bluetooth debe ser activado", Toast.LENGTH_SHORT).show();
        finish();//si el usuario cancela la activacion del bluetooth terminamos el uso de la app pues no funcionaria de ese modo
    }//fin if
}

@Override
protected void onResume() {
    super.onResume();

    // for the system's orientation sensor registered listeners
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_GAME);
}


@Override
public void onSensorChanged(SensorEvent event) {

    float x = event.values[0];
    String dir = "";
    if (x >= 337.5 || x < 22.5) {
        dir = "N";
    } else if (x >= 22.5 && x < 67.5) {
        dir = "NE";
    } else if (x >= 67.5 && x < 112.5) {
        dir = "E";
    } else if (x >= 112.5 && x < 157.5) {
        dir = "SE";
    } else if (x >= 157.5 && x < 202.5) {
        dir = "S";
    } else if (x >= 202.5 && x < 247.5) {
        dir = "SO";
    } else if (x >= 247.5 && x < 292.5) {
        dir = "O";
    } else if (x >= 292.5 && x < 337.5) {
        dir = "NO";
    }
    brujula.setText(dir);

}



@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}

@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);
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//METODO ocupado por la implementacion del onItemClickListener que funcionara para hacer clickeable la lista en lugar del boton

    if(btAdapter.isDiscovering()){
        btAdapter.cancelDiscovery();
    }
    if(listAdapter.getItem(position).contains("Apareado")){
        BluetoothDevice dispositivoSeleccionado = dispositivos.get(position);//position es donde fue clickeado
        ConnectThread conectar = new ConnectThread(dispositivoSeleccionado);// esa posicion representa un dispositivo bluetooth al ser clickeado hace un thread para conectarse
        conectar.start();
    }//fin if
    else{
        Toast.makeText(getApplicationContext(),"El dispositivo no esta apareado",Toast.LENGTH_SHORT).show();
    }//fin else
}//finOnItemClick


private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;


    public void VelocidadContinuo(){


    }


    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, "En la parte del Socket");
        // 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, "Error en el Socket");}
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        btAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
            Log.i(tag, "Conexión realizada con el Socket");
        } catch (IOException connectException) { Log.i(tag, "Error de conexión en run de Connect Thread");
            // Si no te puedes conectar cierras el socket y sales
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;

        }



        // Do work to manage the connection (in a separate thread)

        mhandler.obtainMessage(CONEXION_EXITOSA,mmSocket).sendToTarget();
    }




    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}


//--------------------------------------------------------Iniciamos con el código para el SOCKET


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);
                // Enviar mensajes a la actividad de la UI
                mhandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                //mhandler.obtainMessage(MESSAGE_READ,mmSocket).sendToTarget(); //TEMPORAL AL RECIBIR ALGO MANDAR EL MENSAJE DE PEDIR VELOCIDAD OBD2
                        .sendToTarget();

                Log.i(tag, "Bytes leidos wooho");
                Log.i(tag, String.valueOf(bytes));

            } catch (IOException e) {
                Log.e(tag, "disconnected", e);
                // Start the service over to restart listening mode
                this.start();
                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) { }
    }
}//------------------------------------------------------------------------------------------------

} }

The connection socket is at the end of the code , with the write method )That is working) 连接套接字位于代码的末尾,使用write方法)(有效)

And the case that do something are at the begginig. 而做某事的情况就在开始。

Thanks a lot for your help guys. 非常感谢您的帮助。

Is your code running from a thread different from the main thread? 您的代码是否从与主线程不同的线程运行? In that case you need to switch to the main thread to show your toast message or to make modifications on the UI. 在这种情况下,您需要切换到主线程以显示吐司消息或在UI上进行修改。

Android: Toast in a thread Android:在线程中吐司

In that case, all you need to do is: 在这种情况下,您需要做的只是:

    activity.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        Toast.makeText(getApplicationContext(), "Conexión",
                       Toast.LENGTH_SHORT).show();

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM