Android BluetoothDevice.Action_found广播从未收到过

时间:2016-05-22 17:16:06

标签: android

我正在尝试列出ListView附近的所有可用设备。

我的清单包含BLUETOOTH,BLUETOOTH_ADMIN和ACCESS_COARSE_LOCATION的权限。

我的问题是ACTION_DISCOVERY_STARTED并且收到了ACTION_DISCOVERY_FINISHED,但没有收到ACTION_FOUND。我的电脑蓝牙可以被发现,手机上的蓝牙设备可以发现它,但我的应用却无法发现。这同样适用于附近的所有设备。

private BluetoothAdapter mBluetoothAdapter;
private ArrayList<String> mDeviceList;
private ListView mListView;
private ProgressDialog progressDialog;

private static final int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_list);

    //list to hold all devices found
    mDeviceList = new ArrayList<>();

    //list to display all devices found
    mListView = (ListView) findViewById(R.id.availItems);
    mListView.setAdapter(new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, mDeviceList));

    //local bluetooth device
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
    }else if(!mBluetoothAdapter.isEnabled()){
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            mDeviceList.add(device.getName() + "\n" + device.getAddress());
        }
    }

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Scanning...");
    progressDialog.setTitle("Looking for devices...");
    progressDialog.setCancelable(false);
    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    Button findButton = (Button) findViewById(R.id.findButton);
    findButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            IntentFilter filter = new IntentFilter();

            filter.addAction(BluetoothDevice.ACTION_FOUND);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

            registerReceiver(mReceiver, filter);
            mBluetoothAdapter.startDiscovery();
        }
    });
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            progressDialog.show();
        }else if (BluetoothDevice.ACTION_FOUND.equals(action) && mBluetoothAdapter.isDiscovering()) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mDeviceList.add(device.getName() + "\n" + device.getAddress());
        }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            progressDialog.dismiss();
        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}

@Override
public void onPause() {
    if (mBluetoothAdapter != null) {
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
    }

    super.onPause();
}

@Override
public void onDestroy() {
    unregisterReceiver(mReceiver);
    mListView = null;
    mBluetoothAdapter = null;
    mDeviceList = null;

    super.onDestroy();
}

1 个答案:

答案 0 :(得分:0)

正如我自己提出的问题:

https://stackoverflow.com/posts/comments/63810282?noredirect=1

这里的问题是必须在运行时授予权限,而不仅仅是在清单中,因为ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION在危险权限组中分组。

要做到这一点,试试这个:

  1. 在运行时请求FINE_LOCATION(或粗略位置)权限,因为我正在使用appcompat,我使用了ActivityCompat方法,但是这个方法可以从Activity子类中轻松调用:

    ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);
    
  2. 实施onRequestPermissionsResult方法:

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSION_REQUEST_CONSTANT: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //permission granted!
                }
                return;
            }
        }
    }