-1

I want to develope android studio application which send data to nodemcu and also I want to receive data to android textview. Or the same functionality using Bluetooth module. Because using Bluetooth module I can control led using android application..but I don't know how to receive it on the android application. Can anyone please help?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • there are 2 ways to communicate with bluetooth, [Bluetooth](https://developer.android.com/guide/topics/connectivity/bluetooth) and [Ble](https://developer.android.com/guide/topics/connectivity/bluetooth-le) – Hababa Mar 11 '20 at 07:35
  • I want to use Bluetooth module – Tejal Salve Mar 11 '20 at 09:12
  • please provide any code snippet if possible or idea related to receiving data from nodemcu to android app – Tejal Salve Mar 11 '20 at 09:27

1 Answers1

1

If you want to connect with classic bluetooth, then you need to do some stuff.

(Suppose you have paired device already, or you can paired directly by device's Settings)

  1. scan device with BluetoothAdapter from BluetoothManager

    val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE)

    val bluetoothAdapter = bluetoothManager.adapter


  1. get the device you want to communicate with form BluetoothAdapter.bondedDevices

    val bondedDevices = bluetoothAdapter.bondedDevices

    val device = bondedDevices[any] (bondedDevices type is Set)


  1. call createRfcommSocketToServiceRecord() to create a Socket connection

    val socket = device.createRfcommSocketToServiceRecord()


  1. after connect the socket, you can send/retrieve with outputstream/inputstream from this socket, and finally disconnect the connection.

    socket.connect()

    ...

    your comminucation here with socket.inputstream()/socket.outputStream()

    ...

    socket.disconnect()

Hababa
  • 551
  • 5
  • 8