How do I connect esp32 bluetooth with Windows 10 using Java?

  arduino, bluetooth, esp32, java, windows

I’m trying to send data from an esp32 (Arduino) to my PC program written in Java. The library I’m using is Bluecove 2.1. My code finds the esp32 Url but when I try to open a InputStream it waits a little bit (like 5 seconds) and then this error appears: javax.bluetooth.BluetoothConnectionException: Connection timeout; [10060] This is a Windows exception I suppose since it’s a native method that throws it. In order to understand my problem a little more I tried to connect my esp32 to Windows using Bluetooth Device app on the Microsoft Store: same problem: "Connection timeout". What do I do?

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class Bluetooth extends Thread{
    
    private static final String name = "RealFP2";
    public volatile boolean scanFinished = false;//Indica se é finita la scansione
    RemoteDevice realFP = null;//Oggetto che rappresenta il dispositivo RelaFP
    String esp32URL;//Indirizzo del dispositivo

    Vector<Device> devices = new Vector<>();

    //Scannerizza e inserisce in Vector<Device> devices tutti i device che trova
    public void scan() throws Exception{
        LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
            @Override
            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                try {
                    String name = btDevice.getFriendlyName(false);
                    RemoteDevice dev = btDevice;
                    Device d = new Device(name, dev);
                    devices.add(d);
                    System.out.println(name + " added");

                } catch (IOException ex) {
                    Logger.getLogger(Bluetooth.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            @Override
            public void servicesDiscovered(int i, ServiceRecord[] srs) {
            }

            @Override
            public void serviceSearchCompleted(int i, int i1) {
            }

            @Override
            public void inquiryCompleted(int i) {
                scanFinished = true;
                connect();
            }

        });
    }

    //Si connette a RealFP2
    public void connect(){
        System.out.println("connecting");
        for (int i = 0; i < devices.size(); i++) {
            if (name.equals(devices.elementAt(i).name)) {
                realFP = devices.elementAt(i).realFPdevice;
                System.out.println(devices.elementAt(i));
            }
        }
        UUID uuid = new UUID(0x1101); //scan for btspp://... services 
        UUID[] searchUuidSet = new UUID[]{uuid};
        int[] attrIDs = new int[]{
            0x0100 // service name
        };
        scanFinished = false;
        try {
            LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet,
                    realFP, new DiscoveryListener() {
                        @Override
                        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                        }

                        @Override
                        public void inquiryCompleted(int discType) {
                        }

                        @Override
                        public void serviceSearchCompleted(int transID, int respCode) {
                            scanFinished = true;
                        }

                        @Override
                        public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                            for (int i = 0; i < servRecord.length; i++) {
                                esp32URL = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                                System.out.println("Url: " + esp32URL);
                                if (esp32URL != null) {
                                    break; //take the first one
                                }
                            }
                        }
                    });
        } catch (BluetoothStateException e) {
            e.printStackTrace();
        }

        while (!scanFinished) {
            try {
                this.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Startato");
        
        this.start();
    }

public void run() {
        System.out.println("running");
        if (esp32 != null) {
            
            StreamConnection streamConnection = null;
            InputStream is = null;
            
            try {
                //il problema é qui
                streamConnection = (StreamConnection) Connector.open(esp32URL);
                is = streamConnection.openInputStream();
            }catch(IOException e) {
                e.printStackTrace();
            }
            while(go) {
                int bytesNumber = 0;
                try {
                    bytesNumber = is.available();//HERE IS THE PROBLEM
                } catch (IOException e1) {
                    e1.printStackTrace();
                }...//It goes on but it doesn't matter

Source: Windows Questions

LEAVE A COMMENT