ACTION_DISCOVERY_STARTED从未调用过android

时间:2012-06-21 20:29:34

标签: android bluetooth

我有下一个代码:

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,但所有其他操作都没问题,我缺少什么?谢谢你的时间。

1 个答案:

答案 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();