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:Main.java

/**
 * register bluetooth receiver//from w ww.j a va  2  s. c  o m
 *
 * @param receiver bluetooth broadcast receiver
 * @param activity activity
 */
public static void registerBluetoothReceiver(BroadcastReceiver receiver, Activity activity) {
    if (null == receiver || null == activity) {
        return;
    }
    IntentFilter intentFilter = new IntentFilter();
    //start discovery
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    //finish discovery
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    //bluetooth status change
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    //found device
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    //bond status change
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    //pairing device
    intentFilter.addAction("android.bluetooth.device.action.PAIRING_REQUEST");
    activity.registerReceiver(receiver, intentFilter);
}

From source file:org.libreoffice.impressremote.communication.BluetoothServersFinder.java

@Override
public void startSearch() {
    if (btAdapter == null) {
        return;//from w w w .j  a v  a 2  s.  c  om
    }
    IntentFilter aBluetoothActionsFilter = new IntentFilter();
    aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    aBluetoothActionsFilter.addAction(BluetoothDevice.ACTION_FOUND);

    mContext.registerReceiver(this, aBluetoothActionsFilter);
    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            addServer(device);
        }
    } else {
        if (btAdapter.isDiscovering()) {
            return;
        }
        btAdapter.startDiscovery();
    }
}

From source file:io.v.positioning.BluetoothPositionActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetoothposition);
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new View.OnClickListener() {
        @Override/* ww w  .  j av a2  s .c o  m*/
        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });
    mRecordedDevices = new ArrayAdapter<String>(this, R.layout.device_name);
    ListView recordedDevicesListView = (ListView) findViewById(R.id.recorded_devices);
    recordedDevicesListView.setAdapter(mRecordedDevices);
    this.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:uk.ac.horizon.ubihelper.service.channel.BluetoothDiscoveryChannel.java

/**
 * @param handler// w ww.  ja v  a2 s  . c  o  m
 * @param name
 */
public BluetoothDiscoveryChannel(Service service, Handler handler, String name) {
    super(handler, name);
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    this.service = service;
    if (bluetooth == null)
        Log.w(TAG, "No BluetoothAdapter");
    if (!bluetooth.isEnabled()) {
        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        service.sendBroadcast(i);
    }
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
    this.service.registerReceiver(receiver, filter);
}

From source file:com.example.android.classical.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    GlobalVar.currentDeviceMacAddress = wm.getConnectionInfo().getMacAddress();
    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    registerReceiver(mReceiver, intentFilter);
    intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(mReceiver, intentFilter);
    intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, intentFilter);

    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        BluetoothChatFragment fragment = new BluetoothChatFragment();
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();/*from w  ww  .j a v a 2 s  .  co  m*/
    }

    //new Thread(){
    //    @Override public void run() {
    //        while(true) {
    //            Intent intentService = new Intent(MainActivity.this, TaskIntentService.class);
    //            intentService.putExtra(TaskIntentService.MESSAGE_TYPE, MessageType.toInt(MessageType.BALL_EVENT));
    //            intentService.putExtra(TaskIntentService.EVENT_TYPE, EventType.BALL_CONNECTED);
    //            startService(intentService);
    //            SystemClock.sleep(2000);
    //        }
    //    }
    //}.start();

}

From source file:org.libreoffice.impressremote.communication.BluetoothServersFinder.java

@Override
public void onReceive(Context aContext, Intent aIntent) {
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) {
        switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
        case BluetoothAdapter.STATE_ON:
            startSearch();/*w w  w . ja va 2s.  c o  m*/
            return;

        default:
            return;
        }
    }

    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) {
        LocalBroadcastManager.getInstance(mContext)
                .sendBroadcast(new Intent(Intents.Actions.BT_DISCOVERY_CHANGED));
        return;
    }

    if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
        BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        addServer(aBluetoothDevice);
    }
}

From source file:com.phonegap.plugin.bluetooth.BluetoothPlugin.java

