Search in sources :

Example 1 with DeviceDao

use of nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao 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)

Example 2 with DeviceDao

use of nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao in project Gadgetbridge by Freeyourgadget.

the class DBHelper method findDevice.

public static Device findDevice(GBDevice gbDevice, DaoSession session) {
    DeviceDao deviceDao = session.getDeviceDao();
    Query<Device> query = deviceDao.queryBuilder().where(DeviceDao.Properties.Identifier.eq(gbDevice.getAddress())).build();
    List<Device> devices = query.list();
    if (devices.size() > 0) {
        return devices.get(0);
    }
    return null;
}
Also used : Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) DeviceDao(nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao)

Aggregations

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