简体   繁体   English

类扩展的BroadcastReceiver无法正常工作

[英]Class extending BroadcastReceiver not working

I made a helper class that extends BroadcastReceiver to listen for BluetoothDevice found and discovery finish intents. 我制作了一个帮助程序类,该类扩展了BroadcastReceiver以侦听找到的BluetoothDevice和发现完成的意图。 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: 我实例化类和registerReceiver像这样:

From mainActivity : 来自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 : 从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 : DeviceHelper类

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. 感谢您的帮助。

您需要sendMessage(Message msg) ,其中msg是通过obtainMessage获得的Message

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. 结果表明,根据eclipse的说明,Set <>最初被设置为null,因此它从未添加从BroadcastReceiver帮助器类接收的设备。 I appreciate anyone who tried to help. 感谢任何尝试提供帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setLocation在扩展JPanel的类上不起作用 - setLocation not working on class extending JPanel 当Class实现一个侦听器接口来侦听扩展BroadcastReceiver的类中记录的更改时,没有任何反应 - When a Class implements a listener interface to listen for changes recorded in a class extending BroadcastReceiver, nothing happens 扩展 AndroidTestCase 类时 JUnit 4 注释不起作用 - JUnit 4 annotations not working when extending AndroidTestCase class 解决问题 2 class 扩展 - Problems when working around no 2 class extending 在BroadCastReceiver类中未定义的构造方法 - Constructor undefined in a BroadCastReceiver class AlarmManager / BroadcastReceiver无法正常工作 - AlarmManager/BroadcastReceiver not working 重新启动后,BroadcastReceiver无法正常工作 - BroadcastReceiver is not working after reboot 在AndroidManifest中注册的BroadcastReceiver无法正常工作 - BroadcastReceiver registered in AndroidManifest not working 如果通过扩展Handler在单独的Java类中创建,则Handler无法正常工作 - Handler is not working if created in seprate java class by extending Handler 在不同的包中扩展WebSecurityConfigurerAdapter类时,自定义安全性不起作用 - Custom security is not working when extending the WebSecurityConfigurerAdapter class in different package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM