arduino蓝牙控制程序_创建自定义的android应用程序以使用蓝牙控制arduino板

本文档介绍如何创建一个自定义的Android应用程序,通过蓝牙连接来控制Arduino板。教程涵盖了从设置蓝牙连接到编写控制代码的整个过程。
摘要由CSDN通过智能技术生成

arduino蓝牙控制程序

This tutorial is about creating your custom Android app to connect with an Arduino board using Bluetooth. Consequently, some basic prior knowledge of Android programming is required to follow this tutorial.

本教程是关于创建自定义Android应用程序以使用蓝牙与Arduino开发板连接的。 因此,遵循本教程需要具备一些Android编程的先验知识。

But don’t worry, if you don’t have basic knowledge on Android programming but still want to create your own Bluetooth app, then you can take the Basic Android Programming for Arduino Makers that is available in Udemy. You will learn how to create a Bluetooth app that can talk with your Arduino board from scratch and without prior knowledge in Android programming required.

但请放心,如果您不具备Android编程的基本知识,但仍想创建自己的蓝牙应用程序,那么可以采用Udemy中提供的Arduino Makers基本Android编程。 您将学习如何创建一个蓝牙应用程序,该应用程序可以从头开始与Arduino板通信,而无需事先具备Android编程知识。

The codes presented in this tutorial is the minimum codes that enable an Android phone and Arduino board to send and receive messages (that can be translated into commands) with each other through Bluetooth.

本教程中介绍的代码是使Android手机和Arduino开发板能够通过蓝牙相互发送和接收消息(可以转换为命令)的最小代码。

开发环境 (Development environment)

To manage some expectations in case the app doesn’t work like it’s supposed to be, this is the environment I use to develop this app:

为了在应用程序无法正常运行时管理一些期望,这是我用来开发该应用程序的环境:

  1. Samsung Galaxy S8, with Android version 9.

    三星Galaxy S8,带有Android版本9。
  2. Android Studio version 3.6.3 with compatible Gradle version.

    Android Studio版本3.6.3与兼容的Gradle版本。
  3. Minimum SDK Version: 19 (You need to select this when creating a new project using Android Studio).

    最低SDK版本:19(使用Android Studio创建新项目时,需要选择此版本)。
  4. Mac OS 10.15.4 (Windows machines also works perfectly)

    Mac OS 10.15.4(Windows机器也可以正常运行)

这个应用程式的运作方式 (How this app works)

This app will create a Bluetooth connection with a nearby Arduino board that has been connected with the HC05 Bluetooth module. It is created to be compatible with Arduino board from this tutorial. However, it is easy to modify the codes so that it can be used together with Arduino boards with different configurations.

此应用程序将与附近已与HC05蓝牙模块连接的Arduino板创建蓝牙连接。 通过创建本教程可以使其与Arduino开发板兼容。 但是,修改代码很容易,因此可以与具有不同配置的Arduino板一起使用。

To test Bluetooth connection functionality, you can press the button on this App to control a built-in LED on the Arduino board. Once a predefined command message is received from Android, Arduino will transmit a return message to Android as a status message.

要测试蓝牙连接功能,您可以按此应用上的按钮以控制Arduino板上的内置LED。 从Android接收到预定义的命令消息后,Arduino将向状态消息发送一条返回消息到Android。

在Android上创建蓝牙连接 (Creating Bluetooth Connection on Android)

Before we dive into the coding part, I would like to describe the step by step flow to create a Bluetooth connection on Android. This is a summary of the more detailed documentation from Google.

在深入研究编码部分之前,我将逐步介绍在Android上创建蓝牙连接的流程。 这是Google更详细的文档的摘要。

  1. Initialize the default Bluetooth adapter (device) on your Android phone.

    初始化Android手机上的默认蓝牙适配器(设备)。
  2. Get the MAC Address from the remote device that you are connecting to. In this case, the MAC Address of HC05 Bluetooth module connected to Arduino board.

    从您要连接的远程设备获取MAC地址。 在这种情况下,HC05蓝牙模块的MAC地址连接到Arduino板。
  3. Create a separate thread in your code to initiate a connection using the MAC Address that we previously obtained. This thread will manage what happens if a connection is successfully established or failed to be established. It also handles if we want to close the Bluetooth connection.

    在代码中创建一个单独的线程,以使用我们先前获得的MAC地址启动连接。 如果成功建立连接或建立连接失败,该线程将管理发生的情况。 它还可以处理是否要关闭蓝牙连接。
  4. Once a connection is successfully established, the thread will do callback for the codes that manage data exchange (transmit and receive between 2 devices). For this, we need to create another thread.

    成功建立连接后,线程将对管理数据交换的代码进行回调(在2个设备之间进行发送和接收)。 为此,我们需要创建另一个线程。
  5. This thread will read incoming data transmission and parse it if necessary (or you can parse it elsewhere on the code) and transmit the message or command that is generated by the Android app.

    该线程将读取传入的数据传输并在必要时对其进行解析(或者您可以在代码的其他位置进行解析),并传输由Android应用生成的消息或命令。

Now, the flow above needs to be translated into codes.

现在,以上流程需要转换为代码。

创建活动和Java类 (Creating Activities and Java Class)

Create a new project with the empty activity template and select the appropriate name for your app. For this app we will create 2 activities and 2 Java classes :

使用空的活动模板创建一个新项目,然后为您的应用选择适当的名称。 对于这个应用程序,我们将创建2个活动和2个Java类:

  1. MainActivity. This is automatically created when you create a new project. This is where most of the interactions take place.

    主要活动。 当您创建新项目时,将自动创建该文件。 这是大多数交互发生的地方。
  2. SelectDeviceActivity. The UI where you select the Bluetooth device that you want to connect.

    选择设备活动。 在其中选择要连接的蓝牙设备的UI。
  3. DeviceListAdapter. A class to display a list of paired Bluetooth devices for you to connect. The list will be displayed in SelectDeviceActivity.

    DeviceListAdapter。 显示配对的蓝牙设备列表以供您连接的类。 该列表将显示在SelectDeviceActivity中。
  4. DeviceInfoModel. A class that acts as a placeholder for the remote device information.

    DeviceInfoModel。 充当远程设备信息的占位符的类。

AndroidManifest.xml (AndroidManifest.xml)

Once you created all the activities and classes above, your AndroidManifest.xml file will look something like this :

一旦创建了以上所有活动和类,您的AndroidManifest.xml文件将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.droiduino.bluetoothconn">


    <!--Permissions for Bluetooth access-->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SelectDeviceActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </applicati
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值