如何简单地将字符串从android studio(kotlin)传递到arduino串行蓝牙模块(HC-05)? 我正在使用android Studio(Kotlin)为一个学校项目制作一个android应用程序 我需要通过HC-05蓝牙模块向Arduino Genuino模块发送字符串 应用程序启动时,Arduino已经连接(配对)到我的android设备 有人能帮我找到一种正确而简单的方法,只发送这些数据吗 非常感谢
我终于得到了答案,我做到了:如何简单地将字符串从android studio(kotlin)传递到arduino串行蓝牙模块(HC-05)? 我正在使用android Studio(Kotlin)为一个学校项目制作一个android应用程序 我需要通过HC-05蓝牙模块向Arduino Genuino模块发送字符串 应用程序启动时,Arduino已经连接(配对)到我的android设备 有人能帮我找到一种正确而简单的方法,只发送这些数据吗 非常感谢,android,kotlin,bluetooth,arduino,send,Android,Kotlin,Bluetooth,Arduino,Send,我终于得到了答案,我做到了: private fun CheckBt() { Toast.makeText(applicationContext, "It has started", Toast.LENGTH_SHORT).show() mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter() if (!mBluetoothAdapter.enable()) { Toast.makeText(app
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)
}
}
我终于得到了答案,我做到了:
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)
}
}
你可以用像这样的东西,它是Java的,但是可以帮上忙。谢谢,我试试这个。你可以用像这样的东西,它是Java的,但是可以帮上忙。谢谢,我试试这个。