android_mars老师_蓝牙学习1

news2024/9/21 12:32:17

实现功能:

查看本机是否有蓝牙功能、扫瞄周边蓝牙获取其地址。

效果展示:

效果展示

具体流程:

  1. AndroidManifest.xml配置蓝牙权限
  2. activity_main.xml绘制页面_按钮
  3. MainActivity实现:点击事件监听按钮------>创建一个bluetoothAdapter对象------->bluetoothAdapter对象为空则本机不存在蓝牙功能-------->如存在,bluetoothAdapter.isEnabled()查看蓝牙功能是否被开启-------->bluetoothAdapter.getBondedDevices()扫描蓝牙。

代码展示

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.bluetooth">

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BlueTooth"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
</manifest>

activity_main.xml

<?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"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanBlurtooth"
        android:text="扫描蓝牙设备"
        app:layout_constraintTop_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

package com.example.bluetooth;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Iterator;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private Button bluetoothButton = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothButton = (Button) findViewById(R.id.scanBlurtooth);
        bluetoothButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onClick(View v) {
                //gain a BluetoothAdapter Object.
                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                //judge whether current device has bluetooth work.
                //if bluetoothAdapter == null,it means current device don't have bluetooth work.
                if(bluetoothAdapter != null) {
                    System.out.println("device has bluetooth function");
                    //judge whether bluetooth is opened.
                    if (bluetoothAdapter.isEnabled()) {
                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivity(intent);
                    }
                    //obtain all connected devices
                    Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
                    if(devices.size()>0){
                        for(Iterator iterator = devices.iterator();iterator.hasNext();){
                            BluetoothDevice bluetoothDevice =(BluetoothDevice) iterator.next();
                            System.out.println(bluetoothDevice.getAddress());
                        }
                    }
                }else {
                    System.out.println("device can't use bluetooth");
                }
            }
        });
    };
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/714353.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Check Point R81.20 Gaia - 下一代防火墙 (NGFW)

Check Point R81.20 Gaia - 下一代防火墙 (NGFW) Quantum Security Gateway 请访问原文链接&#xff1a;https://sysin.org/blog/check-point-r81/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Quantum Security Gateway a…

