查找 Android 蓝牙配对设备
我正在尝试创建一个图像按钮,按下该按钮时,会向用户显示要连接的配对蓝牙设备的列表。
但是,我在 ##1 处得到“Set无法解析为变量”, 和“mArrayAdapber 无法解析”在点 ##2 (##1和##2不是代码的一部分...)
我使用了Android站点的代码,但是在黑暗中,我发现自己处于黑暗中。
我希望得到一些指导...
//搜索
ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
}
});
I'm trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.
However, I get "Set cannot be resolved as a variable" at point ##1,
and "mArrayAdapber cannot be resolved" at point ##2
(##1 and ##2 are not part of the code...)
I used the code from the Android site, but being in the dark, I find myself in the dark.
I'd appreciate some guidance...
//Search
ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 1) 如果您还没有这样做,请添加
> 导入 java.util.Set;
在导入语句中 。这将解决“设置”错误。
对于2)声明并初始化
例如在您的 Activity 中执行以下操作:
然后在 onCreate: 上执行操作,
然后将其添加到 ListView 中
请参阅 Android 示例中的蓝牙聊天示例。它应该可以帮助您开始使用蓝牙 API 的
评论更新:
如果您仔细查看 BT 示例中的 BluetoothChat.java 文件,您将看到此
观看这一行:
此函数连接至蓝牙设备。第一次它会要求您自动配对。配对后,下次它将自动连接到蓝牙设备。
For 1) Well if you haven't done so , add
> import java.util.Set;
in your import statements . This will resolve "Set" error.
For 2) Declare and initialize
For example in your Activity do :
and then on onCreate:
which should then be added to a ListView
Refer to Bluetooth Chat example from Android examples. It should help you get going with the Bluetooth api's
Update on comment :
If you look closely on BluetoothChat.java file in BT example, you'll see this
Watch this line :
This function connects to bluetooth device. First time it'll ask you to pair it automatically. Once paired, next time it'll auto connect to the bluetooth device.
嗨,您也可以在刚刚获得绑定设备集的情况下尝试此代码。
Hi, You can also try this code where you just have get the Set of bonded devices.
只需分别从
##1Set
和##2mArrayAdapter
中删除##1
和##2
即可你的代码。我认为您只是从其他来源复制/粘贴而没有注意。这不是原始代码的一部分。它仅用于列表编号目的。Just remove
##1
and##2
respectively from##1Set<BluetoothDevice>
and##2mArrayAdapter
on your code. I think you'd just copy/paste from another source and did not pay attention to. That is not part of the original code. It just used for list number purposes.