【移动开发作业6】蓝牙通信的简要设计与开发

本文介绍了Android平台上蓝牙通信的功能设计,包括搜索设备、设备间通信和控制设备被搜索的选项。详细阐述了核心开发步骤,如开启蓝牙、申请权限、查找和连接设备,以及管理蓝牙连接。并提供了页面设计的XML布局文件示例和权限申请代码。最后展示了开启蓝牙和搜索设备的结果,并给出了源码地址。
摘要由CSDN通过智能技术生成

一、功能说明

蓝牙通信的主要功能设计为以下三个部分,分别是:
1、搜索附近蓝牙设备,并向周围设备发起蓝牙通信请求
2、实现设备之间通过蓝牙进行通信
3、用户自定义蓝牙设备能否被搜索

二、核心代码和步骤

1,首先开启蓝牙

2,搜索可用设备

3,创建蓝牙socket,获取输入输出流

4,读取和写入数据

5,断开连接关闭蓝牙

1、页面设计

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#666"
        android:text="@string/title_paired_devices"
        android:textColor="#fff"
        android:visibility="visible" />
    <ListView android:id="@+id/paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <TextView
        android:id="@+id/title_new_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#666"
        android:text="@string/title_other_devices"
        android:textColor="#fff"
        android:visibility="visible" />
    
    <ListView android:id="@+id/new_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" />
    <Button android:id="@+id/button_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_scan" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值