33

結合された Bluetooth デバイスを取得しようとしていますが、リストではなく長い文字列として取得できます。

これは私のコードです:

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

私はこのようなものを得ています: [00:23:7F:1c, f0:09:f1:b4:b0]. そして、そのすべてが1行にあります。すべてを1行ではなく、リストに入れるように変更するにはどうすればよいですか?

また、これらの番号ではなく、デバイスのわかりやすい名前を取得するにはどうすればよいですか?

ありがとう!!!

4

6 に答える 6

51

以下のようにコードを変更する必要があります。

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));
于 2012-05-29T08:24:45.497 に答える
48

近くの Bluetoothデバイスのリストを検索:

同じスクリーンショットを見つけます。

ここに画像の説明を入力

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

activity_main.xml :

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

ノート:

ユーザーに位置情報の許可を求め、GPS をオンにしてください。

理由: Android 6.0 以降では、Bluetooth 検出に位置情報のアクセス許可が必要です。

より多くの参照:

  1. https://developer.android.com/guide/topics/connectivity/bluetooth
  2. https://getlief.zendesk.com/hc/en-us/articles/360007600233-Why-does-Android-require-Location-Permissions-for-Bluetooth-

終わり

于 2015-07-08T12:44:07.610 に答える
0

これを Kotlin で簡単に行う方法を次に示します。

BluetoothActivity.kt

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class BluetoothActivity : AppCompatActivity() {

private var mBTArrayAdapter: ArrayAdapter<String>? = null
private var mBTAdapter: BluetoothAdapter? = null
private var mPairedDevices: Set<BluetoothDevice>? = null

private var mListPairedDevicesBtn: Button? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_bluetooth)

    mBTArrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1)
    mBTAdapter = BluetoothAdapter.getDefaultAdapter()

    mListPairedDevicesBtn = findViewById<View>(R.id.paired_btn) as Button

    mListPairedDevicesBtn!!.setOnClickListener { listPairedDevices() }

}

private fun listPairedDevices() {
    mBTArrayAdapter?.clear()
    mPairedDevices = mBTAdapter?.bondedDevices
    if (mBTAdapter?.isEnabled == true) {
        for (device in mPairedDevices!!) mBTArrayAdapter?.add(device.name + "\n" + device.address)
    } else Toast.makeText(applicationContext,
        "Please Switch On The Bluetooth First",
        Toast.LENGTH_SHORT).show()
}

}

リスト ビュー:

<Button
        android:id="@+id/paired_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Paired Devices"
        android:layout_marginTop="5dp"
        android:textSize="16sp"/>

それがあなたのために働くかどうか教えてください!

于 2021-12-16T08:22:51.683 に答える