问题:
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.