Action_discovery_finish永远不会被调用

时间:2014-02-12 13:39:52

标签: android bluetooth android-bluetooth

我正在开发一个需要在启动时搜索蓝牙设备的应用程序。但 ACTION_DISCOVERY_FINISHED 永远不会在我的程序中调用,因此它会一直发现并且我不知道为什么。
这是我的代码:

 public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.registerReceiver(mReceiver, new IntentFilter(
            BluetoothDevice.ACTION_FOUND));
    this.registerReceiver(mReceiver, new IntentFilter(
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
            if (checkBluetoothAdapter()) {
        finish();
    } else {
        Intent enableIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, 0);
    }
    search();
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    private int count;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                list.add(device);
                Toast.makeText(
                        context,
                        "Found: " + device.getName() + ": "
                                + device.getAddress(), Toast.LENGTH_SHORT)
                        .show();
                count++;
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            count = 0;
            Toast.makeText(context, "Search started...", Toast.LENGTH_SHORT)
                    .show();
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                .equals(action)) {
            Toast.makeText(
                    context,
                    "Search finished. " + count + " devices found",
                    Toast.LENGTH_LONG).show();
            dialog.dismiss();
            updateList();
        }
    }
};

private void search() {
    if (btfAdapter.isDiscovering()) {
        btfAdapter.cancelDiscovery();
    }

    btfAdapter.startDiscovery();
    dialog = ProgressDialog.show(this, "Searching",
            "Searching bluetooth devices...");
}

0 个答案:

没有答案