1

I made a helper class that extends BroadcastReceiver to listen for BluetoothDevice found and discovery finish intents. I have two activities which use this class by passing a handler. The handler receives messages as per the intent. I instantiate the class and registerReceiver like this:

From mainActivity:

deviceHelper=new DevicesHelper(myHandler,DevicesHelper.REQUEST_DETECT_DEVICES_IN_RANGE);

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

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);

if(myBluetoothAdapter.isDiscovering())myBluetoothAdapter.cancelDiscovery();
myBluetoothAdapter.startDiscovery(); 

From ListActivity:

deviceHelper=new DevicesHelper(deviceListHandler,DevicesHelper.REQUEST_DEVICE_LIST_ACTIVITY);

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

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(deviceHelper, filter);

DeviceHelper class:

public class DevicesHelper extends BroadcastReceiver {

    public final static int REQUEST_DEVICE_LIST_ACTIVITY=1;
    public final static int REQUEST_DETECT_DEVICES_IN_RANGE=2;


    Handler myHandler;      
    int requestCode;

    public DevicesHelper(){

    }
    public DevicesHelper(Handler handler,int requestCode){
            this.requestCode=requestCode;
            myHandler=handler;

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();

        // When discovery finds a device
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            switch(requestCode){
            case REQUEST_DEVICE_LIST_ACTIVITY: 
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    //newDevicesCount++;
                    String[] deviceInfo={device.getName(),device.getAddress()};

                    myHandler.obtainMessage(DeviceListActivity.MESSAGE_NEW_DEVICE,deviceInfo);
                };


                break;

            case REQUEST_DETECT_DEVICES_IN_RANGE:

                String[] deviceInfo={device.getName(),device.getAddress()};

                myHandler.obtainMessage(StripChartRecorder.MESSAGE_NEW_DEVICE,deviceInfo);
                break;

            }


            // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            myHandler.obtainMessage(StripChartRecorder.MESSAGE_DISCOVERY_FINISHED);

        }
    }

Handler:

private final Handler myHandler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case MESSAGE_NEW_DEVICE:
                    doOtherStuff();

                case MESSAGE_DISCOVERY_FINISHED:
                    dostuff();
            }
            break;
}}

Am I missing something here? I appreciate any help.

Kirk
  • 16,182
  • 20
  • 80
  • 112
mojorisinify
  • 377
  • 5
  • 22
  • Do you have receiver class declaration "DevicesHelper" in Manifest? What exactly is not working? Can you hit a breakpoint within receiver? – Maxim Aug 08 '12 at 16:09
  • @mojorisinify. Firstly create one `IntentFilter` and use `addAction` to add any extra intent actions. – techi.services Aug 08 '12 at 16:23
  • @Maxim I have not declared the receiver in manifest, its registered in code. DevicesHelper is not a activity so I don't think it has to be declared in manifest. Reference : [link](http://stackoverflow.com/questions/6882476/android-communicate-between-activity-and-broadcast-receiver). DevicesHelper gets instantiated, registered but it is not getting called when bluetooth devices are in range. – mojorisinify Aug 08 '12 at 20:37
  • @techiServices: Yes, IntentFilters are registered well. Look up. – mojorisinify Aug 08 '12 at 20:38
  • @mojorisinify. You misunderstand. You use one `IntentFilter` and one `registerReceiver`. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(deviceHelper, filter); – techi.services Aug 08 '12 at 20:44
  • @techiServices Tried you idea. No luck. – mojorisinify Aug 08 '12 at 20:58
  • @mojorisinify. You have a more major flaw in your code then. It's hard to tell from what you have posted. Do you get any errors in LogCat? Try debugging and stepping through the code that may be at fault and to pin point the area, log some messages to the LogCat when receiving `Intents` and `Messages`. – techi.services Aug 08 '12 at 21:07

2 Answers2

1

You need to sendMessage(Message msg) where msg is the Message you get with obtainMessage.

techi.services
  • 8,473
  • 4
  • 39
  • 42
  • Sorry but that does not make any sense. – mojorisinify Aug 09 '12 at 19:16
  • @mojorisinify. Why does that not make any sense? How do you think a `Handler` works..? Look at the API reference and you will see `obtainMessage` returns a `Message`. Use that `Message` as the parameter in `Handler.sendMessage`. – techi.services Aug 09 '12 at 19:20
  • I have proper understanding of how a handler works. I have used it in other parts of the application to receive messages from process running in a different thread, and it works fine with them. Tell me why I have to change the approach for this situation particular? – mojorisinify Aug 09 '12 at 19:57
  • @mojorisinify. You obviously don't or you would realise that `obtainMessage` doesn't send a `Message` to a `Handler`... It just obtains one from the pool. Anyway good luck with your copy and paste application. – techi.services Aug 09 '12 at 20:03
  • you were right, sorry about all the fuss. I was missing sendToTarget(); – mojorisinify Aug 12 '12 at 00:39
  • @mojorisinify. `sendToTarget` wasn't quite my answer but has the same principle that the message needs to be sent to the handler. I am glad you fixed the problem and accepting my answer shows your maturity. Sorry for my c&p comment. +5 your question. – techi.services Aug 12 '12 at 02:18
  • @mojorisinify. a little aside... To communicate with other classes on the same `Thread` you can implement an `Interface`. A good example is shown in the `Fragments Communicating With Each Other` API example. After all, if there is no boundary a `Handler` is not required. – techi.services Aug 12 '12 at 02:29
0

The problem was how I was handling the message received from the handler. Turned out Set<> was initially set to null as per eclipse's instruction, so it was never adding the devices received from the BroadcastReceiver helper class. I appreciate anyone who tried to help.

mojorisinify
  • 377
  • 5
  • 22