我正在尝试创建一个图像按钮,当按下该按钮时,会向用户显示要连接的已配对蓝牙设备的列表。
然而,我在第## 1点得到“Set not as as resolved off”, 并且在## 2点处“mArrayAdapber无法解析” (## 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());
}
}
}
});
答案 0 :(得分:4)
1)如果你还没有这样做,请添加
<强>&GT; import java.util.Set;
导入语句中的。这将解决“设置”错误。
For 2)声明并初始化
<强> mArrayAdapter 强>
例如,在您的活动中执行:
private ArrayAdapter<String> mArrayAdapter;
然后on onCreate:
mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);
然后应该添加到ListView
//为新发现的设备查找并设置ListView
ListView newDevicesListView = (ListView)
findViewById(R.id.<layout_file>);
newDevicesListView.setAdapter(mArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
请参阅Android示例中的蓝牙聊天示例。它应该可以帮助您使用蓝牙API
评论更新:
如果你仔细观察BT示例中的BluetoothChat.java文件,你会看到这个
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);
此功能连接到蓝牙设备。第一次它会要求你自动配对。配对后,下次它将自动连接到蓝牙设备。
答案 1 :(得分:3)
您好,您也可以尝试使用此代码,只需获取已设置的绑定设备。
private ArrayAdapter<String> bondedAdapter = null;
private ListView listViewPairedDevices = null;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
try {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview);
Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices();
Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);
int count = 0;
if(bondedSet.size() > 0){
for(BluetoothDevice device : bondedSet){
textViewPairedDevice.setVisibility(View.VISIBLE);
bondedAdapter.add(device.getName()+"\n"+device.getAddress());
Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
}
}else{
bondedAdapter.add("No Devices");
}
listViewPairedDevices.setAdapter(bondedAdapter);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
}
}//onStart ends
答案 2 :(得分:0)
只需从代码中的##1
和##2
分别删除##1Set<BluetoothDevice>
和##2mArrayAdapter
即可。我想你只是从其他来源复制/粘贴而没有注意。这不是原始代码的一部分。它仅用于列表编号目的。