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?
-
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 Answers
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)
scan device with BluetoothAdapter from BluetoothManager
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE)
val bluetoothAdapter = bluetoothManager.adapter
get the device you want to communicate with form BluetoothAdapter.bondedDevices
val bondedDevices = bluetoothAdapter.bondedDevices
val device = bondedDevices[any]
(bondedDevices type is Set)
call createRfcommSocketToServiceRecord() to create a
Socket
connectionval socket = device.createRfcommSocketToServiceRecord()
after
connect
the socket, you can send/retrieve withoutputstream/inputstream
from this socket, and finallydisconnect
the connection.socket.connect()
...
your comminucation here with socket.inputstream()/socket.outputStream()
...
socket.disconnect()

- 551
- 5
- 8