当前位置: 首页 > news >正文

Discoverydevice.java和activity_discoverydevice.xml

一、Discoverydevice.java

public class Discoverydevice extends AppCompatActivity {private DeviceAdapter   mAdapter2;private final List<DeviceClass> mbondDeviceList = new ArrayList<>();//搜索到的所有已绑定设备保存为列表private final List<DeviceClass> mfindDeviceList = new ArrayList<>();//搜索到的所有未绑定设备保存为列表private final BluetoothController mbluetoothController = new BluetoothController();private Toast mToast;private Button button;private BluetoothAdapter bluetoothAdapter;@SuppressLint("MissingPermission")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_discoverydevice);bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();/*       View rootView = findViewById(android.R.id.content);开始扫描蓝牙设备View updatedView = findDevice(rootView);*/button = findViewById(R.id.button1);button.setOnClickListener(v -> {init_Filter();//初始化广播并打开Init_listView();//初始化设备列表findDevice(v);});ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});}//初始化蓝牙权限private void Init_Bluetooth() {mbluetoothController.enableVisibily(this);//让其他蓝牙看得到我mbluetoothController.turnOnBlueTooth(this, 0);//打开蓝牙}//初始化列表,适配器的加载public void Init_listView() {mAdapter2 = new DeviceAdapter(Discoverydevice.this, R.layout.device_item, mfindDeviceList);ListView listView2 = findViewById(R.id.listview2);listView2.setAdapter(mAdapter2);mAdapter2.notifyDataSetChanged();listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {@SuppressLint("MissingPermission")@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {DeviceClass device = mfindDeviceList.get(position); // 获取点击的设备信息String deviceAddress = device.getbAdress();String deviceName= device.getbName();Intent resultIntent = new Intent( );resultIntent.putExtra("bluetoothName", deviceName);resultIntent.putExtra("bluetoothAddress", deviceAddress);// 设置结果代码为 RESULT_OK,表示连接成功setResult(Discoverydevice.RESULT_OK, resultIntent);// 关闭当前活动finish();showToast("连接中...");}});Init_Bluetooth();}//开启广播private void init_Filter() {IntentFilter filter = new IntentFilter();filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //连接蓝牙,断开蓝牙//开始查找filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//结束查找filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//查找设备 蓝牙发现新设备(未配对的设备)filter.addAction(BluetoothDevice.ACTION_FOUND);//设备扫描模式改变filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//绑定状态 设备配对状态改变filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);//在系统弹出配对框之前(确认/输入配对码)filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//最底层连接建立filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//最底层连接断开filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); //BluetoothAdapter连接状态filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); //BluetoothHeadset连接状态filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); //BluetoothA2dp连接状态registerReceiver(receiver, filter);}//广播内容private final BroadcastReceiver receiver = new BroadcastReceiver() {@SuppressLint("MissingPermission")@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//开始查找if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {change_Button_Text("搜索中...", "DISABLE");
//                showToast("搜索中..." );mfindDeviceList.clear();mAdapter2.notifyDataSetChanged(); //刷新列表适配器,将内容显示出来
//                }}//结束查找else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//                showToast("搜索设备..." );change_Button_Text("扫描设备", "ENABLE");}//查找设备else if (BluetoothDevice.ACTION_FOUND.equals(action)) {change_Button_Text("搜索中...", "DISABLE");
//                showToast("搜索中..." );if (device != null && device.getName() != null && !device.getName().isEmpty()) {change_Button_Text("搜索中...", "DISABLE");// showToast("搜索中...");//查找到一个设备且设备名不为空就添加到列表类中mfindDeviceList.add(new DeviceClass(device.getName(), device.getAddress()));mAdapter2.notifyDataSetChanged(); //刷新列表适配器,将内容显示出来show_bondDevice();}}//设备扫描模式改变else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, 0);if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {showToast("true");} else {showToast("false");}} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);if (remoteDevice == null) {return;}int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 0);if (status == BluetoothDevice.BOND_BONDED) {showToast("已绑定---" + remoteDevice.getName());} else if (status == BluetoothDevice.BOND_BONDING) {showToast("正在绑定---" + remoteDevice.getName());} else if (status == BluetoothDevice.BOND_NONE) {showToast("未绑定---" + remoteDevice.getName());}}}};//点击开始查找蓝牙设备public void findDevice(View view) {mbluetoothController.findDevice();}// 判断是否连接  显示已连接设备信息private void show_bondDevice() {bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {@SuppressLint("MissingPermission")@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.A2DP) {List<BluetoothDevice> devices = proxy.getConnectedDevices();if (!devices.isEmpty()) {BluetoothDevice connectedDevice = devices.get(0);mbondDeviceList.clear();mbondDeviceList.add(new DeviceClass(connectedDevice.getName(), connectedDevice.getAddress()));//  button.setText(text);} else {// 展示已经配对的蓝牙设备show_bondDeviceList();}}}@Overridepublic void onServiceDisconnected(int profile) {// 展示已经配对的蓝牙设备show_bondDeviceList();}};bluetoothAdapter.getProfileProxy(Discoverydevice.this, profileListener, BluetoothProfile.A2DP);}// 未连接@SuppressLint("MissingPermission")private void show_bondDeviceList() {mbondDeviceList.clear();}//点击按键搜索后按键的变化private void change_Button_Text(String text, String state) {if ("ENABLE".equals(state)) {button.setEnabled(true);button.getBackground().setAlpha(255); //0~255 之间任意调整button.setTextColor(ContextCompat.getColor(this, R.color.black));} else {button.setEnabled(false);button.getBackground().setAlpha(150); //0~255 之间任意调整button.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));}button.setText(text);}//设置toast的标准格式private void showToast(String text) {if (mToast == null) {mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);mToast.show();} else {mToast.setText(text);mToast.show();}}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(receiver);}}

二、 activity_discoverydevice.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#eeeeee"android:orientation="vertical"tools:context=".Discoverydevice"><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/button_style"android:text="扫描设备"android:textColor="#000000" /><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50px"android:text="附近设备" /><ListViewandroid:id="@+id/listview2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20px"android:background="@drawable/listview_style1" /> 
</LinearLayout>

相关文章:

Discoverydevice.java和activity_discoverydevice.xml

一、Discoverydevice.java public class Discoverydevice extends AppCompatActivity {private DeviceAdapter mAdapter2;private final List<DeviceClass> mbondDeviceList new ArrayList<>();//搜索到的所有已绑定设备保存为列表private final List<Devic…...

华为OD机试 - 最多颜色的车辆(Java JS Python C C++)

须知 哈喽,本题库完全免费,收费是为了防止被爬,大家订阅专栏后可以私信联系退款。感谢支持 文章目录 须知题目描述输入描述输出描述解析代码题目描述 在一个狭小的路口,每秒只能通过一辆车,假设车辆的颜色只有 3 种,找出 N 秒内经过的最多颜色的车辆数量。 三种颜色编…...

【无人机/平衡车/机器人】详解STM32+MPU6050姿态解算—卡尔曼滤波+四元数法+互补滤波——附3个算法源码

效果: MPU6050姿态解算-卡尔曼滤波+四元数+互补滤波 目录 基础知识详解 欧拉角...

NzN的C++之路--构造函数与析构函数

如果一个类中既没有成员变量也没有成员函数&#xff0c;这个类简称为空类。空类中其实并不是什么都没有&#xff0c;任何类在什么都不写时&#xff0c;编译器会自动生成6个默认成员函数。那今天我们就来学习一下其中两个默认成员函数&#xff1a;构造函数与析构函数。先三连后看…...

【算法刷题day24】Leetcode:216. 组合总和 III、17. 电话号码的字母组合

文章目录 Leetcode 216. 组合总和 III解题思路代码总结 Leetcode 17. 电话号码的字母组合解题思路代码总结 草稿图网站 java的Deque Leetcode 216. 组合总和 III 题目&#xff1a;216. 组合总和 III 解析&#xff1a;代码随想录解析 解题思路 回溯三部曲&#xff1a;确定递归…...

一体化泵站的生产制造流程怎样

诸城市鑫淼环保小编带大家了解一下一体化泵站的生产制造流程怎样 综合泵站和传统式混泥土泵站的一大差别是&#xff0c;离去制造厂前&#xff0c;能够开展全部机械设备设备的生产加工及零部件加工&#xff0c;随后运送到建筑项目当场开展安裝。这类经营方式缩短了开发周期&…...

【1】C++设计模式之【单例模式】

单例模式在C中的实现方式有以下几种&#xff1a; 懒汉式&#xff08;线程不安全&#xff09;饿汉式&#xff08;线程安全&#xff09;双检锁/双重校验锁&#xff08;DCL&#xff0c;线程安全&#xff09;静态局部变量&#xff08;线程安全&#xff09;C11版本&#xff08;线程…...

软件设计模式之解释器模式

一、引言 在软件设计中&#xff0c;我们经常遇到需要“解释”和执行某种特定语法或语言的情况。这时&#xff0c;解释器模式就派上了用场。解释器模式&#xff08;Interpreter Pattern&#xff09;是一种行为设计模式&#xff0c;它提供了一种解释语言的语法并定义一个句子如何…...

java Web课程管理系统用eclipse定制开发mysql数据库BS模式java编程jdbc

一、源码特点 JSP 课程管理系统是一套完善的web设计系统&#xff0c;对理解JSP java 编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,eclipse开发&#xff0c;数据库为Mysql5.0&#xff0c;使用ja…...

Electron 桌面端应用的使用 ---前端开发

Electron是什么&#xff1f; Electron是一个使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium 和 Node.js 到 二进制的 Electron 允许您保持一个 JavaScript 代码代码库并创建 在Windows上运行的跨平台应用 macOS和Linux——不需要本地开发 经验。 入门…...

【SpringBoot:详解Bean装配】

&#x1f3e1;Java码农探花&#xff1a; &#x1f525; 推荐专栏&#xff1a;<springboot学习> &#x1f6f8;学无止境&#xff0c;不骄不躁&#xff0c;知行合一 文章目录 前言一、IoC容器的简介BeanFactory接口源码二、Bean装配扫描装配探索启动类条件装配自定义Bean总…...

前端如何将接口返回的码值转成对应的中文展示呢?

后端接口只返回码值,那前端如何将码值转成对应的中文展示呢? 项目中后端接口都是将码值返给前端,前端通过公共获取码值的接口然后将其对应转码 以下是举例操作: created() {//这是公共接口的码值表let oneType [{value: 01,content: 一类,},{value: 02,content: 二类,},];//…...

智慧公厕中的大数据、云计算和物联网技术引领未来公厕管理革命

现代社会对于公共卫生和环境保护的要求越来越高&#xff0c;智慧公厕作为城市基础设施建设的重要组成部分&#xff0c;正引领着公厕管理的革命。随着科技的不断进步&#xff0c;大数据、云计算和物联网技术的应用为智慧公厕带来了全新的可能性&#xff0c;&#xff08;ZonTree中…...

Excel与项目管理软件比较?哪个是项目组合管理的最佳选择?

在定义和管理每个正在进行的项目的资源、任务、收益、风险和优先级时&#xff0c;项目组合管理已成为公司的战略要素。为了实现高效的项目组合管理&#xff0c;PMO 经理需要评估Excel 是否满足他们管理项目组合的需求&#xff0c;或者是否应该尝试不同的解决方案&#xff0c;例…...

过程控制风格的软件架构设计概念及其实际应用

摘要 过程控制风格的软件架构设计强调程序的流程控制逻辑和组件之间的交互方式&#xff0c;旨在提升系统的响应性、扩展性和可维护性。这种架构风格在需要严格的操作序列和流程控制的应用中尤为重要&#xff0c;例如在嵌入式系统、实时系统和复杂的业务流程管理中。本文将介绍…...

WPF 编辑器模式中隐藏/显示该元素

XAML中引用&#xff1a;xmlns:d"http://schemas.microsoft.com/expression/blend/2008" 在所需要的控件中加上d:Visibility"Visible"属性 d:Visibility属性有3个值&#xff0c;可以根据需要进行设置 转自&#xff1a;在Visual Studio设计器中隐藏WPF元素…...

分布式事务 - 个人笔记 @by_TWJ

目录 1. 传统事务1.1. 事务特征1.2. 事务隔离级别1.2.1. 表格展示1.2.2. oracle和mysql可支持的事务隔离级别 2. 分布式事务2.1. CAP指标2.2. BASE理论2.3. 7种常见的分布式事务方案2.3.1. 2PC2.3.2. 3PC2.3.3. TCC2.3.3.1. TCC的注意事项&#xff1a;2.3.3.2. TCC方案的优缺点…...

解决前端笔记本电脑屏幕显示缩放比例125%、150%对页面大小的影响问题--数据可视化大屏

近期在工作中遇到一个问题&#xff0c;记录一下&#xff0c;在项目上线之后&#xff0c;遇到一个问题&#xff0c;即缩放到90%时&#xff0c;页面字体比默认的100%字体大&#xff0c;一开始毫无头绪&#xff0c;经过一番的Google...Google...Google....&#xff0c;终于找到了解…...

【PG-1】PostgreSQL体系结构概述

1. PostgreSQL体系结构概述 代码结构 其中&#xff0c;backend是后端核心代码&#xff0c;包括右边的几个dir: access&#xff1a;处理数据访问方法和索引的代码。 bootstrap&#xff1a;数据库初始化相关的代码。 catalog&#xff1a;系统目录&#xff08;如表和索引的元数据…...

jq命令简易教程——Linux中处理JSON数据的利器

在shell脚本中&#xff0c;当我们需要对JSON数据&#xff08;例如ceph、kubernetes等一些命令的输出&#xff0c;或是调用API获得的响应&#xff09;进行处理和提取时&#xff0c;如果使用传统的文本三剑客sed、awk和grep&#xff0c;命令将会非常臃肿不可读。虽然这三个命令在…...

智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql

智慧工地管理云平台系统&#xff0c;智慧工地全套源码&#xff0c;java版智慧工地源码&#xff0c;支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求&#xff0c;提供“平台网络终端”的整体解决方案&#xff0c;提供劳务管理、视频管理、智能监测、绿色施工、安全管…...

《通信之道——从微积分到 5G》读书总结

第1章 绪 论 1.1 这是一本什么样的书 通信技术&#xff0c;说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号&#xff08;调制&#xff09; 把信息从信号中抽取出来&am…...

Psychopy音频的使用

Psychopy音频的使用 本文主要解决以下问题&#xff1a; 指定音频引擎与设备&#xff1b;播放音频文件 本文所使用的环境&#xff1a; Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

初探Service服务发现机制

1.Service简介 Service是将运行在一组Pod上的应用程序发布为网络服务的抽象方法。 主要功能&#xff1a;服务发现和负载均衡。 Service类型的包括ClusterIP类型、NodePort类型、LoadBalancer类型、ExternalName类型 2.Endpoints简介 Endpoints是一种Kubernetes资源&#xf…...

解决:Android studio 编译后报错\app\src\main\cpp\CMakeLists.txt‘ to exist

现象&#xff1a; android studio报错&#xff1a; [CXX1409] D:\GitLab\xxxxx\app.cxx\Debug\3f3w4y1i\arm64-v8a\android_gradle_build.json : expected buildFiles file ‘D:\GitLab\xxxxx\app\src\main\cpp\CMakeLists.txt’ to exist 解决&#xff1a; 不要动CMakeLists.…...

STM32---外部32.768K晶振(LSE)无法起振问题

晶振是否起振主要就检查两个1、晶振与MCU是否兼容&#xff1b;2、晶振的负载电容是否匹配 目录 一、判断晶振与MCU是否兼容 二、判断负载电容是否匹配 1. 晶振负载电容&#xff08;CL&#xff09;与匹配电容&#xff08;CL1、CL2&#xff09;的关系 2. 如何选择 CL1 和 CL…...

协议转换利器,profinet转ethercat网关的两大派系,各有千秋

随着工业以太网的发展&#xff0c;其高效、便捷、协议开放、易于冗余等诸多优点&#xff0c;被越来越多的工业现场所采用。西门子SIMATIC S7-1200/1500系列PLC集成有Profinet接口&#xff0c;具有实时性、开放性&#xff0c;使用TCP/IP和IT标准&#xff0c;符合基于工业以太网的…...

Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解

文章目录 1. 题目描述1.1 链表节点定义 2. 理解题目2.1 问题可视化2.2 核心挑战 3. 解法一&#xff1a;HashSet 标记访问法3.1 算法思路3.2 Java代码实现3.3 详细执行过程演示3.4 执行结果示例3.5 复杂度分析3.6 优缺点分析 4. 解法二&#xff1a;Floyd 快慢指针法&#xff08;…...

针对药品仓库的效期管理问题,如何利用WMS系统“破局”

案例&#xff1a; 某医药分销企业&#xff0c;主要经营各类药品的批发与零售。由于药品的特殊性&#xff0c;效期管理至关重要&#xff0c;但该企业一直面临效期问题的困扰。在未使用WMS系统之前&#xff0c;其药品入库、存储、出库等环节的效期管理主要依赖人工记录与检查。库…...