基于Android Studio的蓝牙通信的简单应用与开发
实现基于Android Studio的蓝牙通信的简单应用与开发
- 实现蓝牙通信
- 界面展示
- 核心文件
- 部分代码展示
- 总结
实现蓝牙通信
通过权限申请与代码实现,完成蓝牙通信的简单应用与开发。
界面展示
核心文件
三个主要核心java文件:
4个XML文件:
导入相应依赖:
AndroidManifest文件的权限申请:
部分代码展示
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getSupportActionBar().hide(); //隐藏标题栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);}}Toolbar toolbar = findViewById(R.id.toolbar);//创建选项菜单toolbar.inflateMenu(R.menu.option_menu);//选项菜单监听toolbar.setOnMenuItemClickListener(new MyMenuItemClickListener());mTitle = findViewById(R.id.title_left_text);mTitle.setText(R.string.app_name);mTitle = findViewById(R.id.title_right_text);// 得到本地蓝牙适配器mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter == null) {Toast.makeText(this, "蓝牙不可用", Toast.LENGTH_LONG).show();finish();return;}if (!mBluetoothAdapter.isEnabled()) { //若当前设备蓝牙功能未开启Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableIntent, REQUEST_ENABLE_BT); //} else {if (mChatService == null) {setupChat(); //创建会话}}}
//使用Handler对象在UI主线程与子线程之间传递消息private final Handler mHandler = new Handler() { //消息处理@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MESSAGE_STATE_CHANGE:switch (msg.arg1) {case ChatService.STATE_CONNECTED:mTitle.setText(R.string.title_connected_to);mTitle.append(mConnectedDeviceName);mConversationArrayAdapter.clear();break;case ChatService.STATE_CONNECTING:mTitle.setText(R.string.title_connecting);break;case ChatService.STATE_LISTEN:case ChatService.STATE_NONE:mTitle.setText(R.string.title_not_connected);break;}break;case MESSAGE_WRITE:byte[] writeBuf = (byte[]) msg.obj;String writeMessage = new String(writeBuf);mConversationArrayAdapter.add("我: " + writeMessage);break;case MESSAGE_READ:byte[] readBuf = (byte[]) msg.obj;String readMessage = new String(readBuf, 0, msg.arg1);mConversationArrayAdapter.add(mConnectedDeviceName + ": "+ readMessage);break;case MESSAGE_DEVICE_NAME:mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);Toast.makeText(getApplicationContext(),"链接到 " + mConnectedDeviceName, Toast.LENGTH_SHORT).show();break;case MESSAGE_TOAST:Toast.makeText(getApplicationContext(),msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();break;}}};
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><!--新版Android支持的Toolbar,对标题栏布局--><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/title_left_text"style="?android:attr/windowTitleStyle"android:layout_width="0dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_weight="1"android:gravity="left"android:ellipsize="end"android:singleLine="true" /><TextViewandroid:id="@+id/title_right_text"android:layout_width="0dp"android:layout_height="match_parent"android:layout_alignParentRight="true"android:layout_weight="1"android:ellipsize="end"android:gravity="right"android:singleLine="true"android:textColor="#fff" /></LinearLayout></androidx.appcompat.widget.Toolbar><ListView android:id="@+id/in"android:layout_width="match_parent"android:layout_height="match_parent"android:stackFromBottom="true"android:transcriptMode="alwaysScroll"android:layout_weight="1" /><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" ><EditText android:id="@+id/edit_text_out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_gravity="bottom" /><Button android:id="@+id/button_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/send"/></LinearLayout>
</LinearLayout>
总结
- 代码仓库:Gitee-BlueTooth
- 如有错误,敬请指正。
基于Android Studio的蓝牙通信的简单应用与开发相关推荐
- 基于Android Studio经典蓝牙APP---继上一次的完善版
基于Android Studio经典蓝牙APP-继上一次的完善版 考虑到好友网友们反馈的问题总结了以下几点: 1.工程下载爆红:版本问题-gradle:4.1.1. 2.无接收数据功能,怎么实现:这里 ...
- android蓝牙传输脉冲,一种基于Android操作系统的激光测距仪蓝牙通信方法与流程...
本发明属于无线通信技术领域,涉及Android操作系统的应用,具体涉及一种基于Android操作系统的激光测距仪蓝牙通信方法. 背景技术: 目前,随着智能设备的普及,智能操作系统得到了极大的发展,An ...
- 基于Android Studio的五子棋游戏的简单设计
[摘要]: 随着时代的发展,现代科技的飞跃,我们的日常娱乐生活变得丰富多彩.而手机游戏被业内人士称为继通信之后的有一座"金矿",手机休闲娱乐应用将成为PC休闲娱乐之后又一重要业务增 ...
- Android遥控车软件设计,基于Android系统和蓝牙通信的手机遥控车设计
1 设计背景 现有每个普通家庭中的遥控器包括空调遥控器.电视遥控器.DVD遥控器.汽车遥控器.门窗遥控器等,门类繁多且不通用,而另一方面,手机已成为人人必备的工具,如果可以用我们必备的手机去遥控所有这 ...
- android studio实现蓝牙通信
目录 添加蓝牙权限 在文件res/values/strings.xml里,添加程序运行过程中的状态描述文本 编写布局文件添加一个 编写用于蓝牙会话的服务组件ChatService 建立供Bluetoo ...
- 蓝牙聊天App设计3:Android Studio制作蓝牙聊天通讯软件(完结,蓝牙连接聊天,结合生活情景进行蓝牙通信的通俗讲解,以及代码功能实现,内容详细,讲解通俗易懂)
前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是:三.蓝牙连接聊天. 课程1:Android Studio小白安装教程,以及第一个An ...
- (超多图)基于Android studio开发的一个简单入门小应用(超级详细!!)(建议收藏)
基于Android studio开发的一个简单入门小应用 一.前言 二.前期准备 三.开发一个小应用 五.运行应用 一.前言 在暑假期间,我学习JAVA基础,为了能早日实现自己用代码写出一个app的& ...
- 蓝牙聊天App设计1:Android Studio制作蓝牙聊天通讯软件(UI界面设计)
前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是一.UI界面设计 课程1:Android Studio小白安装教程,以及第一个Andr ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(6)、基于Android Studio的ArcGIS Android工程结构解析...
1.前言 Android Studio 是第一个Google官方的 Android 开发环境.其他工具,例如 Eclipse,在 Android Studio 发布之前已经有了大规模的使用.为了帮助开 ...
最新文章
- 中国团队新型类脑芯片登上《自然》封面
- gnome Nautilus cannot handle computer locations
- linux 进程 地址空间 内存分布 简介
- .NET Core RSA密钥的xml、pkcs1、pkcs8格式转换和JavaScript、Java等语言进行对接
- ASP.NET Core EFCore 之Code First
- Python常见问题(5):Python扩展与嵌入 Extending/Embedding FAQ
- 思想实验(逻辑思维)解题
- VC++更改程序图标
- 实例26:python
- 浅谈智能电能表的远程预付费 系统设计
- 多多客api_蚂蚁星球(原好京客)(haojingke.com)- 做大京客,享大数据营销 - 拼多多API接口...
- 会议室管理系统(参考答案)
- 计算机ms高级应用科目一 科目二考什么,科目一、科目二、科目三、科目四都考什么?全都在这儿了!...
- 获取星期--蔡勒公式
- 你可能不知道的canvas烟花秀
- 对物质导数与拉格朗日视角、欧拉视角之间的关系的粗浅理解
- Request Line is too large (xxxx > 4094) 问题处理
- 曾经学舞度芳年(代序)
- 不联网计算机软件升级,不能联网的电脑如何升级病毒库
- 大三使用Turtle库画太极八卦图