当前位置: 首页 > 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;命令将会非常臃肿不可读。虽然这三个命令在…...

基于算法竞赛的c++编程(28)结构体的进阶应用

结构体的嵌套与复杂数据组织 在C中&#xff0c;结构体可以嵌套使用&#xff0c;形成更复杂的数据结构。例如&#xff0c;可以通过嵌套结构体描述多层级数据关系&#xff1a; struct Address {string city;string street;int zipCode; };struct Employee {string name;int id;…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

日语AI面试高效通关秘籍:专业解读与青柚面试智能助攻

在如今就业市场竞争日益激烈的背景下&#xff0c;越来越多的求职者将目光投向了日本及中日双语岗位。但是&#xff0c;一场日语面试往往让许多人感到步履维艰。你是否也曾因为面试官抛出的“刁钻问题”而心生畏惧&#xff1f;面对生疏的日语交流环境&#xff0c;即便提前恶补了…...

超短脉冲激光自聚焦效应

前言与目录 强激光引起自聚焦效应机理 超短脉冲激光在脆性材料内部加工时引起的自聚焦效应&#xff0c;这是一种非线性光学现象&#xff0c;主要涉及光学克尔效应和材料的非线性光学特性。 自聚焦效应可以产生局部的强光场&#xff0c;对材料产生非线性响应&#xff0c;可能…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

逻辑回归:给不确定性划界的分类大师

想象你是一名医生。面对患者的检查报告&#xff08;肿瘤大小、血液指标&#xff09;&#xff0c;你需要做出一个**决定性判断**&#xff1a;恶性还是良性&#xff1f;这种“非黑即白”的抉择&#xff0c;正是**逻辑回归&#xff08;Logistic Regression&#xff09;** 的战场&a…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

Java 加密常用的各种算法及其选择

在数字化时代&#xff0c;数据安全至关重要&#xff0c;Java 作为广泛应用的编程语言&#xff0c;提供了丰富的加密算法来保障数据的保密性、完整性和真实性。了解这些常用加密算法及其适用场景&#xff0c;有助于开发者在不同的业务需求中做出正确的选择。​ 一、对称加密算法…...

现代密码学 | 椭圆曲线密码学—附py代码

Elliptic Curve Cryptography 椭圆曲线密码学&#xff08;ECC&#xff09;是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础&#xff0c;例如椭圆曲线数字签…...