1.进入手机的(更多设置中)开发者选项(这里以小米为例,进入设置,在我的设备中找到全部参数,连点MIUI版本处看到下方提示进入开发者选项即可),打开USB调试以及USB安装即可
2.打开andorid studio,将数据线连接上手机,就进入了真机调试。
1.在Manifest文件中添加
这里需要添加以上权限,否则会导致闪退
2.如果程序正常运行,但是扫描不到任何的设备,打开手机,进入手机权限管理,给安装的app定位权限设置为允许,同时保证手机中的位置信息是打开的,这步非常重要,旧版本的安卓系统可以略过,本机为andorid 10版本,如果不操作这步将不能看到任何设备
扫描设备主界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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”
android:orientation=“vertical”
tools:context=“com.example.blue_test.MainActivity”>>
<Button
android:id=“@+id/Search_device”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“扫描设备”/>
<TextView
android:id=“@+id/Connection_Status”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“连接状态:未连接”
android:gravity=“center”
android:textSize=“20dp”/>
<ListView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/list”
/>
实现效果:
扫描设备信息界面
<?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”>
<ImageView
android:id=“@+id/bluetoothicon”
android:layout_width=“37dp”
android:layout_height=“50dp”
android:background=“@drawable/ic_launcher_background”
android:src=“@android:drawable/stat_sys_data_bluetooth”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<TextView
android:id=“@+id/bluetoothname”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginStart=“16dp”
android:text=“name”
android:textColor=“#000000”
android:textSize=“19dp”
app:layout_constraintStart_toEndOf=“@+id/bluetoothicon”
app:layout_constraintTop_toTopOf=“parent” />
<TextView
android:id=“@+id/uuid”
a