Clean Cache BLE Bluetooth

1

I am doing a reading of a device, and when making successive readings the information is not updated, so I would need to know how I could clean the cache or buffer that may be causing this information not to update. It is using the BLE module already implemented in the SDK that brings the device calling the methods already defined in this.

Specifically it is a bracelet that captures the heart rate and as for the SDK does not bring documentation just a few examples. That calling the methods as indicated, after some synchronization returns the same data that sometimes after a while it is updated again and others do not, I have also noticed that sometimes the data returned is wrong.

ProtocolUtils.getInstance().StartSyncHealthData();//Llamada para sincronizar

@Override
public void onSysEvt(int evt_base, final int evt_type, int error, final int value) {
    // TODO Auto-generated method stub
    if(evt_type == ProtocolEvt.SYNC_EVT_HEALTH_PROGRESS.toIndex()){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setTitle("Synchronization : " + value + "%");
            }
        }, 200);
    }else if (evt_type == ProtocolEvt.SYNC_EVT_HEALTH_SYNC_COMPLETE.toIndex()) {//Synchronization is complete
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 200);
    }
}
    
asked by ja12 23.08.2017 в 09:43
source

1 answer

0

I have had the opportunity to work before with android and devices via bluetooth connection. Considering that you do not specify much about the device, I recommend that you look for the official documentation of it, but here is an example made by me at some time.

I'm going to do the example with a bixolon printer, of which there is very little documentation: Each device has its methods and libraries to import, for example: com.bixolon.printer.BixolonPrinter and android.bluetooth.BluetoothDevice

Once you have that you must make a DeviceProvider.java class and implement some callbacks:

public interface ConnectionCallback {

    void onPrinterConnected();

    void onPrinterNotFound();

    void onConnectingPrinter();

    void onPrinterNameFound(String printerName);
}

This will help you manage the events that happen with the device, the library of the device must have a class with constants that inform you of events, states and methods:

Lastly, in the DeviceProvider, you must implement a thread that listens constantly, in the process initialize the Bluetooth of the cell phone and manage according to the responses of the device:

private PrinterProvider(Context context, BaseSchedulerProvider schedulerProvider) {
    mBixolonPrinter = new BixolonPrinter(context, handler, null);
    mFindPrinterSubscription = Observable.create(e -> {
        int scanned = 0;
        while (mPrinterScanningRunning) {
            if (!mConnectedPrinter) {

                if (scanned < 20) {
                    mBixolonPrinter.findBluetoothPrinters();
                    scanned++;
                } else {
                    scanned = 0;
                    mBixolonPrinter.disconnect();
                }
            }
            Thread.sleep(1000);
        }
    })
            .subscribeOn(schedulerProvider.computation())
            .subscribe();
}

This would be the device's status handler:

private final Handler handler = new Handler(msg -> {
    switch (msg.what) {
        case BixolonPrinter.MESSAGE_STATE_CHANGE: {
            switch (msg.arg1) {
                case BixolonPrinter.STATE_CONNECTED:
                    mConnectedPrinter = true;
                    String printerName = msg.getData().getString(BixolonPrinter.KEY_STRING_DEVICE_NAME);
                    for (ConnectionCallback callback : mConnectionCallbacks) {
                        callback.onPrinterConnected();
                        mPrintername=printerName;
                    }
                    break;
                case BixolonPrinter.STATE_CONNECTING:
                    mConnectedPrinter = false;
                    for (ConnectionCallback callback : mConnectionCallbacks)
                        callback.onConnectingPrinter();
                    break;
                case BixolonPrinter.STATE_NONE:
                    mConnectedPrinter = false;
                    for (ConnectionCallback callback : mConnectionCallbacks)
                        callback.onPrinterNotFound();
                    break;
            }
            break;
        }
        case BixolonPrinter.MESSAGE_BLUETOOTH_DEVICE_SET:
            if (msg.obj == null) {
                mConnectedPrinter = false;
                for (ConnectionCallback callback : mConnectionCallbacks)
                    callback.onPrinterNotFound();
            } else {
                Set<BluetoothDevice> pairedDevices = (Set<BluetoothDevice>) msg.obj;
                for (BluetoothDevice device : pairedDevices) {
                    if (!pairedPrinters.contains(device.getAddress())) {
                        pairedPrinters.add(device.getAddress());
                    }
                    if (pairedPrinters.size() == 1) {
                        mBixolonPrinter.connect(pairedPrinters.get(0));
                    }
                }
            }
            break;

        case BixolonPrinter.MESSAGE_DEVICE_NAME:
            String printerName = msg.getData().getString(BixolonPrinter.KEY_STRING_DEVICE_NAME);
            for (ConnectionCallback callback : mConnectionCallbacks)
                callback.onPrinterNameFound(printerName);
            break;
        case BixolonPrinter.MESSAGE_USB_DEVICE_SET:
            if (msg.obj == null) {
                for (ConnectionCallback callback : mConnectionCallbacks)
                    callback.onPrinterNotFound();
            }
            break;
    }

    return false;
});

At the time it was the most orderly I could implement, if you look for the documentation of your device you can do something like that and you would not have problems with cache.

    
answered by 23.08.2017 в 18:03