Android mobile app unable to communicate with Jetson Orin nano

Hello,
I’m trying to create an Android app (built with React native) operating on Bluetooth classic, which will communicate with a Jetson orin nano. I’m running these following scripts to send and receive data.

# bluetooth_server_send.py
import bluetooth
import random
import sys
import asyncio
import json

server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_socket.bind(("", bluetooth.PORT_ANY))
server_socket.listen(1)
print("Waiting for connection...")

client_socket, address = server_socket.accept()
print("Accepted connection from ", address)


async def send_data():
    print("send_data() started")
    while True:
        # send data at set intervals
        test_string = str(random.randint(1000, 9000))
        message = {"data": test_string}
        print(f"Sending data: {message}")
        print("Sending data type: ", type(message))
        try:
            # client_socket.send(message.encode('utf-8'))
            # client_socket.send(bytes(json.dumps(test_string), 'utf-8'))
            client_socket.send(json.dumps(message))
        except Exception as ex:
            print("Failed to send data. error: ", ex)
            pass

        await asyncio.sleep(1)


async def main():
    print("main() started")
    t1 = asyncio.create_task(send_data())
    await t1
    print("main() finished")


if __name__ == "__main__":
    print('Starting main.py')

    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("Shutting down the app ...")
        sys.exit()
    finally:
        print('Finished main.py')
        client_socket.close()
        server_socket.close()
# bluetooth_server_receive.py

import bluetooth
import sys
import asyncio

server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_socket.bind(("", bluetooth.PORT_ANY))
server_socket.listen(1)
print("Waiting for connection...")

client_socket, address = server_socket.accept()
print("Accepted connection from ", address)


async def receive_data():
    print("receive_data() started")
    while True:
        # print received data
        try:
            received_data = client_socket.recv(1024)
            # if no data is received, wait for 0.1 seconds
            if not received_data:
                print("No data received")
                await asyncio.sleep(0.1)
                continue

            # process the received data
            print("received_data type: ", type(received_data))
            print(f"Received: {received_data.decode('utf-8')}")
        except Exception as ex:
            print("Failed to receive data. error: ", ex)
            pass

        # wait for 0.1 seconds
        await asyncio.sleep(0.1)


async def main():
    print("main() started")
    t2 = asyncio.create_task(receive_data())
    await t2
    print("main() finished")


if __name__ == "__main__":
    print('Starting main.py')

    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("Shutting down the app ...")
        sys.exit()
    finally:
        print('Finished main.py')
        client_socket.close()
        server_socket.close()

Interestingly, the *receive.py is able to receive data (in bytes format) from the mobile app but the mobile app doesn’t receive anything that *send.py script has sent.

Any idea what I could be doing wrong? Your help is greatly appreciated.

No experience on this use case, you could check those BT topics - Latest bluetooth topics - NVIDIA Developer Forums to see if can find some reference.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.