蓝牙开发基本步骤
准备阶段
- 下载并安装 Android Studio 3.0 及其以上版本
- 新建一个安卓项目
File(文件)——New(新建)——New Project(新的项目)——Empty Activity(空的Activity)——Next(下一步)——Name(取一个项目名字,例:Bluetoothtest01)——Finish(完成)
第一步:添加蓝牙权限——AndroidManifest.xml
在新建的安卓项目的配置文件中添加蓝牙权限
【AndroidManifest.xml】配置文件位置如下:
- app
- manifests
- AndroidManifest.xml
- manifests
// 蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
第二步:布局界面——activity_main.xml
根据项目需要,需在Design视图中添加以下android控件
| 名称 | 功能 | 数量 |
|---|---|---|
| Switch(开关) | "打开和关闭蓝牙" |
1 |
| Button (按钮) | "搜索已配对设备、发现远程设备" |
2 |
| ListView (列表) | "显示搜索出来的已配对设备、新的设备、储存远程设备配对信息" |
3 |
布局文件
可根据个人审美布局,修改控件个性样式,以下布局仅供参考
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#f0f0f0"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="蓝牙"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#f6f6f6"
android:text="历史设备"
android:textSize="18sp" />
<Button
android:id="@+id/btn_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#f6f6f6"
android:text="新的设备"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<ListView
android:id="@+id/listview_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f6f6f6"/>
<ListView
android:id="@+id/listview_new"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f6f6f6"/>
</LinearLayout>
<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f6f6"/

这篇博客详细介绍了如何在Android平台上进行蓝牙开发,包括在AndroidManifest.xml中添加蓝牙权限,设计activity_main.xml布局,创建显示设备列表的Layout文件,以及在MainActivity.java中实现打开/关闭蓝牙、搜索已配对和远程设备、显示设备信息及配对状态等功能。通过实际操作和运行效果展示了整个流程。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



