Example usage for android.bluetooth BluetoothDevice ACTION_FOUND

List of usage examples for android.bluetooth BluetoothDevice ACTION_FOUND

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice ACTION_FOUND.

Prototype

String ACTION_FOUND

To view the source code for android.bluetooth BluetoothDevice ACTION_FOUND.

Click Source Link

Document

Broadcast Action: Remote device discovered.

Usage

From source file:com.example.mego.adas.bluetooth.ConnectFragment.java

/**
 * to set the BroadCaster Receiver/*from  www  . j a v a  2  s . c o  m*/
 */
private void setBroadCastReceiver() {
    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    getActivity().registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    getActivity().registerReceiver(mReceiver, filter);
}

From source file:activities.GatewayActivity.java

@Override
public void onResume() {
    super.onResume();
    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    IntentFilter filter3 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    IntentFilter filter4 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    registerReceiver(mReceiver, filter2); // Don't forget to unregister during onDestroy
    registerReceiver(mReceiver, filter3); // Don't forget to unregister during onDestroy
    registerReceiver(mReceiver, filter4); // Don't forget to unregister during onDestroy
}

From source file:gowtham.com.desknote.MyListener.java

@Override
public void onCreate() {
    super.onCreate();
    IntentFilter filterDeviceFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    //IntentFilter filterDisconnect = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);

    registerReceiver(deviceConnectionReceiver, filterDeviceFound);
    //registerReceiver(deviceConnectionReceiver, filterDisconnect);
}

