Как просто передать строку из студии Android (kotlin) в последовательный модуль bluetooth arduino (HC-05)?

  • Я делаю приложение для Android для школьного проекта, используя Android Studio (Котлин).
  • Мне нужно отправить струны на модуль Arduino Genuino Uno, минуя модуль bluetooth HC-05.
  • Arduino уже будет подключен (сопряжен) с моим устройством Android, когда приложение будет запущено.
  • Может ли кто-нибудь помочь мне найти правильный и простой способ только ОТПРАВИТЬ эти данные?
  • Большое спасибо.

Вы можете использовать что-то вроде этого stackoverflow.com/questions/22899475/… на Java, но это может помочь.

Fernandoms 08.02.2019 21:23

Спасибо, я попробую это.

Wise Man 09.02.2019 13:47
0
2
2 603
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Наконец-то я получил ответ, я сделал это:

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)
    }

}

Другие вопросы по теме