Search in sources :

Example 31 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class BluetoothPairingRequest method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
        // convert broadcast intent into activity intent (same action string)
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
        Intent pairingIntent = new Intent();
        pairingIntent.setClass(context, BluetoothPairingDialog.class);
        pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type);
        if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY || type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
            int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
            pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pairingKey);
        }
        pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        String deviceAddress = device != null ? device.getAddress() : null;
        String deviceName = device != null ? device.getName() : null;
        boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(context, deviceAddress, deviceName);
        if (powerManager.isInteractive() && shouldShowDialog) {
            // Since the screen is on and the BT-related activity is in the foreground,
            // just open the dialog
            context.startActivity(pairingIntent);
        } else {
            // Put up a notification that leads to the dialog
            Resources res = context.getResources();
            Notification.Builder builder = new Notification.Builder(context).setSmallIcon(android.R.drawable.stat_sys_data_bluetooth).setTicker(res.getString(R.string.bluetooth_notif_ticker));
            PendingIntent pending = PendingIntent.getActivity(context, 0, pairingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            if (TextUtils.isEmpty(name)) {
                name = device != null ? device.getAliasName() : context.getString(android.R.string.unknownName);
            }
            builder.setContentTitle(res.getString(R.string.bluetooth_notif_title)).setContentText(res.getString(R.string.bluetooth_notif_message, name)).setContentIntent(pending).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setColor(context.getColor(com.android.internal.R.color.system_notification_accent_color));
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(NOTIFICATION_ID, builder.getNotification());
        }
    } else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {
        Intent pairingIntent = new Intent();
        pairingIntent.setClass(context, BluetoothPairingDialog.class);
        pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pending = PendingIntent.getActivity(context, 0, pairingIntent, PendingIntent.FLAG_NO_CREATE);
        if (pending != null) {
            pending.cancel();
        }
        // Remove the notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(NOTIFICATION_ID);
    } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
        int oldState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
        if ((oldState == BluetoothDevice.BOND_BONDING) && (bondState == BluetoothDevice.BOND_NONE)) {
            // Remove the notification
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(NOTIFICATION_ID);
        }
    }
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) NotificationManager(android.app.NotificationManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) PowerManager(android.os.PowerManager) Resources(android.content.res.Resources) PendingIntent(android.app.PendingIntent)

Example 32 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class DevicePickerFragment method onDeviceBondStateChanged.

public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
    if (bondState == BluetoothDevice.BOND_BONDED) {
        BluetoothDevice device = cachedDevice.getDevice();
        if (device.equals(mSelectedDevice)) {
            sendDevicePickedIntent(device);
            finish();
        }
    }
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) CachedBluetoothDevice(com.android.settingslib.bluetooth.CachedBluetoothDevice)

Example 33 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class DockService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (DEBUG)
        Log.d(TAG, "onStartCommand startId: " + startId + " flags: " + flags);
    if (intent == null) {
        // Nothing to process, stop.
        if (DEBUG)
            Log.d(TAG, "START_NOT_STICKY - intent is null.");
        // NOTE: We MUST not call stopSelf() directly, since we need to
        // make sure the wake lock acquired by the Receiver is released.
        DockEventReceiver.finishStartingService(this, startId);
        return START_NOT_STICKY;
    }
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
        handleBtStateChange(intent, startId);
        return START_NOT_STICKY;
    }
    /*
         * This assumes that the intent sender has checked that this is a dock
         * and that the intent is for a disconnect
         */
    final SharedPreferences prefs = getPrefs();
    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
        BluetoothDevice disconnectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int retryCount = prefs.getInt(KEY_CONNECT_RETRY_COUNT, 0);
        if (retryCount < MAX_CONNECT_RETRY) {
            prefs.edit().putInt(KEY_CONNECT_RETRY_COUNT, retryCount + 1).apply();
            handleUnexpectedDisconnect(disconnectedDevice, mProfileManager.getHeadsetProfile(), startId);
        }
        return START_NOT_STICKY;
    } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
        BluetoothDevice disconnectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int retryCount = prefs.getInt(KEY_CONNECT_RETRY_COUNT, 0);
        if (retryCount < MAX_CONNECT_RETRY) {
            prefs.edit().putInt(KEY_CONNECT_RETRY_COUNT, retryCount + 1).apply();
            handleUnexpectedDisconnect(disconnectedDevice, mProfileManager.getA2dpProfile(), startId);
        }
        return START_NOT_STICKY;
    }
    Message msg = parseIntent(intent);
    if (msg == null) {
        // Bad intent
        if (DEBUG)
            Log.d(TAG, "START_NOT_STICKY - Bad intent.");
        DockEventReceiver.finishStartingService(this, startId);
        return START_NOT_STICKY;
    }
    if (msg.what == MSG_TYPE_DOCKED) {
        prefs.edit().remove(KEY_CONNECT_RETRY_COUNT).apply();
    }
    msg.arg2 = startId;
    processMessage(msg);
    return START_NOT_STICKY;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) CachedBluetoothDevice(com.android.settingslib.bluetooth.CachedBluetoothDevice) Message(android.os.Message) SharedPreferences(android.content.SharedPreferences)