执行shell脚本时,提示:第xxx行:[: xxxxxxxxxxxxxxxxxxxxxxxxxxxx:需要一元表达式

定位到 283行看看情况。。。 # ... 此处省略诸多脚本命令isContinue1for item_id in "${ENABLE_BURNER_ARRAY[]}"doif [ $item_id $idd_serial ];then isContinue0continuefidoneif [ $isContinue -eq 0 ];thencontinuefi# ... 此处省略诸多脚本命令# 该行即为 283行…

入侵排查与响应-window和linux版

目录 &#xff08;一&#xff09;关于这方面的一些简单了解 1、我们的电脑为什么会被黑客入侵 2、黑客攻击的方式 &#xff08;二&#xff09;window入侵排查 1、查看异常特征 2、系统账户安全 3、检测异常端口、进程 4、查看启动项、计划任务、服务 5、检查系统相关信…

三、eureka-server端和客户端配置文件讲解

常用配置文件设置 通过这张图理解为什么要进行文件配置 server 中常用的配置 server:port: 8761spring:application:name: eureka-servereureka:client:service-url: #eureka 服务端和客户端的交互地址,集群用,隔开defaultZone: http://localhost:8761/eureka #自己注册自…

中移物联车联网项目,在 TDengine 3.0 的应用

小T导读&#xff1a;在中移物联网的智慧出行场景中&#xff0c;需要存储车联网设备的轨迹点&#xff0c;还要支持对车辆轨迹进行查询。为了更好地进行数据处理&#xff0c;他们在 2021 年上线了 TDengine 2.0 版本的 5 节点 3 副本集群。 3.0 发布后&#xff0c;它的众多特性吸…

C语言入门篇(五)

前言   函数是 C 语言中的重要组成部分&#xff0c;它可以将程序分解为模块&#xff0c;提高代码的可读性和可维护性。   &#x1f352;本篇文章将详细介绍 C 语言中的函数。 函数 1. 函数是什么&#xff1f;2. 函数的分类2.1 库函数2.2 自定义函数 3. 函数的参数3.1 实际参…

select下拉框---无限滚动加载

需求&#xff1a; select的下拉框&#xff0c;后端做了分页&#xff0c;此时前段需要同步加分页 解决思路&#xff1a; 考虑到交互和性能&#xff0c;采用触底请求下一页&#xff08;无限滚动加载&#xff09; 代码示例&#xff1a; import { Select, message } from antd; im…

UE5.1.1 C++从0开始(16.作业5思路分享)

教程的链接&#xff1a;https://www.bilibili.com/video/BV1nU4y1X7iQ 总结一下这次的任务点&#xff1a; 用PlayerState来做一个Credit系统&#xff0c;需要在我们的ui内显示我们的分数更新血药对象&#xff0c;每次使用血药都会扣除相应的分数新增一个金币对象&#xff0c;…

【Python】Sphinx 文档生成器

目录 1. Sphinx 介绍 2. Sphinx 实战 2.1. 初始化 Sphinx 工程 2.2. 编译项目 2.3. Sphinx 主题 2.4. 增加 Sphinx 文档 1. Sphinx 介绍 Sphinx是一个Python文档生成器&#xff0c;它基于reStructuredText标记语言&#xff0c;可自动根据项目生成HTML,PDF等格式的文档。…

使用 OpenCV 进行按位运算和图像屏蔽

在本教程中,我们将了解如何使用按位运算 AND、OR、XOR 和 NOT。 图像处理中使用按位运算从图像中提取感兴趣区域 (ROI)。 正如您所看到的,两个矩形重叠的区域已被删除(黑色),因为在该区域中两个像素都大于 0。 按位非<

浅析代谢组学最常用到的数据分析方法 图形详解pca pls-da opls-da

代谢组学是一门对某一生物或细胞所有低分子质量代谢产物&#xff08;以相对分子质量<1000的有机和无机的代谢物为研究核心区&#xff09;进行分析的新兴学科。生物样本通过NMR、GC-MS、LC-MS等高通量仪器分析检测后&#xff0c;能产生大量的数据&#xff0c;这些数据具有高维…

网页版在线流程图绘制工具Diagram

网页地址&#xff1a;Diagram 可以将流程图保存为图片、网址等多种格式。 界面&#xff1a;

【PortAudio】PortAudio 音频处理库Demo

1. 介绍 PortAudio是一个免费、跨平台、开源的音频I/O库。看到I/O可能就想到了文件&#xff0c;但是PortAudio操作的I/O不是文件&#xff0c;而是音频设备。它能够简化C/C的音频程序的设计实现&#xff0c;能够运行在Windows、Macintosh OS X和UNIX之上&#xff08;Linux的各种…

SAP从入门到放弃系列之生产车间相关单据打印

文章目录概览 一、前言二、系统相关设置2.1、配置:1&#xff1a;2.2、配置点2&#xff1a;2.3、配置点3 三、主数据准备四、测试场景准备五、小结 一、前言 通常在项目实施的时候&#xff0c;如果没有MES&#xff0c;那么生产调度相关岗位下达订单后&#xff08;订单下达感觉没…

K8s部署微服务(springboot+vue)

文章目录 前言一、使用到的K8s资源1.1 Deployment1.2 Service 二、Springboot基础服务部署2.1 网关gateway2.2 鉴权auth2.3 文件file2.4 流程flow2.5 消息message2.6 组织org2.7 系统通用system2.8 用户user2.9 Node 三、Vue前端部署3.1 项目前端nginx3.2 静态资源服务nginx 四…

迪杰斯特拉算法(求最短路径)

迪杰斯特拉算法&#xff08;求最短路径&#xff09; 迪杰斯特拉算法用于查找图中某个顶点到其它所有顶点的最短路径&#xff0c;该算法既适用于无向加权图&#xff0c;也适用于有向加权图。 注意&#xff0c;使用迪杰斯特拉算法查找最短路径时&#xff0c;必须保证图中所有边…

相对位置编码(二) Relative Positional Encodings - Transformer-XL

1. Motivation 在Transformer-XL中&#xff0c;由于设计了segments&#xff0c;如果仍采用transformer模型中的绝对位置编码的话&#xff0c;将不能区分处不同segments内同样相对位置的词的先后顺序。 比如对于segmenti&#xfffd;&#xfffd;&#xfffd;&#xfffd;&…

pycharm安装opencv-python报错

问题一 通过pycharm中的Terminal窗口安装opencv-python错误如下&#xff1a; 上图所示为部分错误&#xff0c;全部错误如下&#xff1a; Building wheel for opencv-contrib-python (PEP 517) ... errorERROR: Complete output from command D:\anzhuanglujing\Anaconda\python…

从零开始之PID控制

从零开始系列之PID控制&#xff0c;宗旨就是以说人话的方式讲述它&#xff0c;真正的做到从零开始&#xff0c;小白一看就会&#xff0c;一学就废。 一、什么是PID控制&#xff1f; PID控制&#xff08;比例-积分-微分控制&#xff09;由比例单元&#xff08;Proportional&…

玩耍的猫咪【 InsCode Stable Diffusion 美图活动一期】

1️⃣ 工具介绍 InsCode是一个集成了在线IDE、在线AI编程、在线算力租赁、在线项目部署以及在线SD 模型使用的综合代码开发平台。 Stable Diffusion是目前最火的AI绘画工具之一&#xff0c;它是一个免费开源的项目。通过Stable Diffusion&#xff0c;可以很轻松的通过文字描述…