Ktolin-Android studio调用蓝牙模块stepbystep
目的是使用蓝牙模块
源码相关内容已经传到资源上了,点击下载。
1 准备工作
1.1 在app->src->main-> AndroidManifest.xml 的package下增加
<!-- permission add bluetooth--><uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><!-- permission add bluetooth-->
1.2app->build.graddle 的Android 里增加
视图绑定功能可按模块启用。要在某个模块中启用视图绑定,请将 viewBinding
元素添加到其 build.gradle
文件中,
viewBinding {enabled = true}
在 Activity 中使用视图绑定
如需设置绑定类的实例以供 Activity 使用,请在 Activity 的 onCreate()
方法中执行以下步骤:
- 调用生成的绑定类中包含的静态
inflate()
方法。此操作会创建该绑定类的实例以供 Activity 使用。 - 通过调用
getRoot()
方法或使用 Kotlin 属性语法获取对根视图的引用。 - 将根视图传递到
setContentView()
,使其成为屏幕上的活动视图。
private lateinit var binding: ResultProfileBinding // addoverride fun onCreate(savedInstanceState: Bundle) {super.onCreate(savedInstanceState)binding = ResultProfileBinding.inflate(layoutInflater)// addval view = binding.root //setContentView(view) //}
那么引用视图的方法
binding.name.text = viewModel.namebinding.button.setOnClickListener { viewModel.userClicked() }
详细的内容,可参考官网材料。
还可我参考前面的例子 对color.xml 和 theme.xml 增加颜色和主题内容。这里不细说。
1.3 Resource Manager -Drawable 里增加蓝牙图标
clipArts 搜索 blue 即可找到
2 app->main->src->layout->activity_main.xml 布局文件
增加 txtview 显示蓝牙状态,增加蓝牙操控按键。蓝牙图标可从上面的图标动态调整。
<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"tools:context=".MainActivity"><!--text view :display whether the bluetooth is availble or not --><!-- image to show the icon of bluetooth --><!-- Turn on bluetooth --><TextViewandroid:id="@+id/bluetoothStatusTv"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:text=""android:hint="I am here"android:textAlignment="center"android:textColor="@color/blue_200"android:textSize="20sp" /><ImageViewandroid:id="@+id/bluetoothIv"android:layout_width="100dp"android:layout_height="100dp"android:contentDescription="@string/bluetoothiv"android:src="@drawable/ic_bluetooth_off" /><Buttonandroid:id="@+id/turnonBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/TextAppearance.AppCompat.Widget.Button.Colored"android:text="@string/turn_on"android:width="280dp"/><!-- Turn off bluetooth -->
<Buttonandroid:id="@+id/turnOffBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/TextAppearance.AppCompat.Widget.Button.Colored"android:text="@string/turn_off"android:width="280dp"/><!-- Discovered bluetooth --><Buttonandroid:id="@+id/discoveredBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/TextAppearance.AppCompat.Widget.Button.Colored"android:text="@string/discovered"android:width="280dp"/><!-- Get list of bluetooth --><Buttonandroid:id="@+id/pairedBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/TextAppearance.AppCompat.Widget.Button.Colored"android:text="@string/get_paired_device"android:width="280dp"/><!-- Pared bluetooth -->
<TextViewandroid:id="@+id/pairedTv"android:padding="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="show paired bluetooth"android:textColor="@color/blue_200"/><TextViewandroid:id="@+id/showActionTv"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:hint="show action"android:textColor="@color/design_default_color_on_primary"/>
</LinearLayout>
3 mainActivity.kt Code
这里主要是进行交互操作
package com.example.bluetoothimport android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothAdapter.*
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.ScriptGroup
import android.widget.Toast
import com.example.bluetooth.databinding.ActivityMainBindingclass MainActivity : AppCompatActivity() {private val REQUEST_CODE_ENABLE_BT:Int=1private val REQUEST_CODE_DISCOVERED_BT:Int=1// blutetooth adapterlateinit var bAdapter:BluetoothAdapterlateinit var binding: ActivityMainBinding // lateinit can use variable with no init valueoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)binding = ActivityMainBinding.inflate(layoutInflater) //初始化 binding 对象,您将使用该对象访问 activity_main.xml 布局中的 ViewssetContentView(binding.root)//设置 activity 的内容视图,指定应用中视图层次结构的根 binding.root// init bluetooth AdapterbAdapter= BluetoothAdapter.getDefaultAdapter()// check if the bluetooth is onif (bAdapter.isEnabled) binding.bluetoothStatusTv.text="Bluetooth is available"else binding.bluetoothStatusTv.text="Bluetooth is not available"// set image according to bluetooth status// if (bAdapter.isEnabled) binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)// else binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)// binding.showActionTv.text="I will try"// turn on blue toothbinding.turnonBtn.setOnClickListener {if(bAdapter.isEnabled){Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)}else{// Turn on bluetoothvar intent=Intent(ACTION_REQUEST_ENABLE)startActivityForResult(intent,REQUEST_CODE_ENABLE_BT)}binding.showActionTv.text="Turn on button down"}// turn off bluetoothbinding.turnOffBtn.setOnClickListener {if(!bAdapter.isEnabled){Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()}else{// Turn offbAdapter.disable()binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)Toast.makeText(this, "Bluetooth turn off", Toast.LENGTH_SHORT).show()}binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)binding.showActionTv.text="Turn off button down"}//discovered bluetoothbinding.discoveredBtn.setOnClickListener {if (!bAdapter.isDiscovering){Toast.makeText(this, "Making your Bluetooth discovered ", Toast.LENGTH_SHORT).show()val intent=Intent(Intent(ACTION_REQUEST_DISCOVERABLE))startActivityForResult(intent,REQUEST_CODE_DISCOVERED_BT)}binding.showActionTv.text="Discover button down"}// get pairedbinding.pairedBtn.setOnClickListener {if(bAdapter.isEnabled){binding.pairedTv.text="paired Device"val devices=bAdapter.bondedDevicesfor(device in devices){val devicename=device.nameval deviceAddress=devicebinding.pairedTv.append("\n Device: $devicename,$device")}}elseToast.makeText(this, "Turn on your bluetooth first ", Toast.LENGTH_SHORT).show()binding.showActionTv.text="Paired button down"}fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {when(requestCode){REQUEST_CODE_ENABLE_BT ->if (requestCode==Activity.RESULT_OK) {binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()}else Toast.makeText(this, "Could not open Bluetooth", Toast.LENGTH_SHORT).show()}super.onActivityResult(requestCode, resultCode, data)}}}
4 Result
后面步骤 就是 蓝牙通信获得数据。
安装到我的老手机,效果如下。
相关内容已经传到资源上了,点击下载。
Ktolin-Android studio调用蓝牙模块stepbystep相关推荐
- android studio调用python_Android Studio调用python运行thensorflow模型--CLE方案实现
Android Studio调用python运行thensorflow模型--CLE方案实现 Android Studio调用python运行thensorflow模型--CLE方案实现 我使用的是虚 ...
- Android Studio开发——蓝牙聊天功能
Android Studio开发--蓝牙聊天功能 蓝牙工作流程 功能要求 实现要点 声明蓝牙权限 添加程序运行的状态描述文本及配色代码 布局文件 蓝牙会话的服务组件ChatService Activi ...
- Android Studio开发蓝牙应用(二)
Android Studio开发蓝牙应用(二) 实现的功能 与蓝牙模块HC-06交换信息 过程 新建Empty Activity 创建布局 activity_btread_and_write.xml ...
- 蓝牙聊天App设计3:Android Studio制作蓝牙聊天通讯软件(完结,蓝牙连接聊天,结合生活情景进行蓝牙通信的通俗讲解,以及代码功能实现,内容详细,讲解通俗易懂)
前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是:三.蓝牙连接聊天. 课程1:Android Studio小白安装教程,以及第一个An ...
- 基于Android Studio经典蓝牙APP---继上一次的完善版
基于Android Studio经典蓝牙APP-继上一次的完善版 考虑到好友网友们反馈的问题总结了以下几点: 1.工程下载爆红:版本问题-gradle:4.1.1. 2.无接收数据功能,怎么实现:这里 ...
- Android Studio开发蓝牙应用(一)
Android Studio开发蓝牙应用(一) 环境 window 11 安卓12 HC-06蓝牙模块 创建空project 选择Empty Activity,后点击Next 可修改项目名,自定义,后 ...
- 如何在Android Studio中删除模块
本文翻译自:How to delete a module in Android Studio Is there a way to delete a module within Android Stud ...
- Android Studio调用百度地图(二):实现地图显示后台定位和步行导航
先看一下运行效果: 实现功能:后台定位+步行导航(可通过长按屏幕自定义终点,起点为定位点) 后台定位即当程序在后台时依旧执行定位功能,步行导航支持30米-50千米范围内的导航 一 导入SDK并配置相关 ...
- 蓝牙聊天App设计1:Android Studio制作蓝牙聊天通讯软件(UI界面设计)
前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是一.UI界面设计 课程1:Android Studio小白安装教程,以及第一个Andr ...
最新文章
- 11旋转编码器原理_科普小知识:八分钟了解电机编码器!
- Tomcat启动时卡在org.apache.catalina.startup.HostConfig
- PHP 基础篇 - PHP 中 DES 加解密详解
- iPhone入门教程 (视频)
- python module
- 人工智能都这么火了,底层基础架构还有必要开源吗?
- Python Subprocess Popen 管道阻塞问题分析解决
- python数据库-mongoDB的高级查询操作(55)
- leach算法的实现过程_LEACH分簇算法实现和能量控制算法实现
- windows注册表解析说明
- matlab gui算法,MATLAB GUI实现计算器(设计)
- openwrt环境下,使用externel commissioning组网openthread
- Docker问题:ERROR: Pool overlaps with other one on this address space
- Java面向对象学习:遥控器
- Ubuntu-pyqt5+qtDesigner安装手顺
- 全志F1C100s主线linux入坑记录 (1)linux系统移植
- Latex 如何给数学公式进行编号
- 编写名为censor的函数,用来把字符串中出现的每一处字母“foo”替换成“xxx”。例如,字符串“food fool”会变为“xxxd xxxl”。再不失清晰性的前提下程序越短越好
- 开源SSL加快器的构建
- 任何的Linux开发板+ADB+fastboot驱动-----真实有效!
热门文章
- 《电机学》第四篇 异步电机 第13、14三相异步电动机 原理/结构/特性 第15章 单相异步电动机
- 理解线性回归中的常数项
- ssm+jsp计算机毕业设计房地产成本管理系统p17k1(程序+LW+源码+远程部署)
- 字体font复合属性
- Linux下 配置NTP时间服务器
- 汽车4S店管理全攻略
- macOS之如何删除插入u盘后会生成的._开头的隐藏文件
- golang学习笔记(19)-gin路由分组和中间件
- matlab鼠标三维坐标点,请问如何用matlab画三维点,已知x,y,z的坐标,在三维坐标系上显示...
- 如何成为新云原生企业,这个“过来人”告诉你