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>

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

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

相关文章

设计模式学习(四)——《大话设计模式》

设计模式学习&#xff08;四&#xff09;——《大话设计模式》 1.泛型 允许在编码时使用类型参数&#xff0c;从而使得代码更加灵活、可重用。泛型可以应用于类、接口、方法中&#xff0c;通过这种方式&#xff0c;可以编写出适用于多种数据类型的通用代码&#xff0c;而不是…

如何在CentOS7.x上生成自签名SSL证书

在配置HTTPS连接时&#xff0c;SSL证书是确保数据传输安全性的关键组件。自签名证书是一种不通过证书颁发机构&#xff08;CA&#xff09;签发的证书&#xff0c;适用于测试和内部使用。以下是在CentOS 7.x系统上生成自签名证书的详细步骤。 1. 安装OpenSSL OpenSSL是一个强大…

【嵌入式学习】ARM day04.11

一、思维导图 二、练习 实现三个灯闪烁 汇编代码 .text .global _start _start: 使能GPIOE和F时钟LDR r0,0x50000A28LDR r1,[R0]ORR R1,R1,#(0X3<<4)STR R1,[R0]配置GPIOE和F的MODER寄存器LDR r0,0x50006000 GPIOELDR R1,0X50007000 G…

顶顶通呼叫中心中间件-回铃音补偿(mod_cti基于FreeSWITCH)

顶顶通呼叫中心中间件-回铃音补偿(mod_cti基于FreeSWITCH) 回铃音的用处 回铃音&#xff1a; 当别人打电话给你时&#xff0c;你的电话响铃了&#xff0c;而他听到的声音叫做回铃音。回铃音是被叫方向主叫方传送&#xff0c;也是彩铃功能的基础。我们平时打电话听到的“嘟 嘟…

【C++进阶】C++异常详解

C异常 一&#xff0c;传统处理错误方式二&#xff0c;C处理的方式三&#xff0c;异常的概念四&#xff0c;异常的使用4.1 异常和捕获的匹配原则4.2 函数调用链中异常栈展开匹配原则4.3 异常的重新抛出&#xff08;异常安全问题&#xff09;4.4 RAII思想在异常中的作用 五&#…

C# 设计模式的七大原则详解

文章目录 前言1. 单一职责原则 (SRP)2. 开放封闭原则 (OCP)3. 里氏替换原则 (LSP)4. 依赖倒置原则 (DIP)5. 接口隔离原则 (ISP)6. 合成/聚合复用原则 (CARP)7. 迪米特法则 (LoD) 前言 在 C# 编程中&#xff0c;设计模式的七大原则是保证代码质量和可维护性的基石。这些原则不仅…

2024 Mathorcup高校数学建模挑战赛(B题)| 甲骨文识别 | 建模秘籍文章代码思路大全

铛铛&#xff01;小秘籍来咯&#xff01; 小秘籍团队独辟蹊径&#xff0c;以CNN卷积神经网络&#xff0c;计算机视觉等强大工具&#xff0c;构建了解决复杂问题的独特方案。深度学习, 混沌模型的妙用&#xff0c;为降低非法野生动物贸易提供新视角。通过综合分析&#xff0c;描…

IP广播对讲系统停车场解决方案

IP广播对讲系统停车场解决方案 一、需求分析 随着国民经济和社会的发展&#xff0c; 选择坐车出行的民众越来越多。在保护交通安全的同时&#xff0c;也给停车场服务部门提出了更高的要求。人们对停车场系统提出了更高的要求与挑战&#xff0c; 需要停车场系统提高工作效率与服…

mysql数据库基础--基础操作

加 油 1.数据库基础操作 1.1创建数据库 在对数据表中的数据进行任何操作之前&#xff0c;首先必须创建一个数据库。 基本语法结果&#xff1a; create database 新建数据库名;1.2查看数据库 在MySQL中查看数据库的基本语法&#xff1a; show create database 数据库; 1.…

01-Git 之快速入门操作本地仓库

