简体   繁体   English

如何从 android 应用程序发送值到 Arduino 蓝牙 HC-05

[英]How to Send Value from android app to Arduino Bluetooth HC-05

I am making an android app using android studio ,我正在使用 android studio 制作一个 android 应用程序,

I want to send value 'string or Integer' to arduino pro mini via bluetooth hc-05.我想通过蓝牙 hc-05 将值“字符串或整数”发送到 arduino pro mini。

The bluetooth hc-05 will already be connected (paired) to the android device .蓝牙 hc-05 已经连接(配对)到 android 设备。

Can you please help me to make a simple code that send the value via bluetooth?你能帮我做一个简单的代码,通过蓝牙发送值吗? Thank you so much.非常感谢。

Well, the easiest way to implement that is to use a third-party library like this BluetoothLibrary .好吧,实现它的最简单方法是使用像这样的第三方库BluetoothLibrary

  • Include the library dependency into your app module gradle file.将库依赖项包含到您的app module gradle 文件中。
  • set up the library in your app as described in the library's repository on GitHub.按照 GitHub 上库的存储库中的说明在您的应用程序中设置库。
  • If you need to connect to the device from the app you have to implement the scanning functionality yourself by providing a ListView and the logic to insert the discovered Bluetooth devices.如果您需要从应用程序连接到设备,您必须通过提供ListView和插入发现的蓝牙设备的逻辑来自己实现扫描功能。
  • If you prefer to follow the easy way just connect to the Bluetooth device externally from your device's Bluetooth manager, then you can communicate with the device as follows.如果您更喜欢从设备的蓝牙管理器外部连接到蓝牙设备的简单方法,那么您可以按如下方式与设备进行通信。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main)

    bluetooth = new Bluetooth(this);
    bluetooth.send("Your message");

}

  • If you want to receive back some response from the other Bluetooth device you have to listen to the DeviceCallback() you will find its description in the Github repository如果你想从其他蓝牙设备接收一些响应,你必须听DeviceCallback()你会在 Github 存储库中找到它的描述

Note笔记

You have to add some permissions in the Manifest files which are您必须在清单文件中添加一些权限

  • BLUETOOTH蓝牙
  • BLUETOOTH_ADMIN BLUETOOTH_管理员
  • ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION

In order to give the access to your app to use the bluetooth为了允许访问您的应用程序以使用蓝牙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用蓝牙模块HC-05从Arduino-uno发送数据并在Android中读取? - How to send data from Arduino-uno using Bluetooth module HC-05 and read it in Android? Arduino蓝牙模块HC-05与Androidstudio app之间的连接 - Connection between Arduino bluetooth module HC-05 and Androidstudio app Arduino HC-05写入串行监视器,但不写入蓝牙(Android) - Arduino HC-05 writes to serial monitor but not Bluetooth (Android) 通过 HC-05 蓝牙从 Arduino 传感器接收字符串数据到 Android Studio Textview - Receive string data from Arduino sensor to Android Studio Textview via HC-05 Bluetooth 带有HC-05的蓝牙串行作为Android服务 - Bluetooth Serial as Android Service with HC-05 Arduino HC-05 蓝牙模块发送数据时断开连接 - Arduino HC-05 Bluetooth module disconnects when sending data Android InputStream无法从Bluetooth HC-05接收整个消息 - Android InputStream cannot receive the whole message from Bluetooth HC-05 从蓝牙输入流中提取数据的问题,使用安卓平板电脑和 HC-05 无线电发射器 - Problems with extracting data from Bluetooth input stream, using android tablet and HC-05 radio transmitter 如何开发与蓝牙“ HC-05”连接的Java桌面应用程序? - How to develop Java desktop application to connect with bluetooth “HC-05”? 最佳实践 - Java 串行蓝牙连接 HC-05 - Best practice - Java serial bluetooth connection HC-05
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM