Android 如何获取蓝牙设备作为列表?

Android 如何获取蓝牙设备作为列表?,android,listview,bluetooth,Android,Listview,Bluetooth,我正在尝试获取绑定的蓝牙设备,但我可以将其作为长字符串而不是列表获取 这是我的代码: BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList<String> listview = new ArrayList&l

我正在尝试获取绑定的蓝牙设备,但我可以将其作为长字符串而不是列表获取

这是我的代码:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
ArrayList<String> listview = 
new ArrayList<String>(Arrays.asList(pairedDevices.toString()));
setListAdapter(new ArrayAdapter<String>(this, R.layout.list, listview));
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
ArrayList listview=
新的ArrayList(Arrays.asList(pairedDevices.toString());
setListAdapter(新的ArrayAdapter(this,R.layout.list,listview));
我得到的是这样的:
[00:23:7F:1c,f0:09:f1:b4:b0]
。这一切都在一条线上。 如何将其更改为列表中而不是一行中的所有内容

另外,我如何获得设备的友好名称而不是这些号码


谢谢

您应该按以下方式更改代码:

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

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

setListAdapter(new ArrayAdapter<String>(this, R.layout.list, s));
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
列表s=新的ArrayList();
用于(蓝牙设备bt:pairedDevices)
s、 添加(bt.getName());
setListAdapter(新的ArrayAdapter(this,R.layout.list,s));

我尝试了下面的代码

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView 
    android:id="@+id/bluetoothstate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<Button
    android:id="@+id/listpaireddevices" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="List Paired Devices" 
    android:enabled="false"
    /> 
<TextView
    android:id="@+id/bluetoothstate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test.AndroidBluetooth"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidBluetooth"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <activity android:name=".ListPairedDevicesActivity" 
      android:label="AndroidBluetooth: List of Paired Devices"/>
    </application>
</manifest>

查找附近蓝牙设备的列表:

找到相同的屏幕截图

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private ListView listView;
    private ArrayList<String> mDeviceList = new ArrayList<String>();
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.startDiscovery();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device.getName() + "\n" + device.getAddress());
                Log.i("BT", device.getName() + "\n" + device.getAddress());
                listView.setAdapter(new ArrayAdapter<String>(context,
                        android.R.layout.simple_list_item_1, mDeviceList));
            }
        }
    };
<RelativeLayout 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"
    tools:context="com.example.bluetoothdemo.MainActivity" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"/>

</RelativeLayout>
公共类MainActivity扩展了ActionBarActivity{
私有列表视图列表视图;
private ArrayList mDeviceList=new ArrayList();
私人蓝牙适配器mBluetoothAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
registerReceiver(mrReceiver,过滤器);
}
@凌驾
受保护的空onDestroy(){
未注册接收人(mReceiver);
super.ondestory();
}
专用最终广播接收器mReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
蓝牙设备=意图
.getParcelableExtra(蓝牙设备.EXTRA_设备);
mDeviceList.add(device.getName()+“\n”+device.getAddress());
Log.i(“BT”,device.getName()+“\n”+device.getAddress());
setAdapter(新的ArrayAdapter(上下文、,
android.R.layout.simple_list_item_1,mDeviceList));
}
}
};
活动\u main.xml

public class MainActivity extends ActionBarActivity {

    private ListView listView;
    private ArrayList<String> mDeviceList = new ArrayList<String>();
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.startDiscovery();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device.getName() + "\n" + device.getAddress());
                Log.i("BT", device.getName() + "\n" + device.getAddress());
                listView.setAdapter(new ArrayAdapter<String>(context,
                        android.R.layout.simple_list_item_1, mDeviceList));
            }
        }
    };
<RelativeLayout 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"
    tools:context="com.example.bluetoothdemo.MainActivity" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"/>

</RelativeLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



  • 完成

    在这段代码中,您只需要在按钮单击中调用它

    private void list_paired_Devices() {
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            ArrayList<String> devices = new ArrayList<>();
            for (BluetoothDevice bt : pairedDevices) {
                devices.add(bt.getName() + "\n" + bt.getAddress());
            }
            ArrayAdapter arrayAdapter = new ArrayAdapter(bluetooth.this, android.R.layout.simple_list_item_1, devices);
            emp.setAdapter(arrayAdapter);
        }
    
    private void list\u paired\u Devices(){
    设置pairedDevices=mBluetoothAdapter.getBondedDevices();
    ArrayList设备=新的ArrayList();
    用于(蓝牙设备bt:pairedDevices){
    添加(bt.getName()+“\n”+bt.getAddress());
    }
    ArrayAdapter ArrayAdapter=新的ArrayAdapter(bluetooth.this,android.R.layout.simple\u list\u item\u 1,设备);
    emp.setAdapter(arrayAdapter);
    }
    
    包com.sekurtrack.myapplication;
    导入android.bluetooth.BluetoothAdapter;
    导入android.bluetooth.bluetooth设备;
    导入android.content.BroadcastReceiver;
    导入android.content.Context;
    导入android.content.Intent;
    导入android.content.IntentFilter;
    导入android.support.v7.app.AppActivity;
    导入android.os.Bundle;
    导入android.util.Log;
    导入android.widget.ArrayAdapter;
    导入android.widget.ListView;
    导入android.widget.Toast;
    导入java.util.ArrayList;
    导入java.util.Set;
    公共类MainActivity扩展了AppCompatActivity{
    列表视图列表视图;
    私人蓝牙适配器BA;
    private ArrayList mDeviceList=new ArrayList();
    专用配对设备;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(listView)findViewById(R.id.DeviceList);
    BA=BluetoothAdapter.getDefaultAdapter();
    BA.startDiscovery();
    IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
    registerReceiver(mrReceiver,过滤器);
    /*BA=BluetoothAdapter.getDefaultAdapter();
    pairedDevices=BA.getBondedDevices();
    ArrayList=新建ArrayList();
    for(BluetoothDevice bt:pairedDevices)list.add(bt.getName());
    Toast.makeText(getApplicationContext(),“显示配对设备”,Toast.LENGTH_SHORT.show();
    最终ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,list);
    setAdapter(适配器)*/
    }
    @凌驾
    受保护的空onDestroy(){
    未注册接收人(mReceiver);
    super.ondestory();
    }
    专用最终广播接收器mReceiver=新广播接收器(){
    公共void onReceive(上下文、意图){
    String action=intent.getAction();
    if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
    蓝牙设备=意图
    .getParcelableExtra(蓝牙设备.EXTRA_设备);
    mDeviceList.add(device.getName()+“\n”+device.getAddress());
    Log.i(“BT1”,device.getName()+“\n”+device.getAddress());
    setAdapter(新的ArrayAdapter(上下文、,
    android.R.layout.simple\u list\u item\u 1,
    
    package com.sekurtrack.myapplication;
    
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.Set;
    
    public class MainActivity extends AppCompatActivity {
        ListView listView;
        private BluetoothAdapter BA;
        private ArrayList<String> mDeviceList = new ArrayList<String>();
        private Set<BluetoothDevice> pairedDevices;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView=(ListView)findViewById(R.id.devicesList);
    
    
    
            BA = BluetoothAdapter.getDefaultAdapter();
            BA.startDiscovery();
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter);
    
           /* BA = BluetoothAdapter.getDefaultAdapter();
            pairedDevices = BA.getBondedDevices();
            ArrayList list = new ArrayList();
            for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
            Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
            final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
            listView.setAdapter(adapter);*/
    
        }
    
    
        @Override
        protected void onDestroy() {
            unregisterReceiver(mReceiver);
            super.onDestroy();
        }
    
        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent
                            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    mDeviceList.add(device.getName() + "\n" + device.getAddress());
                    Log.i("BT1", device.getName() + "\n" + device.getAddress());
                    listView.setAdapter(new ArrayAdapter<String>(context,
                            android.R.layout.simple_list_item_1, mDeviceList));
                }
            }
        };
    }