Can not list the bluetooth devices as a list

问题: I'm trying to list the bluetooth devices as a list (NAME & ADDRESS) on my app but is not working. This is my code : BluetoothAdapter mBluetoothAdapter = BluetoothA...

问题:

I'm trying to list the bluetooth devices as a list (NAME & ADDRESS) on my app but is not working.

This is my code :

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list, Arrays.asList(pairedDevices.toString())));

It's saying that I have not permission to show the devices.

What I'm missing?


回答1:

Well I see two errors in your code, if you want to show it as a "NAME & ADDRESS" as you said you have to make sure that you have this in your manifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Then to show what you want, you have to do this :

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
   s.add(bt.getName() + " : " + bt.getAddress());

setListAdapter(new ArrayAdapter<String>(mContext, R.layout.list, s));

Then it should work.

  • 发表于 2018-07-11 06:39
  • 阅读 ( 220 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除