Hi all,

I have to connect my app to a mobile zebra printer. Print via bluetooth 
works fine.
The bt-connection will be established directly with the mac address of the 
bluetooth printer:



minSdkVersion 16
targetSdkVersion 24

public class BluetoothConnection{

    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;
    private Context context = null;

    // SPP UUID service
    private static final UUID MY_UUID = UUID.fromString(
"00001101-0000-1000-8000-00805F9B34FB");


    public BluetoothConnection(String pAddress, Context pContext){
        this.address= pAddress;
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        this.context = pContext;
        checkBTState();
    }


    private BluetoothSocket createBluetoothSocket(BluetoothDevice device) 
throws IOException {
        if(Build.VERSION.SDK_INT >= 10){
            try {
                final Method  m = device.getClass().getMethod(
"createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                return (BluetoothSocket) m.invoke(device, MY_UUID);
            } catch (Exception e) {
            }
        }
        return  device.createRfcommSocketToServiceRecord(MY_UUID);
    }

    public boolean connect() {

        boolean retVal = true;
        if (btAdapter == null){
            return false;
        }

        // Set up a pointer to the remote node using it's address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);

        try {
            btSocket = createBluetoothSocket(device);
            retVal = true;
        } catch (IOException e1) {
               retVal = false;
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        btAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        try {
            btSocket.connect();
        } catch (IOException e) {
            try {
                btSocket.close();
                retVal = true;
            } catch (IOException e2) {
                retVal = false;
            }
        }


        try {
            outStream = btSocket.getOutputStream();
            retVal = true;
        } catch (IOException e) {
            retVal = false;
        }

        return true;
    }


     .....................




The printing works fine now. I keep the bluetooth connection alive when the 
app runs, because establishing the connection costs 4 - 6 seconds. This 
works fine.
My problem is, I cannot detect if the printer is powered off.

I want to check before each print job if the bluetooth connection to the 
printer still exists & works. btSocket.isConnected() always returns true 
(even if the printer is powered off). I can send data to the outstream with 
write() without getting an IOException.

I tried to use a BroadcastReceiver too. Unfortunatly 
ACTION_ACL_DISCONNECTED is raised when the printer is powered on, not when 
it is powered off. ACTION_ACL_DISCONNECT_REQUESTED is never raised.

    registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.
ACTION_ACL_DISCONNECTED));
    registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.
ACTION_ACL_DISCONNECT_REQUESTED));

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(
BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                Logger.d("Device found");
            }
            else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                Logger.d("Device is now connected");
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(
action)) {
                Logger.d("Done searching");
            }
            else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(
action)) {
                Logger.d("Device is about to disconnect");
            }
            else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) 
{
                Logger.d("Device has disconnected");
            }
        }
    };





What can I do to detect if the bluetooth connection exists and works?


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/7000b843-2574-4cbf-ae74-2bf9aa15796e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to