寻找Android的蓝牙配对设备 [英] Finding Android Bluetooth Paired Devices
问题描述
我想创造的是,当pressed,presents的用户配对的蓝牙设备的列表连接到一个图像按钮。
不过,我得到设置不能被解析为一个变量##点1处, 和##点2处mArrayAdapber不能得到解决 (## 1 ## 2不属于code部分...)
我用从Android网站上的code,但在黑暗之中,我发现自己在黑暗中。
我倒是AP preciate一些指导...
//搜索
的ImageButton =的bsearch(的ImageButton)findViewById(R.id.Search);
bSearch.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图查看){
## 1套< BluetoothDevice类> pairedDevices = mBluetoothAdapter.getBondedDevices();
//如果有配对设备
如果(pairedDevices.size()大于0){
//循环配对设备
对于(BluetoothDevice类设备:pairedDevices){
//添加姓名和地址到一个数组适配器在ListView显示
## 2mArrayAdapter.add(device.getName()+\ N+ device.getAddress());
}
}
}
});
有关1),那么,如果你还没有这样做,添加
>进口为java.util.Set;
在你的import语句。这将解决设置错误。
有关2)声明和初始化
mArrayAdapter
例如,在你的活动做的:
私人ArrayAdapter<字符串> mArrayAdapter;
,然后上的onCreate:
mArrayAdapter =新的ArrayAdapter<字符串>(这一点,<您的布局文件>);
然后它应该添加到ListView
//查找并设置在ListView为新发现的设备
的ListView newDevicesListView =(ListView控件)
findViewById(R.id.&其中; layout_file&GT);
newDevicesListView.setAdapter(mArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
请参照蓝牙聊天例如,从Android的例子。它应该帮助你得到去蓝牙API的
更新的评论:
如果你仔细看的BluetoothChat.java文件的BT例如,你会看到这个
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(D)Log.d(TAG,onActivityResult+结果code);
开关(要求code){
案例REQUEST_CONNECT_DEVICE:
//当DeviceListActivity返回与设备连接
如果(结果code == Activity.RESULT_OK){
//获取设备的MAC地址
字符串的地址= data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
//获取BluetoothDevice类对象
BluetoothDevice类设备= mBluetoothAdapter.getRemoteDevice(地址);
//尝试连接到该设备
mChatService.connect(设备);
}
打破;
案例REQUEST_ENABLE_BT:
//当请求启用蓝牙的回报
如果(结果code == Activity.RESULT_OK){
//蓝牙已经启用,因此建立了一个聊天会话
setupChat();
} 其他 {
//用户没有启用蓝牙或发生错误
Log.d(TAG,BT不启用);
Toast.makeText(这一点,R.string.bt_not_enabled_leaving,Toast.LENGTH_SHORT).show();
完();
}
}
}
关注此行:
//尝试连接到该设备
mChatService.connect(设备);
这个函数连接到蓝牙设备。第一次,它会问你自动配对。一旦配对,下一次它会自动连接到蓝牙设备。
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());
}
}
}
});
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
mArrayAdapter
For example in your Activity do :
private ArrayAdapter<String> mArrayAdapter;
and then on onCreate:
mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);
which should then be added to a 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);
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
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();
}
}
}
Watch this line :
// 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屋!