Search in sources :

Example 1 with GBDeviceCandidate

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate in project Gadgetbridge by Freeyourgadget.

the class DiscoveryActivity method handleDeviceFound.

private void handleDeviceFound(BluetoothDevice device, short rssi, ParcelUuid[] uuids) {
    LOG.debug("found device: " + device.getName() + ", " + device.getAddress());
    if (LOG.isDebugEnabled()) {
        if (uuids != null && uuids.length > 0) {
            for (ParcelUuid uuid : uuids) {
                LOG.debug("  supports uuid: " + uuid.toString());
            }
        }
    }
    if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
        // ignore already bonded devices
        return;
    }
    GBDeviceCandidate candidate = new GBDeviceCandidate(device, rssi, uuids);
    DeviceType deviceType = DeviceHelper.getInstance().getSupportedType(candidate);
    if (deviceType.isSupported()) {
        candidate.setDeviceType(deviceType);
        LOG.info("Recognized supported device: " + candidate);
        int index = deviceCandidates.indexOf(candidate);
        if (index >= 0) {
            // replace
            deviceCandidates.set(index, candidate);
        } else {
            deviceCandidates.add(candidate);
        }
        cadidateListAdapter.notifyDataSetChanged();
    }
}
Also used : ParcelUuid(android.os.ParcelUuid) DeviceType(nodomain.freeyourgadget.gadgetbridge.model.DeviceType) GBDeviceCandidate(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate)

Example 2 with GBDeviceCandidate

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate in project Gadgetbridge by Freeyourgadget.

the class DiscoveryActivity method onItemClick.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    GBDeviceCandidate deviceCandidate = deviceCandidates.get(position);
    if (deviceCandidate == null) {
        LOG.error("Device candidate clicked, but item not found");
        return;
    }
    stopDiscovery();
    DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(deviceCandidate);
    LOG.info("Using device candidate " + deviceCandidate + " with coordinator: " + coordinator.getClass());
    Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
    if (pairingActivity != null) {
        Intent intent = new Intent(this, pairingActivity);
        intent.putExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE, deviceCandidate);
        startActivity(intent);
    } else {
        try {
            BluetoothDevice btDevice = adapter.getRemoteDevice(deviceCandidate.getMacAddress());
            switch(btDevice.getBondState()) {
                case BluetoothDevice.BOND_NONE:
                    {
                        if (btDevice.createBond()) {
                            // async, wait for bonding event to finish this activity
                            bondingAddress = btDevice.getAddress();
                        }
                        break;
                    }
                case BluetoothDevice.BOND_BONDING:
                    // async, wait for bonding event to finish this activity
                    bondingAddress = btDevice.getAddress();
                    break;
                case BluetoothDevice.BOND_BONDED:
                    handleDeviceBonded();
                    break;
            }
        } catch (Exception e) {
            LOG.error("Error pairing device: " + deviceCandidate.getMacAddress());
        }
    }
}
Also used : GBDeviceCandidate(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate) BluetoothDevice(android.bluetooth.BluetoothDevice) Intent(android.content.Intent) DeviceCoordinator(nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)

Example 3 with GBDeviceCandidate

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate in project Gadgetbridge by Freeyourgadget.

the class DeviceCandidateAdapter method getView.

@Override
public View getView(int position, View view, ViewGroup parent) {
    GBDeviceCandidate device = getItem(position);
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item_with_details, parent, false);
    }
    ImageView deviceImageView = (ImageView) view.findViewById(R.id.item_image);
    TextView deviceNameLabel = (TextView) view.findViewById(R.id.item_name);
    TextView deviceAddressLabel = (TextView) view.findViewById(R.id.item_details);
    String name = formatDeviceCandidate(device);
    deviceNameLabel.setText(name);
    deviceAddressLabel.setText(device.getMacAddress());
    deviceImageView.setImageResource(device.getDeviceType().getIcon());
    return view;
}
Also used : GBDeviceCandidate(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Example 4 with GBDeviceCandidate

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate in project Gadgetbridge by Freeyourgadget.

the class PebblePairingActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pebble_pairing);
    message = (TextView) findViewById(R.id.pebble_pair_message);
    Intent intent = getIntent();
    GBDeviceCandidate candidate = intent.getParcelableExtra(DeviceCoordinator.EXTRA_DEVICE_CANDIDATE);
    if (candidate != null) {
        macAddress = candidate.getMacAddress();
    }
    if (macAddress == null) {
        Toast.makeText(this, getString(R.string.message_cannot_pair_no_mac), Toast.LENGTH_SHORT).show();
        returnToPairingActivity();
        return;
    }
    mBtDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
    if (mBtDevice == null) {
        GB.toast(this, "No such Bluetooth Device: " + macAddress, Toast.LENGTH_LONG, GB.ERROR);
        returnToPairingActivity();
        return;
    }
    isLEPebble = mBtDevice.getType() == BluetoothDevice.DEVICE_TYPE_LE;
    GBDevice gbDevice = null;
    if (isLEPebble) {
        if (mBtDevice.getName().startsWith("Pebble-LE ") || mBtDevice.getName().startsWith("Pebble Time LE ")) {
            if (!GBApplication.getPrefs().getBoolean("pebble_force_le", false)) {
                GB.toast(this, "Please switch on \"Always prefer BLE\" option in Pebble settings before pairing you Pebble LE", Toast.LENGTH_LONG, GB.ERROR);
                returnToPairingActivity();
                return;
            }
            gbDevice = getMatchingParentDeviceFromDB(mBtDevice);
            if (gbDevice == null) {
                return;
            }
        }
    }
    startPairing(gbDevice);
}
Also used : GBDeviceCandidate(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate) Intent(android.content.Intent) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Aggregations

GBDeviceCandidate (nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate)4 Intent (android.content.Intent)2 BluetoothDevice (android.bluetooth.BluetoothDevice)1 ParcelUuid (android.os.ParcelUuid)1 LayoutInflater (android.view.LayoutInflater)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 DeviceCoordinator (nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 DeviceType (nodomain.freeyourgadget.gadgetbridge.model.DeviceType)1