Android中的手动扫描会增加蓝牙扫描结果

时间:2016-03-23 12:37:00

标签: android bluetooth broadcastreceiver android-broadcastreceiver

我在Android中实现了一个广播接收器来收听所有可用的蓝牙信号。接收器工作,但只显示四个设备。当我在Android菜单中手动扫描设备时,我突然在我的应用程序中看到八个不同的设备。

如何在不使用手动扫描的情况下查看所有可用设备?

代码:

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Add the name and address to an array adapter to show in a ListView
            int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            Log.d("bluetoothSignal", "name: "+ device.getName() + " adress: "+ device.getAddress() + "strength: "+ rssi + " Data: " + intent.getData());
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }

    }

};

1 个答案:

答案 0 :(得分:0)

请注意,在“Android菜单”中,扫描结果可以显示配对设备以及扫描设备。所以这可以解释重复。然而,这种情况不太可能,因为任何良好的蓝牙扫描结果都应该可以防止重复的结果,但可能存在允许重复的特殊情况。

我怀疑实际发生的是系统设置正在返回传统的蓝牙设备(使用rfcomm)和蓝牙LE设备。问题是,大多数蓝牙设备菜单没有显示指示设备是传统设备还是蓝牙LE设备的标识符,只显示设备名称。此外,使用BluetoothAdapter.startDiscovery()时,它往往更喜欢传统的蓝牙设备,只偶尔返回蓝牙LE设备。例如,根据我的经验,85%的时间它在完成发现过程所花费的时间内没有发现蓝牙LE设备,只有13秒。我建议使用传统的扫描机制和蓝牙LE扫描机制。要执行蓝牙LE扫描,请获取对BluetoothLeScanner来电BluetoothAdapter.getBluetoothLeScanner()的引用。获得参考后,请致电BluetoothLeScanner.startScan(ScanCallback callback)。 ScanCallback将被发现的蓝牙LE设备解雇,您可以将其添加到ListView的适配器中。试试这个,看看你是否通过'android菜单'得到了类似的结果。

相关问题