制作蓝牙小车

制作控制蓝牙小车app

想制作一个蓝牙小车,通过手机app程序操控小车运行,制作分三个部分(app制作,蓝牙小车硬件制作,小车程序制作),先完成第一个部分app制作,本次app是通过androidstudio软件来制作安卓应用程序

一、添加权限

在AndroidManifest.xml文件中添加权限

  <!-- 蓝牙操作权限 --><uses-permission android:name="android.permission.BLUETOOTH"/><!-- 蓝牙配对权限--><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><!--仅在支持BLE(蓝牙4.0及以上)的设备上运行--><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/><!--如果Android6.0蓝牙搜索不到设备,需要补充以下两个权限--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

二、设计界面

这里需要新建一个连接蓝牙的界面以及活动,这里新建的连接蓝牙活动取名Bluetooth_set

主界面
在这里插入图片描述
连接蓝牙界面
在这里插入图片描述
界面设计比较简单,无非就是布局和控件id设置

三、功能实现

MainActivity.java文件代码

package com.example.myapplication_ble_hc7;import androidx.appcompat.app.AppCompatActivity;import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import java.io.IOException;import static com.example.myapplication_ble_hc7.Bluetooth_set.bluetoothSocket;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button_set =findViewById(R.id.button_set);button_set.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this, Bluetooth_set.class);startActivity(intent);//跳转到设置界面}});final Button button_go =findViewById(R.id.button_go);button_go.setBackgroundColor(Color.GREEN);final Button button_left =findViewById(R.id.button_left);button_left.setBackgroundColor(Color.GREEN);final Button button_right =findViewById(R.id.button_right);button_right.setBackgroundColor(Color.GREEN);final Button button_stop =findViewById(R.id.button_back);button_stop.setBackgroundColor(Color.GREEN);TextView textView =findViewById(R.id.textView2);if(bluetoothSocket==null){textView.setText("蓝牙未经连接");textView.setBackgroundColor(Color.RED);}else {textView.setText("蓝牙已经连接");textView.setBackgroundColor(Color.BLUE);}button_go.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {switch (motionEvent.getAction()){case MotionEvent.ACTION_DOWN:send(1);button_go.setBackgroundColor(Color.RED);break;case MotionEvent.ACTION_UP:send(0);button_go.setBackgroundColor(Color.GREEN);break;}return true;}});button_left.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {switch (motionEvent.getAction()){case MotionEvent.ACTION_DOWN:send(2);button_left.setBackgroundColor(Color.RED);break;case MotionEvent.ACTION_UP:send(0);button_left.setBackgroundColor(Color.GREEN);break;}return true;}});button_right.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {switch (motionEvent.getAction()){case MotionEvent.ACTION_DOWN:send(3);button_right.setBackgroundColor(Color.RED);break;case MotionEvent.ACTION_UP:send(0);button_right.setBackgroundColor(Color.GREEN);break;}return true;}});button_stop.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {switch (motionEvent.getAction()){case MotionEvent.ACTION_DOWN:send(4);button_stop.setBackgroundColor(Color.RED);break;case MotionEvent.ACTION_UP:send(0);button_stop.setBackgroundColor(Color.GREEN);break;}return true;}});}public void send(int intData){if(bluetoothSocket==null) {//先判断是否连接Toast.makeText(MainActivity.this,"设备未连接",Toast.LENGTH_SHORT).show();}else {try {bluetoothSocket.getOutputStream().write(intData);//建立数据库} catch (IOException e) { }}}
}

在Bluetooth_set.java文件中代码

package com.example.myapplication_ble_hc7;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;public class Bluetooth_set extends AppCompatActivity {public static BluetoothSocket bluetoothSocket;UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//符合uuid格式就行ArrayList<String> ble_list =new ArrayList<>();//创建数组列表ArrayList<BluetoothDevice> ble=new ArrayList<>();//用来存放蓝牙设备@SuppressLint("MissingPermission")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bluetooth_set);Button button_back = findViewById(R.id.button_back);ListView listView =findViewById(R.id.ble_list);button_back.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(Bluetooth_set.this, MainActivity.class);startActivity(intent);//返回到主界面}});BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//获取设备if (bluetoothAdapter == null) {//判断设备是否支持蓝牙Toast.makeText(Bluetooth_set.this, "注意:设备不支持蓝牙", Toast.LENGTH_SHORT).show();} else {Toast.makeText(Bluetooth_set.this, "设备支持蓝牙", Toast.LENGTH_SHORT).show();}if (!bluetoothAdapter.isEnabled()) { //判断设备是否打开蓝牙// bluetoothAdapter.enable();//打开蓝牙Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent,1);   //通过意图打开蓝牙}Set<BluetoothDevice> device = bluetoothAdapter.getBondedDevices();//获取已经配对的设备,并存放到列表if(device.size()>0){for(BluetoothDevice mdevice:device){ble.add(mdevice);//添加蓝牙ble_list.add(mdevice.getName());//将获取的蓝牙名称添加到列表}}ArrayAdapter<String> view_list=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,ble_list);//创建列表显示的适配器listView.setAdapter(view_list);//显示在列表里面listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//   BluetoothDevice bluetoothDevice =ble.get(i);//获取需要单击的蓝牙try {bluetoothSocket=ble.get(i).createInsecureRfcommSocketToServiceRecord(MY_UUID);//获取需要单击的蓝牙,并且连接填入UUIDbluetoothSocket.connect();//蓝牙连接} catch (IOException e) {}Toast.makeText(Bluetooth_set.this, "蓝牙:"+ble.get(i).getName()+"已经连接", Toast.LENGTH_SHORT).show();}});}
}

