如何简单地将字符串从 android studio (kotlin) 传递到 arduino 串行蓝牙模块 (HC-05)? [英] How to simply pass a string from android studio (kotlin) to arduino serial bluetooth module (HC-05)?
本文介绍了如何简单地将字符串从 android studio (kotlin) 传递到 arduino 串行蓝牙模块 (HC-05)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我正在使用 Android Studio (Kotlin) 为学校项目制作 Android 应用.
- 我需要将字符串发送到一个 Arduino Genuino Uno 模块,并通过一个 HC-05 蓝牙模块.
- 当应用程序启动时,Arduino 已经连接(配对)到我的安卓设备.
- 谁能帮我找到一种正确且简单的方法来只发送这些数据?
- 非常感谢.
- I'm making an android app for a school project, using Android Studio (Kotlin).
- I need to send strings to an Arduino Genuino Uno module, passing by a HC-05 bluetooth module.
- The Arduino will already be connected (paired) to my android device when the app will be launched.
- Can someone help me to find a right and easy way to only SEND these datas ?
- Thanks a lot.
推荐答案
我终于得到了答案,我做到了:
I finally got the answer, I did that :
private fun CheckBt() {
Toast.makeText(applicationContext, "It has started", Toast.LENGTH_SHORT).show()
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (!mBluetoothAdapter.enable()) {
Toast.makeText(applicationContext, "Bluetooth Disabled !", Toast.LENGTH_SHORT).show()
/* It tests if the bluetooth is enabled or not, if not the app will show a message. */
finish()
}
if (mBluetoothAdapter == null) {
Toast.makeText(applicationContext, "Bluetooth null !", Toast.LENGTH_SHORT).show()
}
}
fun Connect() {
val device = mBluetoothAdapter.getRemoteDevice("98:D3:32:71:17:DE")
Log.d("", "Connecting to ... $device")
Toast.makeText(applicationContext, "Connecting to ... ${device.name} mac: ${device.uuids[0]} address: ${device.address}", Toast.LENGTH_LONG).show()
mBluetoothAdapter.cancelDiscovery()
try {
btSocket = device.createRfcommSocketToServiceRecord(myUUID)
/* Here is the part the connection is made, by asking the device to create a RfcommSocket (Unsecure socket I guess), It map a port for us or something like that */
btSocket.connect()
Log.d("", "Connection made.")
Toast.makeText(applicationContext, "Connection made.", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
try {
btSocket.close()
} catch (e2: IOException) {
Log.d("", "Unable to end the connection")
Toast.makeText(applicationContext, "Unable to end the connection", Toast.LENGTH_SHORT).show()
}
Log.d("", "Socket creation failed")
Toast.makeText(applicationContext, "Socket creation failed", Toast.LENGTH_SHORT).show()
}
//beginListenForData()
/* this is a method used to read what the Arduino says for example when you write Serial.print("Hello world.") in your Arduino code */
}
private fun writeData(data: String) {
var outStream = btSocket.outputStream
try {
outStream = btSocket.outputStream
} catch (e: IOException) {
//Log.d(FragmentActivity.TAG, "Bug BEFORE Sending stuff", e)
}
val msgBuffer = data.toByteArray()
try {
outStream.write(msgBuffer)
} catch (e: IOException) {
//Log.d(FragmentActivity.TAG, "Bug while sending stuff", e)
}
}
这篇关于如何简单地将字符串从 android studio (kotlin) 传递到 arduino 串行蓝牙模块 (HC-05)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文