目的是使用蓝牙模块

源码相关内容已经传到资源上了,点击下载。

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() 方法中执行以下步骤:

  1. 调用生成的绑定类中包含的静态 inflate() 方法。此操作会创建该绑定类的实例以供 Activity 使用。
  2. 通过调用 getRoot() 方法或使用 Kotlin 属性语法获取对根视图的引用。
  3. 将根视图传递到 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相关推荐

  1. android studio调用python_Android Studio调用python运行thensorflow模型--CLE方案实现

    Android Studio调用python运行thensorflow模型--CLE方案实现 Android Studio调用python运行thensorflow模型--CLE方案实现 我使用的是虚 ...

  2. Android Studio开发——蓝牙聊天功能

    Android Studio开发--蓝牙聊天功能 蓝牙工作流程 功能要求 实现要点 声明蓝牙权限 添加程序运行的状态描述文本及配色代码 布局文件 蓝牙会话的服务组件ChatService Activi ...

  3. Android Studio开发蓝牙应用(二)

    Android Studio开发蓝牙应用(二) 实现的功能 与蓝牙模块HC-06交换信息 过程 新建Empty Activity 创建布局 activity_btread_and_write.xml ...

  4. 蓝牙聊天App设计3:Android Studio制作蓝牙聊天通讯软件(完结,蓝牙连接聊天,结合生活情景进行蓝牙通信的通俗讲解,以及代码功能实现,内容详细,讲解通俗易懂)

    前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是:三.蓝牙连接聊天. 课程1:Android Studio小白安装教程,以及第一个An ...

  5. 基于Android Studio经典蓝牙APP---继上一次的完善版

    基于Android Studio经典蓝牙APP-继上一次的完善版 考虑到好友网友们反馈的问题总结了以下几点: 1.工程下载爆红:版本问题-gradle:4.1.1. 2.无接收数据功能,怎么实现:这里 ...

  6. Android Studio开发蓝牙应用(一)

    Android Studio开发蓝牙应用(一) 环境 window 11 安卓12 HC-06蓝牙模块 创建空project 选择Empty Activity,后点击Next 可修改项目名,自定义,后 ...

  7. 如何在Android Studio中删除模块

    本文翻译自:How to delete a module in Android Studio Is there a way to delete a module within Android Stud ...

  8. Android Studio调用百度地图(二):实现地图显示后台定位和步行导航

    先看一下运行效果: 实现功能:后台定位+步行导航(可通过长按屏幕自定义终点,起点为定位点) 后台定位即当程序在后台时依旧执行定位功能,步行导航支持30米-50千米范围内的导航 一 导入SDK并配置相关 ...

  9. 蓝牙聊天App设计1:Android Studio制作蓝牙聊天通讯软件(UI界面设计)

    前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是一.UI界面设计 课程1:Android Studio小白安装教程,以及第一个Andr ...

最新文章

  1. 怎么修改网页服务器数据库连接,如何修改网页服务器数据库连接
  2. 干货 | 上手机器学习,从搞懂这十大经典算法开始
  3. 详解 Mysql LEFT JOIN和JOIN查询区别及原理
  4. python语言怎么学-如何从零开始学习Python,python语言编程入门
  5. 7、MySQL数据类型的选择
  6. CUDA中的一些基本概念
  7. Linux定期执行xshell脚本(入门)
  8. VMware Workstation Pro 安装教程
  9. 基于WEB的PDF打印——浏览器中静默打印PDF文件
  10. 整车电子电气架构EEA
  11. php 改变图片大小,如何把照片尺寸改小 电子照片尺寸怎么改
  12. 2010节假日安排通知
  13. lack名词形式_lack用法
  14. Mac 终端 oh-my-zsh 配置,内含解决oh-my-zsh 下载不下来的方法
  15. HTML学生个人网站作业设计:电影网站设计——电影购票项目(9页) HTML+CSS+JavaScript 简单DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
  16. 飞机黑匣子结构:拆解神秘的飞机黑匣子,看看里面的PCB和元器件都长什么样?...
  17. 布袋除尘器过滤风速多少_袋式除尘器过滤风速一般多大
  18. win10关闭自动更新(针对已经关闭windows update项,但系统仍自动更新的问题)
  19. 中国各省份省会地图json数据
  20. [Ahoi2014Jsoi2014]骑士游戏(SPFA)

热门文章

  1. Flask使用json或jsonify返回响应的数据
  2. PHP基础学习第十三篇(了解PHP的作用、PHP的语法、PHP的安装、PHP的开发工具、变量、输出(echo与print)、EOF(heredoc)多行字符串理解、最后总结)
  3. pjsip开发——sip日志分析
  4. unity 学习_宣布全新的学习平台Unity学习
  5. 燕山大学计算机毕业论文,燕山大学本科(毕业论文)格式.doc
  6. Android开发相关下载汇总
  7. LCP Array(排列组合)
  8. 剑指千亿 宝龙迈向星辰大海
  9. 三坐标检测之测量同心度时要注意的问题
  10. 安卓系统耗电太快?针对wakelock(唤醒锁)的设置优化教程