【问题标题】:Bluetooth discovering the same device 10 times蓝牙发现同一设备 10 次
【发布时间】:2018-05-22 14:31:10
【问题描述】:

我正在使用 Android Studio 开发蓝牙应用程序。今天,我遇到了一个奇怪的问题。 我有 3 个蓝牙设备、一个智能手机、一个平板电脑和另一个设备,但我们并不真正关心它。

我在两台设备上执行相同的应用程序,但智能手机运行的是 Android 8.1 (API 27),而平板电脑运行的是 Android 4.0.4 (API 15)。 在智能手机上,该应用程序运行良好。当我扫描附近的设备时,我得到了 4 个不同的设备。

但是有问题。在平板电脑上,当我扫描附近的设备时,我的智能手机检测到每个设备的次数几乎是 10 次。我真的不知道为什么两个设备都没有像彼此一样发现。可能是 Android 版本导致该错误。

 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) { // discover devices
            Scanned_devices = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            scanned_deviceName = Scanned_devices.getName();
            scanned_macAddress = Scanned_devices.getAddress();


            mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
            Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);


            Set<BluetoothDevice> pairedDevices  = blueAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    try {
                        if (scanned_deviceName.equals(device.getName()) || scanned_macAddress.equals(device.getAddress())) {
                            Toast.makeText(getApplicationContext(), "Already Paired", Toast.LENGTH_LONG).show();
                            mDeviceList.remove(scanned_deviceName + "\n" + scanned_macAddress);
                        } //else {
                            //mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
                            //Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
                        //}
                    }catch(Exception e)
                    {
                        Log.d("tag", "not working");
                        Toast.makeText(getApplicationContext(), "not working..", Toast.LENGTH_LONG).show();
                    }
                }
            }

            Scanned_devices_ListView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, mDeviceList));
        }
    }
};

【问题讨论】:

  • 尝试在onReceive方法之外设置适配器并测试

标签: android bluetooth broadcastreceiver android-bluetooth


【解决方案1】:

在尝试了很多方法来检测设备是否已存在于我的 ArrayList 中后,我尝试了以下代码:

if (!mDeviceList.contains(scanned_deviceName + "\n" + scanned_macAddress))
            {
                mDeviceList.add(scanned_deviceName + "\n" + scanned_macAddress);
                Log.i("BT", scanned_deviceName + "\n" + scanned_macAddress);
            }

这将检查一个数组是否包含这个字符串: DeviceName DeviceMac@

如果条件为真,则将字符串添加到 ArrayList 中。

如果为 false(ArrayList 中有另一个 String 内容相同),则不会将该 String 添加到 ArrayList 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 2017-03-17
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多