0

我有一个应用程序,它有一个主要活动和一个类。当我按下后退按钮时,它应该关闭应用程序,但它什么也不做:当我按下按钮时,按钮背光灯亮然后熄灭。如果我再次按下它,我只会听到一些声音反馈。

我的应用程序一直在进行蓝牙通信。蓝牙可以覆盖密钥的正常行为吗?

我也有 onPause 和 onResume。

尝试使用 onBackPressed,它永远不会被调用:

    @Override
 public void onBackPressed() {
super.onBackPressed();
    Log.i ("INFO","BackKey pressed");
         }

BtComm 类:

    //Enables Bluetooth if not enabled
public void enableBT(){
localAdapter=BluetoothAdapter.getDefaultAdapter();
//If Bluetooth not enable then do it
if(localAdapter.isEnabled()==false){
    localAdapter.enable();
    while(!(localAdapter.isEnabled())){

    }
}

}

    //connect to both NXTs
    public  boolean connectToNXTs(){



//get the BluetoothDevice of the NXT
BluetoothDevice nxt_1 = localAdapter.getRemoteDevice(nxt1);
//try to connect to the nxt
try {
    socket_nxt1 = nxt_1.createRfcommSocketToServiceRecord(UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB"));
        socket_nxt1.connect();

    success = true;

} catch (IOException e) {
    Log.i("Bluetooth","Err: Device not found or cannot connect");
    success=false;


}
if (success) {
     Log.i("CONECTE","CONEXION EXITOSA");
}
return success;

}






public void writeMessage(byte[] msg) throws InterruptedException{
//BluetoothSocket connSock;




  //  connSock=socket_nxt1;

    try {

       // OutputStreamWriter out =new OutputStreamWriter(connSock.getOutputStream());
      // mmOutStream.write(msg);
       // out.flush();
        OutputStream outputStream = socket_nxt1.getOutputStream();
        outputStream.write(msg);
        Log.i("Excelente","COMANDO EXITOSO");



    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.i("PROBLEMA","ERROR DE DATOS");
    }
}


public byte [] readMessage(){
byte[] buffer = new byte[64];
Log.i("Excelente","Entre a readmessage");

    try {

        InputStream inputstream = socket_nxt1.getInputStream();         
        inputstream.read(buffer);
        //mmInStream.read(buffer);

        Log.i("Excelente","Lei los datos");

        for (int index = 0; index < buffer.length; index++){

             Log.i("RECIBIDO:", String.format("0x%20x", buffer[index]));

        }

        Log.i("Excelente","Recibi datos y los escribi");

        return buffer;


    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.i("PROBLEMA","ERROR DE DATOS AL RECIBIR");
        return null;
    }


}

public void cancel() {
    try {
        //OutputStream outputStream = socket_nxt1.getOutputStream();
        socket_nxt1.close();
    } catch (IOException e) {
        Log.e("ERROR","Unable to close");
    }
}
4

0 回答 0