https://learngitbranching.js.org/?localezh_CN在线练习git 1. Git 安装好Git以后, 先检查是否已经绑定了用户名和邮箱 git config --list1.1 为什么要使用版本控制&#xff1f; 从个人角度&#xff1a; 在做项目时&#xff0c;如果一点点去改代码会很乱&#xff0c;不利…

OpenCV4.9更多形态转换

返回&#xff1a;OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 上一篇:OpenCV4.9处理平滑图像 下一篇:OpenCV4.9更多形态转换 基于这两者&#xff0c;我们可以对图像进行更复杂的转换。在这里&#xff0c;我们简要讨论 OpenCV 提供的 5 个操作&#xff1a; …

FreeBuf 全球网络安全产业投融资观察(3月)

综述 据不完全统计&#xff0c;2024年3月&#xff0c;全球网络安全市场共发生投融资事件53起&#xff0c;其中国内4起&#xff0c;国外49起。 3月全球络安全产业投融资统计表&#xff08;数据来源&#xff1a;航行资本、36氪&#xff09; 整体而言&#xff0c;国内4起投融资事…

EasyExcel中对图片大小及位置进行调整(自定义拦截器)

如何对我们导出的图片设置大小和位置 首先我们先丢入一张图片,这里我就直接通过URL的形式进行编写 定义一个存储图片的类 import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter;import java.net.URL;Getter Setter EqualsAndHashCode public class…

AI写作软件哪个好?这4款好评如潮

在信息时代&#xff0c;AI技术的发展的日新月异&#xff0c;AI写作软件也因此诞生。特别是人们对于高效、便捷的写作工具需求日益增长&#xff0c;AI写作软件作为一种新兴的工具&#xff0c;在帮助人们提升写作效率、拓展创作思路方面发挥着越来越重要的作用。这些AI写作软件为…

C语言 函数——代码风格

目录 基本的代码规范 程序版式 对齐&#xff08;Alignment&#xff09;与缩进&#xff08;indent&#xff09; 变量的对齐规则 空行——分隔程序段落的作用 代码行内的空格——增强单行清晰度 代码行 长行拆分 标识符命名规则 标识符命名的共性规则 windows应用程序…

PostgreSQL入门到实战-第十八弹

PostgreSQL入门到实战 PostgreSQL中表连接操作(二)官网地址PostgreSQL概述PostgreSQL中表别名命令理论PostgreSQL中表别名命令实战更新计划 PostgreSQL中表连接操作(二) 了解PostgreSQL表别名及其实际应用程序。 官网地址 声明: 由于操作系统, 版本更新等原因, 文章所列内容…

19c数据库/dev/shm/过小导致pga内存不够

pga_aggregate_limit已经设置了120G&#xff0c;alert还是报内存不够 查询select * from v$pgastat&#xff0c;发现MGA占了80G内存 查看/dev/shm: 发现设置了7G&#xff0c;操作系统是512G&#xff0c;正常情况下/dev/shm应该是操作系统的一半&#xff0c;修改为250G后数据库…

微信小程序 django+nodejs电影院票务售票选座系统324kd

小程序Android端运行软件 微信开发者工具/hbuiderx uni-app框架&#xff1a;使用Vue.js开发跨平台应用的前端框架&#xff0c;编写一套代码&#xff0c;可编译到Android、小程序等平台。 前端&#xff1a;HTML5,CSS3 VUE 后端&#xff1a;java(springbootssm)/python(flaskdja…

vue3:菜单、标签页和面包屑联动效果

文章目录 1.整体思路2.实现过程 概要 提示&#xff1a;这里可以添加技术概要 例如&#xff1a; openAI 的 GPT 大模型的发展历程。 1.整体思路 在之前做的后台项目中&#xff0c;菜单、标签页和面包屑之间的联动&#xff0c;自己都是通过在路由前置守卫中&#xff0c;定义b…

微服务面试题二

1.什么是雪崩 微服务之间相互调用&#xff0c;因为调用链中的一个服务故障&#xff0c;引起整个链路都无法访问的情况。 如何解决雪崩&#xff1f; 超时处理&#xff1a;请求超时就返回错误信息&#xff0c;不会无休止等待仓壁模式&#xff1a;限定每个业务能使用的线程数&a…