布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="hard.practice.MainActivity"> <Button android:id="@+id/open" android:layout_width="300dp" android:layout_height="wrap_content" android:text="OPEN" /> <Button android:id="@+id/visible" android:layout_width="300dp" android:layout_height="wrap_content" android:text="Visible" /> <Button android:id="@+id/discover" android:layout_width="300dp" android:layout_height="wrap_content" android:text="Discover" /> <Button android:id="@+id/off" android:layout_width="300dp" android:layout_height="wrap_content" android:text="OFF" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout> MainActivity:package hard.practice; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.Set; import static hard.practice.R.id.lv; public class MainActivity extends AppCompatActivity{ private Button open, visible, discover, off; private BluetoothAdapter BA; private Set<BluetoothDevice> SBD; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); open = (Button) findViewById(R.id.open); visible = (Button) findViewById(R.id.visible); discover = (Button) findViewById(R.id.discover); off = (Button) findViewById(R.id.off); listView= (ListView) findViewById(lv); BA = BluetoothAdapter.getDefaultAdapter(); open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!BA.isEnabled()){ Intent open = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(open,0); } } }); off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BA.disable(); } }); visible.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent visible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(visible,0); } }); discover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SBD=BA.getBondedDevices(); ArrayList list = new ArrayList(); for (BluetoothDevice bt :SBD) list.add(bt.getName()); final ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list); listView.setAdapter(adapter); } }); } }权限:<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />