4

私は次のコードを持っています:

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 が呼び出されることはありませんが、他のすべてのアクションは問題ありません。何が欠けていますか? お時間をいただきありがとうございます。

4

1 に答える 1

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();受信者を登録した後に置く

于 2012-06-21T20:44:34.783 に答える