Search in sources :

Example 36 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Gadgetbridge by Freeyourgadget.

the class DeviceHelper method getBondedDevices.

private List<GBDevice> getBondedDevices(BluetoothAdapter btAdapter) {
    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    List<GBDevice> result = new ArrayList<>(pairedDevices.size());
    DeviceHelper deviceHelper = DeviceHelper.getInstance();
    for (BluetoothDevice pairedDevice : pairedDevices) {
        if (pairedDevice.getName() != null && (pairedDevice.getName().startsWith("Pebble-LE ") || pairedDevice.getName().startsWith("Pebble Time LE "))) {
            // ignore LE Pebble (this is part of the main device now (volatileAddress)
            continue;
        }
        GBDevice device = deviceHelper.toSupportedDevice(pairedDevice);
        if (device != null) {
            result.add(device);
        }
    }
    return result;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) ArrayList(java.util.ArrayList) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 37 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Gadgetbridge by Freeyourgadget.

the class BluetoothConnectReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
        return;
    }
    LOG.info("got connection attempt");
    GBDevice gbDevice = service.getGBDevice();
    if (gbDevice != null) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (device.getAddress().equals(gbDevice.getAddress()) && gbDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
            LOG.info("will connect to " + gbDevice.getName());
            GBApplication.deviceService().connect();
        } else {
            LOG.info("won't connect to " + device.getAddress() + "(" + device.getName() + ")");
        }
    }
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 38 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Gadgetbridge by Freeyourgadget.

the class MiBandPairingActivity method performBluetoothPair.

protected void performBluetoothPair(GBDeviceCandidate deviceCandidate) {
    BluetoothDevice device = deviceCandidate.getDevice();
    int bondState = device.getBondState();
    if (bondState == BluetoothDevice.BOND_BONDED) {
        GB.toast(getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
        performApplicationLevelPair();
        return;
    }
    bondingMacAddress = device.getAddress();
    if (bondState == BluetoothDevice.BOND_BONDING) {
        GB.toast(this, getString(R.string.pairing_in_progress, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
        return;
    }
    GB.toast(this, getString(R.string.pairing_creating_bond_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.INFO);
    if (!device.createBond()) {
        GB.toast(this, getString(R.string.pairing_unable_to_pair_with, device.getName(), bondingMacAddress), Toast.LENGTH_LONG, GB.ERROR);
    }
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice)

Example 39 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Gadgetbridge by Freeyourgadget.

the class BtLEQueue method connect.

/**
     * Connects to the given remote device. Note that this does not perform any device
     * specific initialization. This should be done in the specific {@link DeviceSupport}
     * class.
     *
     * @return <code>true</code> whether the connection attempt was successfully triggered and <code>false</code> if that failed or if there is already a connection
     */
public boolean connect() {
    if (isConnected()) {
        LOG.warn("Ingoring connect() because already connected.");
        return false;
    }
    synchronized (mGattMonitor) {
        if (mBluetoothGatt != null) {
            // Tribal knowledge says you're better off not reusing existing BluetoothGatt connections,
            // so create a new one.
            LOG.info("connect() requested -- disconnecting previous connection: " + mGbDevice.getName());
            disconnect();
        }
    }
    LOG.info("Attempting to connect to " + mGbDevice.getName());
    mBluetoothAdapter.cancelDiscovery();
    BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mGbDevice.getAddress());
    synchronized (mGattMonitor) {
        // connectGatt with true doesn't really work ;( too often connection problems
        mBluetoothGatt = remoteDevice.connectGatt(mContext, false, internalGattCallback);
    }
    boolean result = mBluetoothGatt != null;
    if (result) {
        setDeviceConnectionState(State.CONNECTING);
    }
    return result;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice)

Example 40 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project android_frameworks_base by DirtyUnicorns.

the class ScanResultTest method testScanResultParceling.

/**
     * Test read and write parcel of ScanResult
     */
@SmallTest
public void testScanResultParceling() {
    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("01:02:03:04:05:06");
    byte[] scanRecord = new byte[] { 1, 2, 3 };
    int rssi = -10;
    long timestampMicros = 10000L;
    ScanResult result = new ScanResult(device, ScanRecord.parseFromBytes(scanRecord), rssi, timestampMicros);
    Parcel parcel = Parcel.obtain();
    result.writeToParcel(parcel, 0);
    // Need to reset parcel data position to the beginning.
    parcel.setDataPosition(0);
    ScanResult resultFromParcel = ScanResult.CREATOR.createFromParcel(parcel);
    assertEquals(result, resultFromParcel);
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) Parcel(android.os.Parcel) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

BluetoothDevice (android.bluetooth.BluetoothDevice)101 Intent (android.content.Intent)13 BluetoothAdapter (android.bluetooth.BluetoothAdapter)12 CachedBluetoothDevice (com.android.settingslib.bluetooth.CachedBluetoothDevice)11 ParcelUuid (android.os.ParcelUuid)9 RemoteException (android.os.RemoteException)9 ScanFilter (android.bluetooth.le.ScanFilter)5 Parcel (android.os.Parcel)5 SmallTest (android.test.suitebuilder.annotation.SmallTest)5 IOException (java.io.IOException)5 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)5 IntentFilter (android.content.IntentFilter)4 MidiDeviceInfo (android.media.midi.MidiDeviceInfo)4 View (android.view.View)4 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2