Question

I'm writing an application to communicate between my smartphone and a computer using a bluetooth device.

I'm using Bluecove to manage the bluetooth on the computer, and the android API for my android device.

However, when I'm debugging, nothing happens. I think that the problem is that the UUID is wrong. I'm not sure how to get the devices to identify each other, in order to establish a connection.

I have read some other "questions" about those tags, but what I've tried didn't fix my problem:

Here's what I've written so far:

  1. For tho android (Server) (This is the function that will make the connection)

    public void connectSocket(){ blueAdapter.cancelDiscovery(); // Cancel discovery because it'll slow down the connection

    final BluetoothServerSocket serverSocket;
    BluetoothServerSocket sSocket= null;
    try{
        sSocket = blueAdapter.listenUsingRfcommWithServiceRecord("BluetoothJANE", MY_UUID);
    }catch(IOException e){}
    serverSocket = sSocket;
    
    Thread acceptThread = new Thread(new Runnable() {
    
        @Override
        public void run() {
            BluetoothSocket socket = null;
            while(true){
                try{
                    socket = serverSocket.accept();
                }catch(IOException e){
                    break;
                }
                if(socket != null){
                    try{
                        iStream = socket.getInputStream();
                        oStream = socket.getOutputStream();
                    } catch(IOException e){}
                }
            }
        }
    });
    acceptThread.start();
    

    }

  2. For java app on PC (This is the constructor of the class (it's on an independent thread) that will manage the bluetooth connection)

    public ModuleBluetooth() {

    StreamConnectionNotifier notifier = null;
    StreamConnection connection = null;
    
    try {
        blueDevice = LocalDevice.getLocalDevice();
        blueDevice.setDiscoverable(DiscoveryAgent.GIAC);
    
        String url = "btspp://localhost:" + MY_UUID.toString()
                + ";name=RemoteBluetooth";
        notifier = (StreamConnectionNotifier) Connector.open(url);
    
    } catch (BluetoothStateException e) {
        System.out
                .println("ModuleBluetooth: Error getting the bluetooth device");
    } catch (IOException e) {
    }
    System.out.println("waiting for connection...");
    try {
        connection = notifier.acceptAndOpen();
        System.out.println("Conenction created");
    } catch (IOException e) {
        System.out.println("Can not create the connection");
    }
    

    }

Could somebody please help me? Any thoughts would be very much appreciated.

I have also tried to use some functions to acquire the UUID (in android) such as, [fetchUuidsWithSdp][2] (and the related functions) but eclipse doesn't recognize that functions (It seems that they don't exist in "my" API).

http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#fetchUuidsWithSdp%28%29

Was it helpful?

Solution

Try this example, http://luugiathuy.com/2011/02/android-java-bluetooth/ . I also had problem related to UUID, in this example, Converting UUID to 00001101-0000-1000-8000-00805F9B34FB worked out of the box. See this link: http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top