Android SDK Java - 在列表视图中列出蓝牙名称 [英] Android SDK Java - Listing Bluetooth names in a listview
本文介绍了Android SDK Java - 在列表视图中列出蓝牙名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
this is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_to);
ListView lv1 = (ListView) findViewById(R.id.listViewBlueToothNames);
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
lv1.setAdapter(btArrayAdapter);
}
ArrayAdapter<String> btArrayAdapter;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
我完成了所有的进口和东西,问题是列表视图中没有任何东西出现,我我被要求接受我的蓝牙并且我说是的,但有人可以帮助我这样做,所以listview会显示所有可用的蓝牙名称吗?
ive done all the imports and stuff, the problem with this is that nothing is comming up in the listview, I do get asked to acesss my bluetooth and I say yes, but can some one please help me make it so the listview will show all the bluetooth names that are avalible?
推荐答案
您好,
请查看 http://android-er.blogspot.in/2011/05/get-list-of-paired-bluetooth-devices.html [ ^ ]或Bluettoh文档位于 http://developer.android.com/guide/topics/connectivity/bluetooth.html [ ^ ]。
要检索设备列表,您必须使用如下所示的代码(如下所示) 取自Android文档 )。
Hello,
Please have a look at http://android-er.blogspot.in/2011/05/get-list-of-paired-bluetooth-devices.html[^] or Bluettoh documentation located at http://developer.android.com/guide/topics/connectivity/bluetooth.html[^].
To retrieve the list of devices you will have to use code something like one shown below (taken from the Android Documentation).
Set<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
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}</bluetoothdevice>
问候,
Regards,
这篇关于Android SDK Java - 在列表视图中列出蓝牙名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文