如何获取android中所有蓝牙绑定设备的列表?
如何使用蓝牙适配器获取所有绑定设备的名称,我需要一个正确的工作代码,希望有人能帮助我。首先确保将这些权限添加到应用程序清单文件中:如何获取android中所有蓝牙绑定设备的列表?,android,Android,如何使用蓝牙适配器获取所有绑定设备的名称,我需要一个正确的工作代码,希望有人能帮助我。首先确保将这些权限添加到应用程序清单文件中: <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permissio
<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());
}
}
}
BluetoothAdapter BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
//设备不支持蓝牙
}
否则{
//蓝牙已关闭,请用户打开蓝牙。
如果(!bluetoothAdapter.isEnabled()){
Intent enableAdapter=新的Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(enableAdapter,0);
}
//用你的蓝牙适配器做任何你想做的事
设置所有设备=bluetoothAdapter.getBondedDevices();
如果(所有设备.size()>0){
用于(蓝牙设备currentDevice:all_设备){
log.i(“设备名称”+currentDevice.getName());
}
}
}
完整示例:
public class PairedDeviceActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> mDeviceList = new ArrayList<>();
private void getBluetoothPairedDevices(final ArrayList<String> deviceList, final ListView listView){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "This device not support bluetooth", Toast.LENGTH_LONG).show();
} else {
if (!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
Set<BluetoothDevice> all_devices = bluetoothAdapter.getBondedDevices();
if (all_devices.size() > 0) {
for (BluetoothDevice currentDevice : all_devices) {
deviceList.add("Device Name: "+currentDevice.getName() + "\nDevice Address: " + currentDevice.getAddress());
listView.setAdapter(new ArrayAdapter<>(getApplication(),
android.R.layout.simple_list_item_1, deviceList));
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paired_device);
listView = findViewById(R.id.listView);
getBluetoothPairedDevices(mDeviceList,listView);
}
}
公共类PairedDeviceActivity扩展了AppCompativeActivity{
私有列表视图列表视图;
private ArrayList mDeviceList=new ArrayList();
私有void getBluetoothPairedDevices(最终ArrayList deviceList,最终ListView ListView){
BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
Toast.makeText(getApplicationContext(),“此设备不支持蓝牙”,Toast.LENGTH\u LONG.show();
}否则{
如果(!bluetoothAdapter.isEnabled()){
Intent enableAdapter=新的Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(enableAdapter,0);
}
设置所有设备=bluetoothAdapter.getBondedDevices();
如果(所有设备.size()>0){
用于(蓝牙设备currentDevice:all_设备){
deviceList.add(“设备名称:”+currentDevice.getName()+“\n设备地址:”+currentDevice.getAddress());
setAdapter(新的ArrayAdapter(getApplication()),
android.R.layout.simple_list_item_1,deviceList));
}
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u配对设备);
listView=findViewById(R.id.listView);
getBluetoothPairedDevices(mDeviceList,listView);
}
}
在xml上:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PairedDeviceActivity">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>