Android之蓝牙开发——简易布局(蓝牙的打开、关闭、搜索已配对设备、发现远程设备、配对,显示配对状态)

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

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

准备阶段

  1. 下载并安装 Android Studio 3.0 及其以上版本
  2. 新建一个安卓项目
    File(文件)——New(新建)——New Project(新的项目)——Empty Activity(空的Activity)——Next(下一步)——Name(取一个项目名字,例:Bluetoothtest01)——Finish(完成)

第一步:添加蓝牙权限——AndroidManifest.xml

在新建的安卓项目的配置文件中添加蓝牙权限
【AndroidManifest.xml】配置文件位置如下:

  • app
    • manifests
      • AndroidManifest.xml
// 蓝牙权限
<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"/
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值