Example 34 with BluetoothDevice

use of android.bluetooth.BluetoothDevice in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class DockService method hasOtherConnectedDevices.

synchronized boolean hasOtherConnectedDevices(BluetoothDevice dock) {
    Collection<CachedBluetoothDevice> cachedDevices = mDeviceManager.getCachedDevicesCopy();
    Set<BluetoothDevice> btDevices = mLocalAdapter.getBondedDevices();
    if (btDevices == null || cachedDevices == null || btDevices.isEmpty()) {
        return false;
    }
    if (DEBUG) {
        Log.d(TAG, "btDevices = " + btDevices.size());
        Log.d(TAG, "cachedDeviceUIs = " + cachedDevices.size());
    }
    for (CachedBluetoothDevice deviceUI : cachedDevices) {
        BluetoothDevice btDevice = deviceUI.getDevice();
        if (!btDevice.equals(dock) && btDevices.contains(btDevice) && deviceUI.isConnected()) {
            if (DEBUG)
                Log.d(TAG, "connected deviceUI = " + deviceUI.getName());
            return true;
        }
    }
    return false;
}
Also used : CachedBluetoothDevice(com.android.settingslib.bluetooth.CachedBluetoothDevice) BluetoothDevice(android.bluetooth.BluetoothDevice) CachedBluetoothDevice(com.android.settingslib.bluetooth.CachedBluetoothDevice)

Example 35 with BluetoothDevice

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

the class ScanFilter method matches.

/**
     * Check if the scan filter matches a {@code scanResult}. A scan result is considered as a match
     * if it matches all the field filters.
     */
public boolean matches(ScanResult scanResult) {
    if (scanResult == null) {
        return false;
    }
    BluetoothDevice device = scanResult.getDevice();
    // Device match.
    if (mDeviceAddress != null && (device == null || !mDeviceAddress.equals(device.getAddress()))) {
        return false;
    }
    ScanRecord scanRecord = scanResult.getScanRecord();
    // Scan record is null but there exist filters on it.
    if (scanRecord == null && (mDeviceName != null || mServiceUuid != null || mManufacturerData != null || mServiceData != null)) {
        return false;
    }
    // Local name match.
    if (mDeviceName != null && !mDeviceName.equals(scanRecord.getDeviceName())) {
        return false;
    }
    // UUID match.
    if (mServiceUuid != null && !matchesServiceUuids(mServiceUuid, mServiceUuidMask, scanRecord.getServiceUuids())) {
        return false;
    }
    // Service data match
    if (mServiceDataUuid != null) {
        if (!matchesPartialData(mServiceData, mServiceDataMask, scanRecord.getServiceData(mServiceDataUuid))) {
            return false;
        }
    }
    // Manufacturer data match.
    if (mManufacturerId >= 0) {
        if (!matchesPartialData(mManufacturerData, mManufacturerDataMask, scanRecord.getManufacturerSpecificData(mManufacturerId))) {
            return false;
        }
    }
    // All filters match.
    return true;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice)

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