Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android search for bluetooth devices

I am trying to find the available Bluetooth devices.

This is my OnClickListener which is called when the user tries to search for the available devices.

View.OnClickListener OnSearchDevices = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(context, "Search Devices", Toast.LENGTH_LONG).show();
            Log.d("Discovery", "Started");
            listOfDevices.clear();
            label.setText("Searching Available Devices...");
            label.setEnabled(false);
        }
    };

I have also registered a BroadcastReceiver.

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d("DeviceList" , device.getName() + "\n" + device.getAddress());
                MyBluetoothDevice tempDevice = new MyBluetoothDevice();
                tempDevice.setDeviceAddress(device.getAddress());
                tempDevice.setDeviceName(device.getName());
                listOfDevices.add(tempDevice);
                mListAdapter.notifyDataSetChanged();
                //  discovery is finished
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

                Log.d("Discovery","Finished");
                label.setEnabled(true);
                if(listOfDevices.size() == 0)
                {
                    label.setText("No Devices Available!");
                    label.setTextColor(Color.parseColor("#FF0000"));

                }
                else
                {
                    label.setText("Available Devices");
                }

            }
        }
    };

But nothing happens. It does not show anything. Please help.

like image 657
user2477164 Avatar asked Jun 12 '13 06:06

user2477164


People also ask

Can you search up Bluetooth devices?

Using the BluetoothAdapter , you can find remote Bluetooth devices either through device discovery or by querying the list of paired devices. Make sure you have the appropriate Bluetooth permissions and set up your app for Bluetooth before attempting to find Bluetooth devices.

Why is my Bluetooth not searching for devices?

If your Bluetooth devices won't connect, it's likely because the devices are out of range, or aren't in pairing mode. If you're having persistent Bluetooth connection problems, try resetting your devices, or having your phone or tablet "forget" the connection.

How do I find my Bluetooth list on Android?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.


1 Answers

Looks like you are missing the call to mBluetoothAdapter.startDiscovery().

That would explain why you are not getting any results, because the adapter doesn't even start searching for the devices.

Your code looks fine to me otherwise.

like image 92
Swayam Avatar answered Sep 28 '22 01:09

Swayam