From source file:com.piusvelte.taplock.client.core.TapLockService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            if (state == BluetoothAdapter.STATE_ON) {
                if (mStartedBT) {
                    if (mUIInterface != null) {
                        try {
                            mUIInterface.setMessage("Bluetooth enabled");
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }//from  w  ww .j a  v a2 s.co  m
                    }
                    if ((mQueueAddress != null) && (mQueueState != null))
                        requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                    else if (mRequestDiscovery && !mBtAdapter.isDiscovering())
                        mBtAdapter.startDiscovery();
                    else if (mUIInterface != null) {
                        try {
                            mUIInterface.setBluetoothEnabled();
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setMessage("Bluetooth disabled");
                    } catch (RemoteException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
                stopThreads();
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // connect if configured
                String address = device.getAddress();
                for (JSONObject deviceJObj : mDevices) {
                    try {
                        if (deviceJObj.getString(KEY_ADDRESS).equals(address)) {
                            // if queued
                            mDeviceFound = (mQueueAddress != null) && mQueueAddress.equals(address)
                                    && (mQueueState != null);
                            break;
                        }
                    } catch (JSONException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            } else if (mRequestDiscovery && (mUIInterface != null)) {
                String unpairedDevice = TapLock
                        .createDevice(device.getName(), device.getAddress(), DEFAULT_PASSPHRASE).toString();
                try {
                    mUIInterface.setUnpairedDevice(unpairedDevice);
                } catch (RemoteException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (mDeviceFound) {
                requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                mDeviceFound = false;
            } else if (mRequestDiscovery) {
                mRequestDiscovery = false;
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setDiscoveryFinished();
                    } catch (RemoteException e) {
                        Log.e(TAG, e.toString());
                    }
                }
            }
        } else if (ACTION_TOGGLE.equals(action) && intent.hasExtra(EXTRA_DEVICE_ADDRESS)) {
            String address = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
            requestWrite(address, ACTION_TOGGLE, null);
        } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            // create widget
            if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (intent.hasExtra(EXTRA_DEVICE_NAME)) {
                    // add a widget
                    String deviceName = intent.getStringExtra(EXTRA_DEVICE_NAME);
                    for (int i = 0, l = mDevices.size(); i < l; i++) {
                        String name = null;
                        try {
                            name = mDevices.get(i).getString(KEY_NAME);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        if ((name != null) && name.equals(deviceName)) {
                            JSONObject deviceJObj = mDevices.remove(i);
                            JSONArray widgetsJArr;
                            if (deviceJObj.has(KEY_WIDGETS)) {
                                try {
                                    widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                                } catch (JSONException e) {
                                    widgetsJArr = new JSONArray();
                                }
                            } else
                                widgetsJArr = new JSONArray();
                            widgetsJArr.put(appWidgetId);
                            try {
                                deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            mDevices.add(i, deviceJObj);
                            TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                            break;
                        }
                    }
                }
                buildWidget(appWidgetId);
            } else if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
                int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null) {
                    for (int appWidgetId : appWidgetIds)
                        buildWidget(appWidgetId);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            Log.d(TAG, "delete appWidgetId: " + appWidgetId);
            for (int i = 0, l = mDevices.size(); i < l; i++) {
                JSONObject deviceJObj = mDevices.get(i);
                if (deviceJObj.has(KEY_WIDGETS)) {
                    JSONArray widgetsJArr = null;
                    try {
                        widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (widgetsJArr != null) {
                        boolean wasUpdated = false;
                        JSONArray newWidgetsJArr = new JSONArray();
                        for (int widgetIdx = 0, wdigetsLen = widgetsJArr
                                .length(); widgetIdx < wdigetsLen; widgetIdx++) {
                            int widgetId;
                            try {
                                widgetId = widgetsJArr.getInt(widgetIdx);
                            } catch (JSONException e) {
                                widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                                e.printStackTrace();
                            }
                            Log.d(TAG, "eval widgetId: " + widgetId);
                            if ((widgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                                    && (widgetId == appWidgetId)) {
                                Log.d(TAG, "skip: " + widgetId);
                                wasUpdated = true;
                            } else {
                                Log.d(TAG, "include: " + widgetId);
                                newWidgetsJArr.put(widgetId);
                            }
                        }
                        if (wasUpdated) {
                            try {
                                deviceJObj.put(KEY_WIDGETS, newWidgetsJArr);
                                mDevices.remove(i);
                                mDevices.add(i, deviceJObj);
                                TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE),
                                        mDevices);
                                Log.d(TAG, "stored: " + deviceJObj.toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    JSONArray widgetsJArr = new JSONArray();
                    try {
                        deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                        mDevices.remove(i);
                        mDevices.add(i, deviceJObj);
                        TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return START_STICKY;
}

From source file:org.jonblack.bluetrack.services.BluetoothLogService.java

@Override
public void onCreate() {
    Log.d(TAG, "Creating BluetoothLogService");

    super.onCreate();

    mLocalBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // Bluetooth should be available and enabled at this point.
    assert (mLocalBtAdapter != null);
    assert (mLocalBtAdapter.isEnabled());

    // Register broadcast receiver for finding bluetooth devices.
    registerReceiver(mBtDiscoveryReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

    // Register broadcast receiver for when discovery finished.
    registerReceiver(mBtDiscoveryReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

    // Setup the periodic handler which will do the discovery
    mPeriodicEventHandler = new Handler();
}

From source file:io.github.msc42.masterthemaze.SearchDeviceActivity.java

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);

    if (canOnResumeStartRepeatedDiscovery) {
        mDiscoveryRepeatHandler.removeCallbacks(mRepeatedDiscoveryRunnable);
        mDiscoveryRepeatHandler.postDelayed(mRepeatedDiscoveryRunnable, 0);
    }/*w w  w  . ja  v a2 s.  c  om*/
}

From source file:ac.robinson.bettertogether.hotspot.HotspotManagerService.java

@SuppressFBWarnings("REC_CATCH_EXCEPTION")
@Nullable/* w w w  . jav a  2 s  . c o  m*/
@Override
public IBinder onBind(Intent intent) {
    if (!mIsBound) {
        //TODO: check mBluetoothAdapter not null and/or check bluetooth is available - BluetoothUtils.isBluetoothAvailable()
        mBluetoothAdapter = BluetoothUtils.getBluetoothAdapter(HotspotManagerService.this);
        mOriginalBluetoothStatus = mBluetoothAdapter.isEnabled();
        mOriginalBluetoothName = mBluetoothAdapter.getName();

        //TODO: check mWifiManager not null and/or check bluetooth is available - WifiUtils.isWifiAvailable()
        // note we need the WifiManager for connecting to other hotspots regardless of whether we can create our own
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        // try to get the original state to restore later
        int wifiState = mWifiManager.getWifiState();
        switch (wifiState) {
        case WifiManager.WIFI_STATE_ENABLED:
        case WifiManager.WIFI_STATE_ENABLING:
            mOriginalWifiStatus = true;
            break;
        case WifiManager.WIFI_STATE_DISABLED:
        case WifiManager.WIFI_STATE_DISABLING:
        case WifiManager.WIFI_STATE_UNKNOWN:
            mOriginalWifiStatus = false;
            break;
        default:
            break;
        }

        // try to save the existing hotspot state
        if (CREATE_WIFI_HOTSPOT_SUPPORTED) {
            try {
                // TODO: is it possible to save/restore the original password? (WifiConfiguration doesn't hold the password)
                WifiConfiguration wifiConfiguration = WifiUtils.getWifiHotspotConfiguration(mWifiManager);
                mOriginalHotspotConfiguration = new ConnectionOptions();
                mOriginalHotspotConfiguration.mName = wifiConfiguration.SSID;
            } catch (Exception ignored) {
                // note - need to catch Exception rather than ReflectiveOperationException due to our API level (requires 19)
            }
        }

        // set up background thread for message sending - see: https://medium.com/@ali.muzaffar/dc8bf1540341
        mMessageThread = new HandlerThread("BTMessageThread");
        mMessageThread.start();
        mMessageThreadHandler = new Handler(mMessageThread.getLooper());

        // set up listeners for network/bluetooth state changes
        IntentFilter intentFilter = new IntentFilter();
        if (CREATE_WIFI_HOTSPOT_SUPPORTED) {
            intentFilter.addAction(HOTSPOT_STATE_FILTER); // Wifi hotspot states
        }
        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); // Wifi on/off
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); // network connection/disconnection
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); // Bluetooth on/off
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND); // Bluetooth device found
        registerReceiver(mGlobalBroadcastReceiver, intentFilter);

        // listen for messages from our PluginMessageReceiver
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction(PluginIntent.ACTION_MESSAGE_RECEIVED);
        localIntentFilter.addAction(PluginIntent.ACTION_STOP_PLUGIN);
        LocalBroadcastManager.getInstance(HotspotManagerService.this).registerReceiver(mLocalBroadcastReceiver,
                localIntentFilter);

        // listen for EventBus events (from wifi/bluetooth servers)
        if (!EventBus.getDefault().isRegistered(HotspotManagerService.this)) {
            EventBus.getDefault().register(HotspotManagerService.this);
        }

        mIsBound = true;
    }

    return mMessenger.getBinder();
}

From source file:org.chromium.ChromeBluetooth.java

private void registerAdapterStateReceiver() {
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    webView.getContext().registerReceiver(adapterStateReceiver, filter);
}

From source file:com.tiagohm.bluedroid.BlueDroid.java

/**
 * Executa a descoberta de dispositivos Bluetooth.
 *///from w w  w  . j a va  2 s.  co  m
public void doDiscovery(Activity activity) {
    Log.d(TAG, "BlueDroid.doDiscovery()");

    final int hasPermission = ActivityCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_COARSE_LOCATION);

    if (hasPermission != PackageManager.PERMISSION_GRANTED) {
        if (activity != null) {
            ActivityCompat.requestPermissions(activity,
                    new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
                    REQUEST_COARSE_LOCATION_PERMISSIONS);
        }

        return;
    }

    if (isDiscovering()) {
        mContext.unregisterReceiver(mReceiver);
        cancelDiscovery();
    }

    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

    startDiscovery();
}

From source file:com.pairbluetooth.mariachiio.dagorik.pairbluetooth.test_bluetooth.test_bluetooth.utils.BlueDroid.java

/**
 * Executa a descoberta de dispositivos Bluetooth.
 *///from ww w. ja  v  a2 s.c  om
public void doDiscovery(Activity activity) {
    Log.d(TAG, "BlueDroid.doDiscovery()");

    final int hasPermission = ActivityCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_COARSE_LOCATION);

    if (hasPermission != PackageManager.PERMISSION_GRANTED) {
        if (activity != null) {
            ActivityCompat.requestPermissions(activity,
                    new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                    REQUEST_COARSE_LOCATION_PERMISSIONS);
        }

        return;
    }

    if (isDiscovering()) {
        mContext.unregisterReceiver(mReceiver);
        cancelDiscovery();
    }

    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
    mContext.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

    startDiscovery();
}