2012-08-08 87 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