4

I am trying to scan for bluetooth devices, but the register is never called:

 public class MainActivity extends Activity {


          ArrayList<String> arr_devices = new ArrayList<String>();
          ArrayList<String> arr_in_range = new ArrayList<String>();
          Button btn_devices;
          BluetoothAdapter bluetoothAdapter;
          IntentFilter filter;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            btn_devices = (Button)findViewById(R.id.search);
            btn_devices.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {

                    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
                    if (bluetooth != null)
                    {
                        if (bluetooth.isEnabled())
                        {
                            //this is called
                            bluetoothAdapter.startDiscovery();
                            Log.i("State", bluetoothAdapter.getState() + ""); //12 (STATE_ON)
                            Log.i("Discovery", bluetoothAdapter.isDiscovering() + "");  //FALSE

                            filter = new IntentFilter();
                            filter.addAction(BluetoothDevice.ACTION_FOUND);
                            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
                            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

                           registerReceiver(myreceiver, filter);
                           //this is where the program stops. No more actions are performed
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Bluetooth is not enabled!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });


        }


        @Override
        protected void onDestroy() {

            super.onDestroy();
            unregisterReceiver(myreceiver);
        }


        final BroadcastReceiver myreceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
        //I placed a Log statement here but it doesn't appear in the logcat
           String action = intent.getAction();

           if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
           }
           else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           }

           if(BluetoothDevice.ACTION_FOUND.equals(action)) 
           {
               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
               Log.i("device", device.getName() + "\n" + device.getAddress());
           }

         }};

    }

Bluetooth is enabled. What am I missing?

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Use Log.v statements to ensure that bluetooth isn't null, and bluetooth is showing up as enabled on the device. Also, print out the value of the startDiscovery. – PearsonArtPhoto Nov 25 '12 at 21:01
  • I think you need to put the myreceiver declaration inside of onCreate(). Right now you are instantiating that outside of the lifecycle methods but still in the Activity. I think it will be null when you register it so it will seem like you aren't getting callbacks. – FoamyGuy Nov 25 '12 at 21:06
  • @PearsonArtPhoto: I am using log statements, but removed them to provide a transparent code. – erdomester Nov 25 '12 at 21:13
  • Where does it stop then? Can you edit your code with comments to show where it stopped? – PearsonArtPhoto Nov 25 '12 at 21:15
  • Hm. Right after calling startDiscovery() I logged out the value of bluetoothAdapter.isDiscovering() which is false. Any ideas? I added some comments to the code – erdomester Nov 25 '12 at 21:17
  • What was the return on `bluetoothAdapter.startDiscovery()`? – PearsonArtPhoto Nov 25 '12 at 21:27
  • Also, do a `bluetoothAdapter.getState()`, and verify that it is STATE_ON (12/0x0c) – PearsonArtPhoto Nov 25 '12 at 21:30
  • It returns 12, see code. What do you mean by return on `bluetoothAdapter.startDiscovery()`? – erdomester Nov 25 '12 at 21:45

2 Answers2

2

Try to put your broadcast receiver before

 bluetoothAdapter.startDiscovery();

Cause you startDiscovery and after you listen broadcastListener.

jaumard
  • 8,202
  • 3
  • 40
  • 63
  • sometimes bluetooth stops working. it doesn't force close, but the value of bluetoothAdapter.isDiscovering() returns false. No matter if I restart the app, I need to turn off and on bluetooth to make it work again. Any ideas? – erdomester Nov 26 '12 at 17:36
  • startDiscovery launch device to search new BT devices but when it have done isDiscovering return false you have to call again startDiscovery. Look doc http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html – jaumard Nov 26 '12 at 17:48
  • I am familiar with this. But I keep pressing btn_devices the value returns false. If you take another look at the code you'll see the startDiscovery() method is inside the btn_devices onClickListener, so the discovery should be started again.. – erdomester Nov 26 '12 at 18:02
  • Sorry maybe take a look at this http://stackoverflow.com/questions/8837386/setting-discoverable-mode-for-bluetooth-to-always-on-seam-to-fail – jaumard Nov 26 '12 at 18:06
1

Why do you have two bluetooth adapters set up? The system might be getting confused with both of them, you should only need one. I recommend you remove the line listed below, and change all references from bluetooth to bluetoothAdapter.

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

I suspect what is happening is that this is returning null, as you already have a copy of the receiver, or something similar to that.

Also, make sure you have added the permissions to the manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

All of that failing, you might try rebooting your phone. At least on my phone, bluetooth gets mucked up sometimes.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142