佈局代碼:
<?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=".MainActivity">
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open bluetooth"
/>
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open bluetooth"
/>
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open bluetooth"
/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
java代碼:
package com.example.bluetooth;
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.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
BluetoothAdapter adapter;
private TextView tv;
private MyReciver reciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reciver=new MyReciver();
tv=findViewById(R.id.tv);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter =BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
for (BluetoothDevice d :
bondedDevices) {
tv.append( d.getAddress()+"\n");
}
adapter.enable();
}
});
findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.disable();
}
});
IntentFilter filter =new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(reciver,filter);
findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setProgressBarVisibility(true);
setTitle("正在掃描。。。");
if (adapter .isDiscovering()) {
adapter.cancelDiscovery();
}
adapter.startDiscovery();
}
});
}
class MyReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
setProgressBarVisibility(false);
setTitle("已經搜索完成");
}else if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState()!=BluetoothDevice.BOND_BONDED){
tv.append(device.getName()+":"+device.getAddress()+"\n");
}
}
}
}
}
添加權限:
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />