Feststellen, ob Bluetooth-Verbindung noch existiert

H

hs1

Fortgeschrittenes Mitglied
8
Hallo Leute,

ich muss in einer App einen mobilen Zebra-Bluetoothdrucker ansteuern. Das Drucken klappt auch ohne Probleme.
Die Bluetoothverbindung zum Drucker wird direkt mittels der Bluetooth-MAC-Adresse des Druckers aufgebaut:

minSdkVersion 16
targetSdkVersion 24

Code:
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;
    }


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



Das ganze funktioniert soweit sehr gut. Die Bluetooth-Verbindung wird die ganze Zeit aufrecht erhalten (vor jedem Druck neu aufbauen und danach schließen dauert zu lange; Verbindungsaufbau dauert ca. 5 Sekunden).
Mein Problem ist, dass ich nicht mitbekomme wenn der Drucker ausgeschaltet wird und die Verbindung abreißt.

Ich möchte also vor jedem Ausdruck kurz abfragen, ob die Verbindung noch existiert. btSocket.isConnected() gibt leider grundsätzlich true zurück. Ebenso kann ich trotz nicht mehr vorhandener Verbindung Daten mittels .write() an den outStream schicken, ohne dass eine IOException kommt.

Ich habe auch versucht, das ganze mittels BroadcastReceiver zu lösen. Leider wird ACTION_ACL_DISCONNECTED erst ausgelöst, wenn der Drucker wieder eingeschaltet wird (warum auch immer). ACTION_ACL_DISCONNECT_REQUESTED wird gar nicht ausgelöst.

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

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        [USER=3517]@override[/USER]
        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");
            }
        }
    };



Ich bin mit meinem Latein am Ende. Habt Ihr eine Idee wie ich abfragen kann, ob die Verbindung zum Drucker zwischenzeitlich getrennt wurde?


Gruß hs1
 
@hs1

Hallo hs1,

ich habe leider noch nicht mit der Kombination BT und einem Drucker gearbeitet, deshalb kann ich nur hier vermuten.

Das Erste , was mir dazu eingefallen ist, wäre zu schauen, ob das BT Device noch da ist ( neuer Scan )
Das Zweite, was mir dazu noch eingefallen ist : Einen Ping auf die IP mittels Shell abzusetzen.

Und als Letztes : Versuche mal die Sockets, nonblock zu setzen.
 

Ähnliche Themen

S
Antworten
4
Aufrufe
4.368
mblaster4711
mblaster4711
N
Antworten
8
Aufrufe
1.015
NerkJ
N
M
Antworten
11
Aufrufe
2.261
jogimuc
J
Zurück
Oben Unten