How to properly connect the bluetooth server in C for Android?

Thread Starter

Hunter25

Joined Aug 23, 2018
2
I'm trying to connect an application between the computer and an android app. The app will be the client and the computer will be the server.

Using BlueZ (C library for bluetooth on linux) for server:



C:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/rfcomm.h>
 
    int main(int argc, char **argv)
    {
        struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
        char buf[1024] = { 0 };
        int s, client, bytes_read;
        socklen_t opt = sizeof(rem_addr);
 
        // allocate socket
        s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
 
        // bind socket to port 1 of the first available
        // local bluetooth adapter
        loc_addr.rc_family = AF_BLUETOOTH;
        loc_addr.rc_bdaddr = *BDADDR_ANY;
        loc_addr.rc_channel = (uint8_t) 1;
        bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
 
        // put socket into listening mode
        listen(s, 1);
 
        // accept one connection
        client = accept(s, (struct sockaddr *)&rem_addr, &opt);
 
        ba2str( &rem_addr.rc_bdaddr, buf );
        fprintf(stderr, "accepted connection from %s\n", buf);
        memset(buf, 0, sizeof(buf));
 
        // read data from the client
        bytes_read = read(client, buf, sizeof(buf));
        if( bytes_read > 0 ) {
            printf("received [%s]\n", buf);
        }
 
        // close connection
        close(client);
        close(s);
        return 0;
    }
The source of this example is : http://people.csail.mit.edu/albert/bluez-intro/x502.html

And the class of the app that i am using to connect is:




    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothServerSocket;
    import android.bluetooth.BluetoothSocket;
    import android.os.Bundle;
    import android.os.Message;
 
    import java.io.IOException;
    import java.io.InputStream;
    import java.io_OutputStream;
    import java.util.Arrays;
    import java.util.UUID;
 
    public class ConnectionThread extends Thread {
 
 
        BluetoothSocket btSocket = null;
        BluetoothServerSocket btServerSocket = null;
        InputStream input = null;
        OutputStream output = null;
        String btDevAddress = null;
        String myUUID = "00000101-0000-1000-8000-00805F9C34BF";
        boolean server;
        boolean running = false;
        public ConnectionThread() {
 
            this.server = true;
        }
        public ConnectionThread(String btDevAddress) {
 
            this.server = false;
            this.btDevAddress = btDevAddress;
        }
        public void run() {

            this.running = true;
            BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

            if(this.server) {
                try {
                    btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord("Super Bluetooth", UUID.fromString(myUUID));
                    btSocket = btServerSocket.accept();
                    if(btSocket != null) {
 
                        btServerSocket.close();
                    }
 
                } catch (IOException e) {
                    e.printStackTrace();
                    toMainActivity("---N".getBytes());
                }
 
 
            } else {

                try {

                    BluetoothDevice btDevice = btAdapter.getRemoteDevice(btDevAddress);
                    btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString(myUUID));

                    btAdapter.cancelDiscovery();

                    if (btSocket != null)
                        btSocket.connect();
 
                } catch (IOException e) {
                    e.printStackTrace();
                    toMainActivity("---N".getBytes());
                }
 
            }
 
            if(btSocket != null) {

                toMainActivity("---S".getBytes());
 
                try {
                    input = btSocket.getInputStream();
                    output = btSocket.getOutputStream();
 
                    byte[] buffer = new byte[1024];
                    int bytes;

                    while(running) {
 
                        bytes = input.read(buffer);
                        toMainActivity(Arrays.copyOfRange(buffer, 0, bytes));
 
                    }
 
                } catch (IOException e) {
                    e.printStackTrace();
                    toMainActivity("---N".getBytes());
                }
            }
 
        }
        private void toMainActivity(byte[] data) {
 
            Message message = new Message();
            Bundle bundle = new Bundle();
            bundle.putByteArray("data", data);
            message.setData(bundle);
            MainActivity.handler.sendMessage(message);
        }
        public void write(byte[] data) {
 
            if(output != null) {
                try {
 
                    output.write(data);
 
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                toMainActivity("---N".getBytes());
            }
        }
 
   
        public void cancel() {
 
            try {
 
                running = false;
                btServerSocket.close();
                btSocket.close();
 
            } catch (IOException e) {
                e.printStackTrace();
            }
            running = false;
        }
    }

To send a data to the server I do:

    ConnectionThread connect = new ConnectionThread(data.getStringExtra("Address_server"));
    connect.start();
    byte[] data = "20".getBytes();
    connect.write(data);

The problem is not that the connection is occurring. I believe it's because the server is out of UUID.

How can I insert the UUID into the server so that it can receive the data sent?

Moderators note : used code tags
 
Last edited by a moderator:
Top