2012-08-08 84 views
1

我做了一个助手类,它扩展了BroadcastReceiver来侦听BluetoothDevice找到和发现完成意图。我有两个通过传递处理程序来使用这个类的活动。处理程序按照意图接收消息。我实例化这样的类和registerReceiver:类扩展BroadcastReceiver不工作

从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(); 

从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类

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); 

     } 
    } 

处理器

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; 
}} 

我失去了一些东西在这里?我感谢任何帮助。

+0

您是否在Manifest中有接收器类声明“DevicesHelper”?什么不工作?你可以在接收器内打断点吗? – Maxim 2012-08-08 16:09:12

+0

@mojorisinify。首先创建一个'IntentFilter'并使用'addAction'来添加任何额外的意图操作。 – techiServices 2012-08-08 16:23:51

+0

@Maxim我没有在声明中声明接收器,它的代码已经注册。 DevicesHelper不是一个活动,所以我不认为它必须在清单中声明。参考:[链接](http://stackoverflow.com/questions/6882476/android-communicate-between-activity-and-broadcast-receiver)。 DevicesHelper被实例化,注册,但是当蓝牙设备处于范围内时它不会被调用。 – mojorisinify 2012-08-08 20:37:26

回答

0

问题是我如何处理从处理程序接收到的消息。原来设置<>按照eclipse的指令最初设置为null,所以它从不添加从BroadcastReceiver助手类接收到的设备。我感谢任何试图帮助的人。

1

你需要sendMessage(Message msg)其中msg是Message你得到obtainMessage

+0

对不起,但没有任何意义。 – mojorisinify 2012-08-09 19:16:47

+0

@mojorisinify。为什么这没有任何意义?你如何看待'Handler'的工作原理?看看API参考,你会看到'obtainMessage'返回一个'Message'。使用'Message'作为'Handler.sendMessage'中的参数。 – techiServices 2012-08-09 19:20:44

+0

我对处理程序的工作原理有了正确的理解。我已经在应用程序的其他部分使用它来接收来自在不同线程中运行的进程的消息,并且它可以正常工作。告诉我为什么我必须改变这种情况的方法,特别是? – mojorisinify 2012-08-09 19:57:23