制作蓝牙小车(一)

制作控制蓝牙小车app

想制作一个蓝牙小车,通过手机app程序操控小车运行,制作分2个部分(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/221710.shtml

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

相关文章

FFmpegd的AVBSF

本章主要介绍AVBSF 文章目录 结构体定义对外函数常见的过滤器 从名字我们可以知道这是个码流过滤器&#xff0c;我们最常用的是一个叫做h264_mp4toannexb_bsf的东东 这个过滤器的作用是把h264以MP4格式的NALU转换为annexb&#xff08;0x000001&#xff09; const AVBitStreamF…

centos安装了curl却报 -bash: curl: command not found

前因 我服务器上想用curl下载docker-compress&#xff0c;发现没有curl命令&#xff0c;就去下载安装&#xff0c;安装完成之后&#xff0c;报-bash: curl: command not found 解决方法 [rootcentos ~]# rpm -e --nodeps curl warning: file /usr/bin/curl: remove failed: …

python每日学10:关于python实用版本的选择

用python也有好几年了&#xff0c;也会经常安装python&#xff0c;因为有工作需要&#xff0c;可能在各个地方使用python&#xff0c;自己的电脑也经常重装&#xff0c;重装后会装python&#xff0c;还有的时候&#xff0c;装的包太多了&#xff0c;影响整个环境的使用&#xf…

每日一道算法题 6(2023-12-14)

题目描述&#xff1a; 有一种简易压缩算法&#xff1a;针对全部为小写英文字母组成的字符串&#xff0c;将其中连续超过两个相同字母的部分压缩为连续个数加该字母&#xff0c;其他部分保持原样不变。 例如字符串aaabbccccd 经过压缩变成字符串 3abb4cd 请您编写解压函数,根据…

react 自动设置高度

export default function Height() {const [showDom,setShowDom]useState([])const heightRef useRef(null)let arrData [{"index": 1,//占的U位"model_id": "assert",//model_id"ci_id": 1,//设备id "ci_name": "设备…

数字IC验证快速入门全攻略,你想知道的都在这!

芯片行业是个高风险、高投入的行业&#xff0c;做一款芯片仅仅是开模的费用就是百万起。 从设计到制造都是环环相扣的&#xff0c;设计过程中的BUG或者错误能够达到上千个。 所以验证是保证芯片功能正确性和完整性最重要的一环。&#xff08;文末有学习视频哦~&#xff09; …

21、状态模式(State Pattern)

状态模式指给对象定义不同的状态&#xff0c;并为不同的状态定义不同的行为&#xff0c;在对象的状态发生变换时自动切换状态的行为。 状态模式是一种对象行为型模式&#xff0c;它将对象的不同行为封装到不同的状态中&#xff0c;遵循了“单一职责”原则。同时&#xff0c;状…

MATLAB 2018一本通 学习笔记一

vivado暂时可以收一下&#xff0c;而且今天看场景和问题的解决程度&#xff0c;这两天看的还是有效果&#xff0c;需要接下来弄一下matlab。 算法开发、数据可视化、数据分析、数值计算方面&#xff0c;之前搞Python弄过matlib库&#xff0c;觉得差不多&#xff0c;但是实际工…

【动手学深度学习】(十三)深度学习硬件

文章目录 一、CPU和GPU二、更多的芯片1.DSP:数字信号处理2.可编程阵列(FPGA)3.AI ASIC 三、单机多卡并行 一、CPU和GPU 提升CPU利用率 在计算ab之前&#xff0c;需要准备数据 主内存->L3->L2->L1->寄存器(数据只有进入寄存器才可以参与运算) 提升空间和时间的内存…

【react.js + hooks】useVirtualArea 渲染虚拟列表

useVirtualArea Hook useVirtualArea 是一个 React Hook&#xff0c;用于创建虚拟列表。虚拟列表是一种优化技术&#xff0c;用于在不影响性能的情况下显示大量数据。 参数 useVirtualArea 接受一个对象和一个数组作为参数&#xff0c;该对象包含以下属性&#xff1a; load…

Android : Room 数据库的基本用法 —简单应用_三_版本

在实体类中添加了新字段&#xff1a; Entity(tableName "people") public class People {//新添加的字段private String email;public String getEmail() {return email;}public void setEmail(String email) {this.email email;}} 再次编译启动时会报错&#xf…

电子元器件介绍——电阻(一)

电子元器件 文章目录 电子元器件前言1.1电阻基本知识1.2电阻的作用1.3电阻的分类1.4 贴片电阻贴片电阻的规范、尺寸、封装 1.5 技术参数噪声&#xff1a; 1.6 电阻的失效 总结 前言 接下来我们就把常用的电子元器件全部介绍给大家&#xff0c;这一节是电阻&#xff0c;电容电感…

基础算法(2):排序(2):计数排序

1.计数排序实现 计数排序是一个非基于比较的稳定的线性时间的排序算法&#xff0c;而选择排序是基于比较的&#xff0c;计数排序不用&#xff0c;它的实现依靠计数。 工作原理&#xff1a;使用一个额外的数组&#xff0c;其中第i个位置是待排序数组1中值等于i的元素的个数&…

蓝桥杯物联网竞赛_STM32L071_9_按键矩阵扩展模块

原理图&#xff1a; 矩阵按键原理图&#xff1a; 实验板接口原理图&#xff1a; 得到对应图&#xff1a; 扫描按键原理&#xff1a; 按键的COLUMN1、2、3分别制0&#xff0c;每次只允许其中一个为0其他都是1&#xff08;POW1和POW2正常状况为上拉&#xff09;&#xff0c;当有…

软件设计中如何画各类图之七了解组件图:系统架构的关键视角

目录 1 前言2 组件图基本介绍3 画组件图的步骤4 组件图的用途5 场景及实际场景举例6 结语 1 前言 组件图是一种UML的图形化表示工具&#xff0c;为系统架构提供了重要视角。它描述了系统中各个组件以及它们之间的依赖关系和连接。用于展示系统中的组件、软件模块、以及它们之间…

平头哥玄铁系列 RISC-V 芯片及开发板

1、玄铁 9 系列概述 玄铁 8 系列 基于C-SKY架构&#xff0c;玄铁 9 系列基于 RISC-V 架构。E 系列为 RISC-V 32 位&#xff0c;C 系列为 RISC-V 64 位。 E902&#xff1a;超低功耗 RSIC-V 架构处理器 E902 采用 2 级极简流水线兼容 RISC-V 架构且对执行效率等方面进行了增强&a…

linux-tar命令、解压、压缩

压缩 文件夹 命令&#xff1a;tar -zcvf ~/test/tar_t.tar.gz /target/ 将/target/文件夹及其子文件夹和文件压缩成tar_t.tar.gz文件&#xff0c;并放于~/test/路径下 文件 命令&#xff1a;tar -zcvf ~/test/tar_t.tar.gz /target/file 将/target/file文件压缩成tar_t.tar…

linux中文件服务器NFS和FTP服务

文件服务器 NFSNFS介绍配置nfs文件共享服务端客户端 FTPftp介绍FTP基础ftp主动模式ftp被动模式 Vsftp 服务器简介vsftpd配置安装vsftpd[ftp的服务端]编辑配置文件匿名用户设置创建本地用户使用ftp服务 客户端操作匿名用户登录本地用户登录lftp服务 NFS NFS介绍 文件系统级别共…

对于初学者来说,从哪些方面开始学习 Java 编程比较好?

对于初学者来说&#xff0c;从哪些方面开始学习 Java 编程比较好&#xff1f; 在开始前我有一些资料&#xff0c;是我根据自己从业十年经验&#xff0c;熬夜搞了几个通宵&#xff0c;精心整理了一份「Java的资料从专业入门到高级教程工具包」&#xff0c;点个关注&#xff0c;全…

玩转大数据14:分布式计算框架的选择与比较

1. 引言 随着大数据时代的到来&#xff0c;越来越多的企业和组织需要处理海量数据。分布式计算框架提供了一种有效的方式来解决大数据处理的问题。分布式计算框架将计算任务分解成多个子任务&#xff0c;并在多个节点上并行执行&#xff0c;从而提高计算效率。 2. 分布式计算…