Search code examples
pythonreact-nativebluetoothraspberry-piraspberry-pi3

How do you connect a react native app and raspberry pi via bluetooth?


I have a device I'm making and its powered by the Raspberry Pi 3 B, and to access/control this device will be an app I'm developing using React Native.

  1. The phone and RPi both have bluetooth turned on
  2. The phone connects to the RPi's bluetooth signal
  3. The user then starts up the app, and is able to send a command through to the app via bluetooth
  4. RPi is able to read the data, and carry out the command.

That is the jist of it. From my understanding I will need to create a python program of some sort to deal with the incoming data. My problem is how do I even access that data? How does my python program understand that a connection has been made, and that it is going to receive information(in this case a .txt file)

I've seen some documentation about PyBluez, but i'm not entirely sure if thats how I want to go about doing it. Maybe that is the solution and I'm not understanding it correctly.

Sorry if this was too long, any advice will be greatly appreciated.


Solution

  • I am going to assume you are using a Serial Port Profile (SPP) which is sometimes called RFCOMM. [The other option is that you are using Bluetooth Low Energy (BLE)].

    Using SPP means you need to create a server on the RPi for the app to connect to.

    Examples of how to do this are at:

    http://blog.kevindoran.co/bluetooth-programming-with-python-3/

    https://bluedot.readthedocs.io/en/latest/btcommapi.html#bluetoothserver

    These two examples take slightly different approaches to achieve the same thing. Pick whichever one feels more natural to you.

    To test you have the server running correctly on the RPi, you can use a generic app on the phone. For example, the Serial Bluetooth Terminal https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_GB