查找 Android 蓝牙配对设备 [英] Finding Android Bluetooth Paired Devices
问题描述
我正在尝试创建一个图像按钮,当按下该按钮时,会向用户显示要连接的配对蓝牙设备列表.
I'm trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.
但是,我在##1 处得到Set 无法解析为变量",和mArrayAdapber 无法解析"在点 ##2(##1 和 ##2 不是代码的一部分...)
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...)
我使用了 Android 网站上的代码,但在黑暗中,我发现自己陷入了黑暗.
I used the code from the Android site, but being in the dark, I find myself in the dark.
希望得到一些指导...
I'd appreciate some guidance...
//搜索
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() + "
" + device.getAddress());
}
}
}
});
推荐答案
对于 1) 如果您还没有这样做,请添加
For 1) Well if you haven't done so , add
> import java.util.Set;
在您的导入语句中.这将解决设置"错误.
in your import statements . This will resolve "Set" error.
对于2)声明并初始化
mArrayAdapter
例如在你的活动中做:
private ArrayAdapter<String> mArrayAdapter;
然后 onCreate:
mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);
然后将其添加到 ListView
which should then be added to a ListView
//为新发现的设备查找并设置 ListView
// Find and set up the ListView for newly discovered devices
ListView newDevicesListView = (ListView)
findViewById(R.id.<layout_file>);
newDevicesListView.setAdapter(mArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
<小时>
参考 Android 示例中的蓝牙聊天示例.它应该可以帮助您使用蓝牙 api 的
评论更新:
如果您仔细查看 BT 示例中的 BluetoothChat.java 文件,您会看到这个
If you look closely on BluetoothChat.java file in BT example, you'll see this
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(D) Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
mChatService.connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
setupChat();
} else {
// User did not enable Bluetooth or an error occured
Log.d(TAG, "BT not enabled");
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
}
}
观看这一行:
// Attempt to connect to the device
mChatService.connect(device);
此功能连接蓝牙设备.第一次它会要求你自动配对.配对后,下次它会自动连接到蓝牙设备.
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.
这篇关于查找 Android 蓝牙配对设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!