【问题标题】:For loop issue with Bluetooth devices蓝牙设备的循环问题
【发布时间】:2017-09-17 17:58:18
【问题描述】:

我正在尝试将我的手机连接到适用于 Arduino 的 HN-06 蓝牙模块。
我正在使用 for 循环将每个绑定设备添加到 ListView:

mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Set pairedDevices = mBtAdapter.getBondedDevices();

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

但是当我这样做时,我在 BluetoothDevice 设备中收到一个错误,告诉我它们是

不兼容的类型,必需:对象,找到:蓝牙设备。

我该如何解决这个问题?

钯。 mBtAdapter 以下列方式在类下声明:

private BluetoothAdapter mBtAdapter;

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    您需要为您的 Set 定义对象的类型

    Set<BluetoothDevice> pairedDevices
    

    您也可以删除 if (pairedDevices.size() &gt; 0){,因为循环会处理这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多