制作蓝牙小车(一)

制作控制蓝牙小车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.hqwc.cn/news/265733.html

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

相关文章

MySQL - 事务隔离级别

MySQL 事务 本文所说的 MySQL 事务都是指在 InnoDB 引擎下&#xff0c;MyISAM 引擎是不支持事务的。 数据库事务指的是一组数据操作&#xff0c;事务内的操作要么就是全部成功&#xff0c;要么就是全部失败 事务具有原子性&#xff08;Atomicity&#xff09;、一致性&#xff0…

Appium python自动化测试系列之移动自动化测试!

1.1 移动自动化测试现状 因为软件行业越来越发达&#xff0c;用户的接受度也在不断提高&#xff0c;所以对软件质量的要求也随之提高&#xff0c;当然这个也要分行业&#xff0c;但这个还是包含了大部分。因为成本、质量的变化现在对自动化测试的重视度越来越高&#xff0c;在…

空中“千里眼” 复亚环保监测无人机助力生态保护

生态环境保护是全球共同关注的重要议题&#xff0c;为了持续改善环境、加强执法效能&#xff0c;复亚智能环保监测无人机在环保领域大显身手。该智能系统为环境执法人员提供了全新的工具&#xff0c;使其能够在无人机的“千里眼”下&#xff0c;及时发现和制止环境违法行为&…

Qt学习笔记

参考文章&#xff1a;&#xff08;部分内容转载自以下文章&#xff09; 【Qt】边学边写之Qt教程&#xff08;零基础&#xff09;-CSDN博客QT入门看这一篇就够了——超详细讲解&#xff08;40000多字详细讲解&#xff0c;涵盖qt大量知识&#xff09;-CSDN博客 1.创建Qt项目 1…

自定义类型详解(1)

文章目录 目录1. 结构体1.1 结构的基础知识1.2 结构的声明1.3 特殊的声明1.4 结构的自引用1.5 结构体变量的定义和初始化1.6 结构体内存对齐1.7 修改默认对齐数1.8 结构体传参 2. 位段2.1 什么是位段2.2 位段的内存分配2.3 位段的跨平台问题2.4 位段的应用 3. 枚举3.1 枚举类型…

ABAP - Function ALV 02 简单开发一个Function ALV

了解Function ALV&#xff1a; https://blog.csdn.net/HeathlX/article/details/134879766?spm1001.2014.3001.5501程序开发步骤&#xff1a;① TCODE:SE38创建程序 ② 编写程序 DATA gt_spfli TYPE TABLE OF spfli.** Layout 变量定义 (固定使用 直接粘贴复制即可) DATA gs…

OpenWRT搭建本地web站点并结合内网穿透实现公网远程访问

文章目录 前言1. 检查uhttpd安装2. 部署web站点3. 安装cpolar内网穿透4. 配置远程访问地址5. 配置固定远程地址 前言 uhttpd 是 OpenWrt/LuCI 开发者从零开始编写的 Web 服务器&#xff0c;目的是成为优秀稳定的、适合嵌入式设备的轻量级任务的 HTTP 服务器&#xff0c;并且和…

MS2502视频8位数模转换器

MS2502是低功率、超高速视频数模转换器。MS2502以从DC至20MHz的采样速率将 数字信号转换成模拟信号。由于高速工作&#xff0c;MS2502适合于数字电视、电脑视频处 理及雷达信号处理等数字视频应用。 MS2502工作于-20℃至85℃。 特点 1&#xff09;8位分辨率 2&#xff09…

选择最适合你的接口测试工具:SoapUI、JMeter、Postman!

在软件开发的过程中&#xff0c;接口测试是确保系统正常运行的关键环节。为了有效地执行接口测试&#xff0c;选择适当的工具至关重要。在这篇文章中&#xff0c;我们将比较分析三种常见的接口测试工具&#xff1a;SoapUI、JMeter和Postman&#xff0c;以帮助你了解它们的优势和…

边缘智能网关如何应对环境污染难题

随着我国工业化、城镇化的深入推进&#xff0c;包括大气污染在内的环境污染防治压力继续加大。为应对环境污染防治难题&#xff0c;佰马综合边缘计算、物联网、智能感知等技术&#xff0c;基于边缘智能网关打造环境污染实时监测、预警及智能干预方案&#xff0c;可应用于大气保…

【设计模式--结构型--组合模式】

设计模式--结构型--组合模式 组合模式定义结构案例组合模式的分类优点使用场景 组合模式 定义 又称部分整体模式&#xff0c;是用于把一组相似的对象当作一个单一的对象。组合模式依据树型结构来组合对象&#xff0c;用来表示部分以及整体层次&#xff0c;这种类型的设计模式…

用户登录权限

文章目录 [TOC](文章目录) 前言一、 Cookie与session1.HTTP无状态2.cookie 和 session 的生命周期2.1 cookie 生命周期影响因素2.2 session 生命周期影响因素 3.cookie 和 session 的区别4.工作原理3 用户登录Node.js和Express验证session 二、JSON Web Token1. JWT 介绍2. JWT…