Search in sources :

Example 1 with DeviceHelper

use of nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper in project Gadgetbridge by Freeyourgadget.

the class PebblePairingActivity method getMatchingParentDeviceFromDB.

private GBDevice getMatchingParentDeviceFromDB(BluetoothDevice btDevice) {
    String expectedSuffix = btDevice.getName();
    expectedSuffix = expectedSuffix.replace("Pebble-LE ", "");
    expectedSuffix = expectedSuffix.replace("Pebble Time LE ", "");
    expectedSuffix = expectedSuffix.substring(0, 2) + ":" + expectedSuffix.substring(2);
    LOG.info("will try to find a Pebble with BT address suffix " + expectedSuffix);
    GBDevice gbDevice = null;
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        DeviceDao deviceDao = session.getDeviceDao();
        Query<Device> query = deviceDao.queryBuilder().where(DeviceDao.Properties.Type.eq(1), DeviceDao.Properties.Identifier.like("%" + expectedSuffix)).build();
        List<Device> devices = query.list();
        if (devices.size() == 0) {
            GB.toast("Please pair your non-LE Pebble before pairing the LE one", Toast.LENGTH_SHORT, GB.INFO);
            returnToPairingActivity();
            return null;
        } else if (devices.size() > 1) {
            GB.toast("Can not match this Pebble LE to a unique device", Toast.LENGTH_SHORT, GB.INFO);
            returnToPairingActivity();
            return null;
        }
        DeviceHelper deviceHelper = DeviceHelper.getInstance();
        gbDevice = deviceHelper.toGBDevice(devices.get(0));
        gbDevice.setVolatileAddress(btDevice.getAddress());
    } catch (Exception e) {
        GB.toast("Error retrieving devices from database", Toast.LENGTH_SHORT, GB.ERROR);
        returnToPairingActivity();
        return null;
    }
    return gbDevice;
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) BluetoothDevice(android.bluetooth.BluetoothDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) DeviceHelper(nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper) DeviceDao(nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Aggregations

BluetoothDevice (android.bluetooth.BluetoothDevice)1 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 DeviceDao (nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 DeviceHelper (nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper)1