实现功能:
查看本机是否有蓝牙功能、扫瞄周边蓝牙获取其地址。
效果展示:
具体流程:
- AndroidManifest.xml配置蓝牙权限
- activity_main.xml绘制页面_按钮
- MainActivity实现:点击事件监听按钮------>创建一个bluetoothAdapter对象------->bluetoothAdapter对象为空则本机不存在蓝牙功能-------->如存在,bluetoothAdapter.isEnabled()查看蓝牙功能是否被开启-------->bluetoothAdapter.getBondedDevices()扫描蓝牙。
代码展示
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.bluetooth">
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BlueTooth"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scanBlurtooth"
android:text="扫描蓝牙设备"
app:layout_constraintTop_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.bluetooth;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Iterator;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private Button bluetoothButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothButton = (Button) findViewById(R.id.scanBlurtooth);
bluetoothButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
//gain a BluetoothAdapter Object.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//judge whether current device has bluetooth work.
//if bluetoothAdapter == null,it means current device don't have bluetooth work.
if(bluetoothAdapter != null) {
System.out.println("device has bluetooth function");
//judge whether bluetooth is opened.
if (bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//obtain all connected devices
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
if(devices.size()>0){
for(Iterator iterator = devices.iterator();iterator.hasNext();){
BluetoothDevice bluetoothDevice =(BluetoothDevice) iterator.next();
System.out.println(bluetoothDevice.getAddress());
}
}
}else {
System.out.println("device can't use bluetooth");
}
}
});
};
}