2012-06-21 62 views
3

我有下面的代碼:ACTION_DISCOVERY_STARTED從來沒有所謂的Android

public class AddPrinter extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    bluetoothAdapter.startDiscovery(); 
    filter = new IntentFilter(); 
    filter.addAction(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(receiver, filter); 
} 

BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     System.out.println(action); 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      adapter.add(device); 
     } 
     else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction())) { 
      System.out.println("STARTED"); 
     } 
     else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) { 
      Utils.dialog.dismiss(); 
     } 
    } 
}; 

我的問題是如此怪異,由於某種原因,ACTION_DISCOVERY_STARTED永遠不會被調用,但所有其他操作都很好,我失去了什麼?感謝您的時間。

+0

名稱確保ü已<用途的許可安卓:名稱= 「android.permission.BLUETOOTH_ADMIN」/>後 <使用許可權的android:名稱=「android.permission.BLUETOOTH」/>在顯示中? –

+0

是的,我有 –

回答

7

試試這個

public class AddPrinter extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

      filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
      registerReceiver(receiver, filter); 
      filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
      registerReceiver(receiver, filter); 
      filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
      registerReceiver(receiver, filter); 

      // 
      bluetoothAdapter.startDiscovery(); 

} 

bluetoothAdapter.startDiscovery();註冊recevier

相關問題