0

I have looked at many examples and everything seems to be correct. I have both permissions, check to see if they are activated. I have looked at countless posts on Stackoverflow. I know it is something super tiny that I am missing, like always. I make sure that all the devices I search are visible. I have even double checked with the system bluetooth and it finds it just find. What am I doing wrong. Here is my code.

public class Bluetooth_ListView extends ListActivity
{
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayList<Bluetooth> arrayOfFoundBTDevices;
    private BroadcastReceiver mReceiver;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // Quick permission check
        int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
        permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
        if (permissionCheck != 0) {

            this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
        }
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        mBluetoothAdapter.startDiscovery();

        displayListOfFoundDevices();
    }

    private void displayListOfFoundDevices()
    {
        arrayOfFoundBTDevices = new ArrayList<Bluetooth>();


        // Create a BroadcastReceiver for ACTION_FOUND
        mReceiver = new BroadcastReceiver()
        {

            @Override
            public void onReceive(Context context, Intent intent)
            {
                Log.v("discover_me", "Bluetooth receiving");
                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);

                    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);

                    // Create the device object and add it to the arrayList of devices
                    Bluetooth bluetoothObject = new Bluetooth();
                    bluetoothObject.setBluetooth_name(device.getName());
                    bluetoothObject.setBluetooth_address(device.getAddress());
                    bluetoothObject.setBluetooth_state(device.getBondState());
                    bluetoothObject.setBluetooth_type(device.getType());    
                    bluetoothObject.setBluetooth_uuids(device.getUuids());
                    bluetoothObject.setBluetooth_rssi(rssi);

                    arrayOfFoundBTDevices.add(bluetoothObject);

                    Bluetooth_Adapter adapter = new Bluetooth_Adapter(getApplicationContext(), arrayOfFoundBTDevices);

                    setListAdapter(adapter);
                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mBluetoothAdapter.cancelDiscovery();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mReceiver);
        mBluetoothAdapter.cancelDiscovery();
    }
}

Any advice would be awesome. The array is always blank no matter what. Also, the Log.v inside the onReceive is never called.

srtmonkey
  • 85
  • 9
  • And, what is the issue? The onReceive method is never invoked or the result is an empty array? Another question: is the logging function Log.v working? I remember that sometimes when I was working with android I have some issues with Log function. – Lubo Jul 03 '16 at 21:25
  • It always returns a blank array. The Log.v inside the onReceive never gets called. I had 4 bluetooth devices next to it and none ever were found. It seems like it never detects or receives any information from any device. – srtmonkey Jul 03 '16 at 21:30
  • And what if you first call displayListOfFoundDevices() and just the start the discovery process? – Lubo Jul 03 '16 at 21:31
  • I tried that. I tried just calling displayListOfFoundDevices () and calling start discovery inside the function, but still nothing. – srtmonkey Jul 03 '16 at 21:33
  • But you cant start the discovery inside the function because the function is called if the discovery process find any active bluetooth device. Just try to exchange the order to first call displayListOfFoundDevices(); and then mBluetoothAdapter.startDiscovery(); – Lubo Jul 03 '16 at 21:39
  • Still just displays a blank array. Never even enters in the onReceive. – srtmonkey Jul 03 '16 at 21:44
  • It could be in the permissions then. You are checking the fine_location permission but not permissions for bluetooth. So, did you enabled BLUETOOTH and maybe BLUETOOTH_ADMIN permission? – Lubo Jul 03 '16 at 21:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116322/discussion-between-srtmonkey-and-lubo). – srtmonkey Jul 03 '16 at 21:54

1 Answers1

0

Too much comments. So I decided to put it as an answer.

You need to make your bluetooth devices discoverable in order to make them visible in your list of bluetooth devices found nearby.

If this is a BT Headset, then there's must be some kind of mechanism to force the BT headset to become discoverable. Most of the BT headsets are now not discoverable by default. I had a problem with that and I posted this as an question in SO.

You can see this answer too. Though the answer is not a proper one but I wanted to share my findings. Hope this will help.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98