Search in sources :

Example 56 with BluetoothDevice

use of android.bluetooth.BluetoothDevice 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 57 with BluetoothDevice

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

the class DeviceHelper method removeBond.

/**
     * Attempts to removing the bonding with the given device. Returns true
     * if bonding was supposedly successful and false if anything went wrong
     * @param device
     * @return
     */
public boolean removeBond(GBDevice device) throws GBException {
    BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
    if (defaultAdapter != null) {
        BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress());
        if (remoteDevice != null) {
            try {
                Method method = BluetoothDevice.class.getMethod("removeBond", (Class[]) null);
                Object result = method.invoke(remoteDevice, (Object[]) null);
                return Boolean.TRUE.equals(result);
            } catch (Exception e) {
                throw new GBException("Error removing bond to device: " + device, e);
            }
        }
    }
    return false;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) Method(java.lang.reflect.Method) BluetoothAdapter(android.bluetooth.BluetoothAdapter) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 58 with BluetoothDevice

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

the class PebbleIoThread method connect.

@Override
protected boolean connect() {
    String deviceAddress = gbDevice.getAddress();
    GBDevice.State originalState = gbDevice.getState();
    gbDevice.setState(GBDevice.State.CONNECTING);
    gbDevice.sendDeviceUpdateIntent(getContext());
    try {
        // contains only one ":"? then it is addr:port
        int firstColon = deviceAddress.indexOf(":");
        if (firstColon == deviceAddress.lastIndexOf(":")) {
            mIsTCP = true;
            InetAddress serverAddr = InetAddress.getByName(deviceAddress.substring(0, firstColon));
            mTCPSocket = new Socket(serverAddr, Integer.parseInt(deviceAddress.substring(firstColon + 1)));
            mInStream = mTCPSocket.getInputStream();
            mOutStream = mTCPSocket.getOutputStream();
        } else {
            mIsTCP = false;
            if (gbDevice.getVolatileAddress() != null && prefs.getBoolean("pebble_force_le", false)) {
                deviceAddress = gbDevice.getVolatileAddress();
            }
            BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(deviceAddress);
            if (btDevice.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                LOG.info("This is a Pebble 2 or Pebble-LE/Pebble Time LE, will use BLE");
                mInStream = new PipedInputStream();
                mOutStream = new PipedOutputStream();
                mPebbleLESupport = new PebbleLESupport(this.getContext(), btDevice, (PipedInputStream) mInStream, (PipedOutputStream) mOutStream);
            } else {
                ParcelUuid[] uuids = btDevice.getUuids();
                if (uuids == null) {
                    return false;
                }
                for (ParcelUuid uuid : uuids) {
                    LOG.info("found service UUID " + uuid);
                }
                mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
                mBtSocket.connect();
                mInStream = mBtSocket.getInputStream();
                mOutStream = mBtSocket.getOutputStream();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        gbDevice.setState(originalState);
        gbDevice.sendDeviceUpdateIntent(getContext());
        mInStream = null;
        mOutStream = null;
        mBtSocket = null;
        return false;
    }
    mPebbleProtocol.setForceProtocol(prefs.getBoolean("pebble_force_protocol", false));
    mIsConnected = true;
    write(mPebbleProtocol.encodeFirmwareVersionReq());
    gbDevice.setState(GBDevice.State.CONNECTED);
    gbDevice.sendDeviceUpdateIntent(getContext());
    return true;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothDevice(android.bluetooth.BluetoothDevice) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) PebbleLESupport(nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.ble.PebbleLESupport) InetAddress(java.net.InetAddress) BluetoothSocket(android.bluetooth.BluetoothSocket) Socket(java.net.Socket)

Example 59 with BluetoothDevice

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

the class LiveviewIoThread method connect.

@Override
protected boolean connect() {
    GBDevice.State originalState = gbDevice.getState();
    setUpdateState(GBDevice.State.CONNECTING);
    try {
        BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(gbDevice.getAddress());
        ParcelUuid[] uuids = btDevice.getUuids();
        if (uuids == null) {
            return false;
        }
        for (ParcelUuid uuid : uuids) {
            LOG.info("found service UUID " + uuid);
        }
        mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
        mBtSocket.connect();
        mInStream = mBtSocket.getInputStream();
        mOutStream = mBtSocket.getOutputStream();
        setUpdateState(GBDevice.State.CONNECTED);
    } catch (IOException e) {
        LOG.error("Server socket cannot be started.");
        //LOG.error(e.getMessage());
        setUpdateState(originalState);
        mInStream = null;
        mOutStream = null;
        mBtSocket = null;
        return false;
    }
    write(mLiveviewProtocol.encodeSetTime());
    setUpdateState(GBDevice.State.INITIALIZED);
    return true;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothDevice(android.bluetooth.BluetoothDevice) IOException(java.io.IOException) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Example 60 with BluetoothDevice

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

the class MiBandPairingActivity method pairingFinished.

private void pairingFinished(boolean pairedSuccessfully, GBDeviceCandidate candidate) {
    LOG.debug("pairingFinished: " + pairedSuccessfully);
    if (!isPairing) {
        // already gone?
        return;
    }
    isPairing = false;
    AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(this), mPairingReceiver);
    AndroidUtils.safeUnregisterBroadcastReceiver(this, mBondingReceiver);
    if (pairedSuccessfully) {
        // remember the device since we do not necessarily pair... temporary -- we probably need
        // to query the db for available devices in ControlCenter. But only remember un-bonded
        // devices, as bonded devices are displayed anyway.
        String macAddress = deviceCandidate.getMacAddress();
        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddress);
        if (device != null && device.getBondState() == BluetoothDevice.BOND_NONE) {
            Prefs prefs = GBApplication.getPrefs();
            prefs.getPreferences().edit().putString(MiBandConst.PREF_MIBAND_ADDRESS, macAddress).apply();
        }
        Intent intent = new Intent(this, ControlCenterv2.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    finish();
}
Also used : ControlCenterv2(nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2) BluetoothDevice(android.bluetooth.BluetoothDevice) Intent(android.content.Intent) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs)

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