四、效果呈现

把蓝牙先连接到电脑
在这里插入图片描述
安卓设备连接蓝牙并发送数据,下面是接收数据情况,我这边分别使用0,1,2,3,4表示停、前进、左转、右转、后退
在这里插入图片描述
第一阶段app程序暂时通过验证,接下来制作蓝牙小车

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

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

相关文章

MongoDB知识总结

这里写自定义目录标题 MongoDB基本介绍MongoDB基本操作数据库相关集合相关增删改查 MongoDB基本介绍 简单介绍 MongoDB是一个基于分布式文件存储的数据库。由C语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非关系数据库之间的产…

【Hive】——数据仓库

1.1 数仓概念 数据仓库&#xff08;data warehouse&#xff09;&#xff1a;是一个用于存储&#xff0c;分析&#xff0c;报告的数据系统 目的&#xff1a;是构建面向分析的集成化数据环境&#xff0c;分析结果为企业提供决策支持 特点&#xff1a; 数据仓库本身不产生任何数据…

Spring Boot学习随笔-SpringBoot的引言,回顾传统SSM开发

学习视频&#xff1a;【编程不良人】2021年SpringBoot最新最全教程 第一章、传统SSM开发回顾以及问题 Spring SpringMVC Mybatis SSM 实现一个简单功能 员工添加、查询… SSM项目简单实现 项目 需求分析 —>概要设计 —>&#xff08;库表设计&#xff09; —> 详细…

从零开始的c语言日记day40——字符函数和字符串函数——内存函数

常用函数介绍 求字符串长度 strlen 长度不受限制的字符串函数 Strcpy Strcat strcmp 长度受限制的字符串函数介绍 strncpy strncat strncmp 字符串查找 Strstro strtok 错误信息报告 strerror 字符操作 内存操作函数 memcpy memmove memset Memcmp 使用Asser…

点击el-tree小三角后去除点击后的高亮背景样式,el-tree样式修改

<div class"videoTree" v-loading"loadingTree" element-loading-text"加载中..." element-loading-spinner"el-icon-loading" element-loading-background"rgba(0, 0, 0, 0.8)" > <el-tree :default-expand-all&q…

鸿蒙4.0开发笔记之ArkTS语法基础之应用生命周期与页面中组件的生命周期(十六)

文章目录 一、应用生命周期二、生命周期函数定义三、生命周期五函数练习 一、应用生命周期 1、定义 应用生命周期就是代表了一个HarmonyOS应用中所有页面从创建、开启到销毁等过程的全生命周期。查看路径如下&#xff1a; Project/entry/src/main/ets/entryability/EntryAbili…

17、XSS——session攻击

文章目录 一、session攻击简介二、主要攻击方式2.1 预测2.2 会话劫持2.3 会话固定 一、session攻击简介 session对于web应用是最重要&#xff0c;也是最复杂的。对于web应用程序来说&#xff0c;加强安全性的首要原则就是&#xff1a;不要信任来自客户端的数据&#xff0c;一定…

Spring Boot与Mybatis基础配置(手动写增删改查)

一、 配置 1.新建项目 1.项目基础配置 解释&#xff1a;记得把这个改成start.aliyun.com要不没有java8也就是jdk1.8 2.项目依赖配置 2.配置maven 配置前&#xff1a; 配置后&#xff1a; 3.创建子项目并配置父子项目pom.xml 配置父pom.xml 声明当前项目不是要打成jar包的…

NFC和蓝牙在物联网中有什么意义?如何选择?

