socketsbluetoothandroid-bluetoothandroid-print-framework

Android Bluethoot socket connection


hello I connect my device android to printers, of the way follows.

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      BluetoothDevice bluetakebt220 =
bluetoothAdapter.getRemoteDevice(obj.getMac().toString());
      BluetoothSocket mBTsocket= null;
      bluetoothAdapter.cancelDiscovery();  
       UUID num = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
       mBTsocket = bluetakebt220.createRfcommSocketToServiceRecord(num);
       mBTsocket.connect();

this works well for for most devices, but some do not connected, ¿that can be due?


Solution

  • the device acts equal, simply does not establish proper connection , but yes allow write in

    Here is the code if someone can help

    UUID num = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      BluetoothDevice bluetakebt220 = bluetoothAdapter.getRemoteDevice(obj.getMac().toString());//PRINTER ADAPTER FOR LPT BLUETAKE BT220
      BluetoothSocket mBTsocket= null;
      mBTsocket = bluetakebt220.createRfcommSocketToServiceRecord(num);
      bluetoothAdapter.cancelDiscovery();    
      mBTsocket.connect();
      OutputStream os = **mBTsocket**.getOutputStream();
      os.flush();
      byte[] CPCLFormat = null;
      PCLFormat = objImpresion.getTexto().getBytes("utf-8");
      os.write(CPCLFormat);//se imprime el texto
      os.flush();
      os.close();
    

    .I simply comment msocket.isConnected and write directly after open connection. i don´t why happen