【问题标题】:How to get list of all bluetooth bonded devices in android?如何获取android中所有蓝牙绑定设备的列表?
【发布时间】:2023-03-02 21:32:01
【问题描述】:

如何使用蓝牙适配器获取所有绑定设备的名称,我需要一个正确的工作代码,希望有人能帮助我。

【问题讨论】:

标签: android


【解决方案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());
        }
    }
}

完整示例:

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);
 }
}

在 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>

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2011-06-24
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多