安卓基于BLE的蓝牙开发入门

本文详细介绍了如何在Android设备上设置开发者选项,启用USB调试,进行真机调试,以及在Manifest文件中添加权限。还涉及设备扫描、权限管理、BluetoothAPI的使用,包括LeScanCallback和BluetoothGattCallback回调方法。此外,还提供了Android学习资源链接,强调了坚持学习的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值