#NFC物联网# #蓝牙物联网# 在物联网中&#xff0c;NFC和蓝牙有什么意义&#xff1f; NFC在物联网中代表近场通信技术。它是一种短距离、高频的无线通信技术&#xff0c;可以在近距离内实现设备间的数据传输和识别。NFC技术主要用于移动支付、电子票务、门禁、移动身份识别、防…

利用阿里云 DDoS、WAF、CDN 和云防火墙为在线业务赋能

在这篇博客中&#xff0c;我们将详细讨论使用阿里云 CDN 和安全产品保护您的在线业务所需的步骤。 方案描述 创新技术的快速发展为世界各地的在线业务带来了新的机遇。今天的人们不仅习惯了&#xff0c;而且依靠互联网来开展他们的日常生活&#xff0c;包括购物、玩游戏、看电…

【python VS vba】(7) python与numpy (建设ing)

目录 1 numpy 的基本介绍 2 numpy里的两种新数据类型&#xff1a;ndarray 和 matrix 2.1 numpy特殊的数据类型 2.1.1 python的数据类型 2.1.2 首先 python原生的list 和 tuple 2.1.3 numpy的数据类型 2.2 np.matrix() 或者 np.mat() 2.2.1 首先&#xff0c;两种写法相…

基于PicGo实现Typora图片自动上传GitHub

文章目录 一. 引言二. 原理三. 配置3.1 GitHub 设置3.2 下载配置 PicGo3.3 配置 Typora3.4 使用 一. 引言 Typora是一款非常好的笔记软件&#xff0c;但是有一个比较不好的地方&#xff1a;默认图片是存放在本地缓存中。这就会导致文件夹一旦被误删或电脑系统重装而忘记备份文件…

18、XSS——cookie安全

文章目录 1、cookie重要字段2、子域cookie机制3、路径cookie机制4、HttpOnly Cookie机制5、Secure Cookie机制6、本地cookie与内存cookie7、本地存储方式 一般来说&#xff0c;同域内浏览器中发出的任何一个请求都会带上cookie&#xff0c;无论请求什么资源&#xff0c;请求时&…

西南科技大学C++程序设计实验六( 继承与派生一)

一、实验目的 1. 理解不同继承属性对派生类访问基类成员的区别 2. 掌握单继承程序编写 二、实验任务 1、调试下列程序,并在对程序进行修改后再调试,指出调试中的出错原因(该题中A为基类,B为派生类,B以public方式继承A) 重点:理解不同继承方式数据的访问权限,派生类…

.kann勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复

导言&#xff1a; 在数字化的今天&#xff0c;.kann勒索病毒等数字威胁正日益猖狂&#xff0c;给个人和企业的数据安全带来了巨大威胁。本文将深入介绍.kann勒索病毒的特征&#xff0c;提供有效的数据恢复方法&#xff0c;并分享一些预防措施&#xff0c;助您更好地在数字世界…

Java利用TCP实现简单的双人聊天

一、创建新项目 首先创建一个新的项目&#xff0c;并命名为聊天。然后创建包&#xff0c;创建两个类&#xff0c;客户端&#xff08;SocketClient&#xff09;和服务器端&#xff08;SocketServer&#xff09; 二、实现代码 客户端代码&#xff1a; package 聊天; import ja…

Memory-augmented Deep Autoencoder for Unsupervised Anomaly Detection 论文阅读

Memorizing Normality to Detect Anomaly: Memory-augmented Deep Autoencoder for Unsupervised Anomaly Detection 摘要1.介绍2.相关工作异常检测Memory networks 3. Memory-augmented Autoencoder3.1概述3.2. Encoder and Decoder3.3. Memory Module with Attention-based S…

mysql知识分享(包含安装卸载)(一)

如果博客有错误&#xff0c;请佬指正。 目录 注意&#xff1a;打开cmd时要有管理员身份打开&#xff0c;重要 为何使用数据库&#xff1f; 数据库的相关概念 关系型数据库 关系型数据库设计规则 表&#xff0c;记录&#xff0c;字段 表的关联关系 一对一关联 一对多关系 …

西南科技大学C++程序设计实验七(继承与派生二)

一、实验目的 1. 掌握多继承程序设计 2. 掌握虚基类编程 3. 拓展学习可视化程序设计中的继承与派生应用 二、实验任务 重点:掌握虚基类的定义与实现,拓展其功能。 阅读分析、完善程序。下面程序(1)与程序(2)分别是没有使用虚基类和使用虚基类的代码,其中A是最上层基…

【二分答案法】寻找峰值

题目&#xff1a;162. 寻找峰值 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 题目分析&#xff1a; &#xff08;1&#xff09;据题知&#xff0c;索引-1、索引n&#xff08;n为数组长度&#xff09;处的元素都默认为无穷小&#xff0c;我们可以在一开始特判…