Android蓝牙BroadcastReceiver没有发现设备

时间:2017-07-19 20:00:45

标签: android bluetooth android-bluetooth

我已经梳理了论坛,以便试图找出为什么我的蓝牙BroadcastReceiver无法工作,而我能找到的就是人们说要确保请求粗略/精细的位置权限。我已经在这里做了(在发现方法之前调用方法):

申请权限

private void requestPermissions()
{
    final int CODE = 5; // app defined constant used for onRequestPermissionsResult

    boolean allPermissionsGranted = true;

    String[] permissionsToRequest =
            {
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            };

    for(String permission : permissionsToRequest)
    {
        allPermissionsGranted = allPermissionsGranted &&
                (ContextCompat.checkSelfPermission(this, permission)
                        == PackageManager.PERMISSION_GRANTED);
    }

    if(!allPermissionsGranted)
    {
        ActivityCompat.requestPermissions(this, permissionsToRequest, CODE);
    }
}

问题是我的BroadcastReceiver从未找到其他可发现的设备,如果设备已经配对,它会按预期将其放入ListView,但是一旦设备未配对,它就无法再次识别它。请参阅下面的截图。

Output with no paired devices

Output with paired devices

我现在感到很茫然,感觉就像我已经尝试过一切,如果有人有任何想法你会挽救我的生命,我会永远感激。

谢谢,

广播接收器

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

        if(BluetoothDevice.ACTION_FOUND.equals(action))
        {
            BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if(btDevice.getBondState() != BluetoothDevice.BOND_BONDED)
            {
                newDevicesArrayAdapter.add(btDevice.getName() + "\n" + btDevice.getAddress());
            }
        }
        else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
        {
            setTitle(R.string.select_device);

            if(newDevicesArrayAdapter.getCount() == 0)
            {
                newDevicesArrayAdapter.clear();
                String noDevices = getResources().getText(R.string.none_found).toString();
                newDevicesArrayAdapter.add(noDevices);
            }
        }
    }
};

onCreate - 设置ListView元素和IntentFilters

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_machine);

    setResult(Activity.RESULT_CANCELED);

    pairedDevicesArrayAdapter   = new ArrayAdapter<String>(this, R.layout.device_name);
    newDevicesArrayAdapter      = new ArrayAdapter<String>(this, R.layout.device_name);

    ListView pairedListView     = (ListView) findViewById(R.id.paired_devices);
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);

    pairedListView.setAdapter(pairedDevicesArrayAdapter);
    pairedListView.setOnItemClickListener(deviceClickListener);
    newDevicesListView.setAdapter(pairedDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(deviceClickListener);

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(broadcastReceiver, filter);

    filter              = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(broadcastReceiver, filter);
}

doDiscovery() - 由onStart方法调用,我已经确认它被调用

private void doDiscovery()
{
    Log.d(TAG, "doDiscovery()");

    requestPermissions();

    setTitle(R.string.scanning);

    findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);

    if(btAdapter.isDiscovering()) { btAdapter.cancelDiscovery(); }

    newDevicesArrayAdapter.clear();
    btAdapter.startDiscovery();
}

1 个答案:

答案 0 :(得分:0)

原来我只是愚蠢并且更新了两个ListViews的值,所以我从未见过其他设备的列表。

pairedListView.setAdapter(pairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(deviceClickListener);
newDevicesListView.setAdapter(pairedDevicesArrayAdapter);
newDevicesListView.setOnItemClickListener(deviceClickListener);

应该是:

pairedListView.setAdapter(pairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(deviceClickListener);
newDevicesListView.setAdapter(newDevicesArrayAdapter);
newDevicesListView.setOnItemClickListener(deviceClickListener);