制作蓝牙小车

制作控制蓝牙小车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是一个介于关系数据库和非关系数据库之间的产…

elasticsearch 是如何实现 master 选举的?

Elasticsearch 中的 master 选举机制主要是通过选举算法实现的。Elasticsearch 使用了 Raft 算法作为其选举算法&#xff0c;用于在集群中选举出一个 master 节点&#xff0c;以及确定新的 master 节点。 在 Elasticsearch 中&#xff0c;所有节点在启动时都会自动加入一个名为…

【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…

C语言——二级指针

指针变量也是变量&#xff0c;是变量就有地址&#xff0c;那么指针变量的地址存放在哪里&#xff1f;——这就是二期指针 int a 10;int *pa &a;int **ppa &pa;//a的地址存放在pa中&#xff0c;pa的地址存放在ppa中。 //pa是一级指针&#xff0c;ppa是二级指针。 对…

【Taro】React+Taro项目实现页面样式兼容手机端和Pad端

前言&#xff1a; taro本身就是兼容多端的ui框架&#xff0c;是现在开发中比较常用的框架&#xff0c;兼容小程序、h5等&#xff0c;所以下面介绍下一些特殊情况的时候怎么实现样式兼容&#xff0c;在不同分辨率下 1.手机端兼容 在config/index.js配置文件中添加如下配置 desi…

数据库函数大全(更新中)

IF(expr1,expr2,expr3) SELECT IF(1<2,yes ,no);#如果 1<2 满足返回 ‘yes’ 否则 ‘no’,也可以用于修改 UPDATE salary SET sex IF(Sex m, f, m); # sex m 改为 ‘f’ , sex f 改为 ‘m’ with with xx_v1 as( sql查询语句1 ),xx_v2 as( sql查询语句2 ),xx_v…

点击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…

libevent库中的http相关函数举例

evhttp_set_cb 是一个用于设置回调函数的函数&#xff0c;该回调函数在 Libevent 的 HTTP 服务器框架 (libevent-http) 中用于处理 HTTP 请求。这个函数需要三个参数&#xff1a;一个 evhttp 实例&#xff0c;一个回调函数&#xff0c;以及一个用于传递到回调函数的 "ctx&…

SpringBoot集成Elasticsearch8.x(9)|(RestClient实现Elasticsearch的简单操作)

SpringBoot集成Elasticsearch8.x(9)|(RestClient curl实现Elasticsearch的操作) 文章目录 SpringBoot集成Elasticsearch8.x(9)|(RestClient curl实现Elasticsearch的操作)@[TOC]前言一、DSL 介绍二、初始化客户端三、封装RestClient dsl执行三、更新1.实用script更新es…

Google Guava简析

Google Guava 是Google开源的一个Java类库&#xff0c;对基本类库做了扩充。感觉最大的价值点在于其 集合类、Cache和String工具类。 github项目地址&#xff1a;GitHub - google/guava: Google core libraries for Java github文档地址&#xff1a;Home google/guava Wiki …

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;默认图片是存放在本地缓存中。这就会导致文件夹一旦被误删或电脑系统重装而忘记备份文件…

pymysql报错: unable to rollback、Already closed

参考&#xff1a; https://www.coder.work/article/4516746 https://www.cnblogs.com/leijiangtao/p/11882107.html https://stackoverflow.com/questions/55116007/pymysql-query-unable-to-rollback unable to rollback处理&#xff1a; # 关闭光标对象 cursor.close() # 关…