如何使用蓝牙适配器获取所有绑定设备的名称,我需要一个正确的工作代码,希望有人可以帮助我。
答案 0 :(得分:1)
首先确保将这些权限添加到您的应用清单文件中:
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
现在做:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// device doesn't support bluetooth
}
else {
// bluetooth is off, ask user to on it.
if(!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
// Do whatever you want to do with your bluetoothAdapter
Set<BluetoothDevice> all_devices = bluetoothAdapter.getBondedDevices();
if (all_devices.size() > 0) {
for (BluetoothDevice currentDevice : all_devices) {
log.i("Device Name " + currentDevice.getName());
}
}
}