@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
    Log.d("BluetoothPlugin", "Plugin Called");
    PluginResult result = null;//from   ww w  . j  ava 2 s.  c  om
    context = this.ctx;

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery starts
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

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

    // Register for broadcasts when connectivity state changes
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);

    Looper.prepare();
    btadapter = BluetoothAdapter.getDefaultAdapter();
    found_devices = new ArrayList<BluetoothDevice>();

    if (ACTION_DISCOVER_DEVICES.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES);

            found_devices.clear();
            discovering = true;

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            Log.i("BluetoothPlugin", "Discovering devices...");
            btadapter.startDiscovery();

            while (discovering) {
            }

            String devicesFound = null;
            int count = 0;
            devicesFound = "[";
            for (BluetoothDevice device : found_devices) {
                Log.i("BluetoothPlugin",
                        device.getName() + " " + device.getAddress() + " " + device.getBondState());
                if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                    devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ,"
                            + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                            + device.getBluetoothClass().getDeviceClass() + "\" }";
                    if (count < found_devices.size() - 1)
                        devicesFound = devicesFound + ",";
                } else
                    Log.i("BluetoothPlugin",
                            device.getName() + " Problems retrieving attributes. Device not added ");
                count++;
            }

            devicesFound = devicesFound + "] ";

            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound);
            result = new PluginResult(Status.OK, devicesFound);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED);

            boolean isEnabled = btadapter.isEnabled();

            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED,
                    "Returning " + "is Bluetooth Enabled? " + isEnabled);
            result = new PluginResult(Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_ENABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT);

            boolean enabled = false;

            Log.d("BluetoothPlugin", "Enabling Bluetooth...");

            if (btadapter.isEnabled()) {
                enabled = true;
            } else {
                enabled = btadapter.enable();
            }

            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled);
            result = new PluginResult(Status.OK, enabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_DISABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT);

            boolean disabled = false;

            Log.d("BluetoothPlugin", "Disabling Bluetooth...");

            if (btadapter.isEnabled()) {
                disabled = btadapter.disable();
            } else {
                disabled = true;
            }

            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled);
            result = new PluginResult(Status.OK, disabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean paired = false;

            Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName()
                    + " and address " + device.getAddress());

            try {
                Method m = device.getClass().getMethod("createBond");
                paired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired);
            result = new PluginResult(Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_UNPAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean unpaired = false;

            Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("removeBond");
                unpaired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired);
            result = new PluginResult(Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES);

            Log.d("BluetoothPlugin", "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i("BluetoothPlugin",
                            device.getName() + " " + device.getAddress() + " " + device.getBondState());

                    if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                        resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ,"
                                + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                                + device.getBluetoothClass().getDeviceClass() + "\" }";
                        if (count < pairedDevices.size() - 1)
                            resultBoundDevices = resultBoundDevices + ",";
                    } else
                        Log.i("BluetoothPlugin",
                                device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices);
            result = new PluginResult(Status.OK, resultBoundDevices);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT);

            boolean stopped = true;

            Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices...");

            if (btadapter.isDiscovering()) {
                Log.i("BluetoothPlugin", "Stop discovery...");
                stopped = btadapter.cancelDiscovery();
                discovering = false;
            }

            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped);
            result = new PluginResult(Status.OK, stopped);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BOUND_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT);
            String addressDevice = arg1.getString(0);
            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState());

            boolean state = false;

            if (device != null && device.getBondState() == 12)
                state = true;
            else
                state = false;

            Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress());

            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state);
            result = new PluginResult(Status.OK, state);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("BluetoothPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}

From source file:com.phonegap.plugin.BluetoothPlugin.java

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    context = this.cordova.getActivity().getBaseContext();

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery starts
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

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

    // Register for broadcasts when connectivity state changes
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);
}

From source file:org.bcsphere.bluetooth.BCBluetooth.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    myContext = cordova.getActivity().getApplicationContext();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    myContext.registerReceiver(receiver, intentFilter);
    sp = myContext.getSharedPreferences("VERSION_OF_API", 1);
    BluetoothDetection.detectionBluetoothAPI(myContext);
    try {//  w  w  w .j  av a 2  s .  co m
        if ((versionOfAPI = sp.getString("API", "no_google")).equals("google")) {
            bluetoothAPI = (IBluetooth) Class.forName("org.bcsphere.bluetooth.BluetoothG43plus").newInstance();
        } else if ((versionOfAPI = sp.getString("API", "no_samsung")).equals("samsung")) {
            bluetoothAPI = (IBluetooth) Class.forName("org.bcsphere.bluetooth.BluetoothSam42").newInstance();
        }
        /*else if ((versionOfAPI = sp.getString("API", "no_htc"))
              .equals("htc")) {
           bluetoothAPI = (IBluetooth) Class.forName(
          "org.bcsphere.bluetooth.BluetoothHTC41").newInstance();
        }*/
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.openracer.remote.DeviceListActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i(LOGTAG, "on-create");

    setContentView(R.layout.activity_device_list);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    setResult(Activity.RESULT_CANCELED);

    Button scanButton = (Button) findViewById(R.id.button1);
    scanButton.setOnClickListener(new OnClickListener() {
        @Override//from   ww w .  j  a  va 2s .  c  o  m
        public void onClick(View v) {

            if (hasBtPerms()) {
                Log.e(LOGTAG, "pushed");
                v.setEnabled(false);
                discoveredDevices.clear();
                doDiscovery();
            }
        }
    });

    pairedDevices = new ArrayAdapter<String>(this, R.layout.btdevice_list_item);
    discoveredDevices = new ArrayAdapter<String>(this, R.layout.btdevice_list_item);

    ListView discoveredListView = (ListView) findViewById(R.id.listView2);
    discoveredListView.setAdapter(discoveredDevices);
    discoveredListView.setOnItemClickListener(deviceClickListener);

    ListView pairedListView = (ListView) findViewById(R.id.listView1);
    pairedListView.setAdapter(pairedDevices);
    pairedListView.setOnItemClickListener(deviceClickListener);

    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        Log.e(LOGTAG, "No bluetooth adapter");
    }

    if (!hasBtPerms()) {
        Button button = (Button) findViewById(R.id.button1);
        button.setText("No Bluetooth permissions");
        button.setEnabled(false);
    }

    if (btAdapter != null) {
        if (!btAdapter.isEnabled()) {
            Button button = (Button) findViewById(R.id.button1);
            button.setText("Bluetooth not enabled");
            button.setEnabled(false);
        }
        Set<BluetoothDevice> sysPairedDevices = btAdapter.getBondedDevices();
        if (sysPairedDevices.isEmpty()) {
            pairedDevices.add("No paired devices.");
        } else {
            for (BluetoothDevice pDev : sysPairedDevices) {
                pairedDevices.add(pDev.getName() + "\n" + pDev.getAddress());
            }
        }
    } else {
        Button button = (Button) findViewById(R.id.button1);
        button.setText("No Bluetooth device");
        button.setEnabled(false);
    }

}