Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a bluetooth server socket in android?

I am trying to create a server program that just starts bluetooth, creates a server socket, waits for some device to connect and accepts the connection.

The onClick() method starts the bluetooth, then I call the AcceptThread() method to create a server socket and start listening. Then run() is called which accepts a connection.

But it is not working. My app just stops. Any idea why?

The code is given below:

public class MainActivity extends Activity {

    public BluetoothAdapter mBluetoothAdapter;
    private BluetoothServerSocket mmServerSocket;
    private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=(TextView)findViewById(R.id.textView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button1:

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                text.setText("Does not support bluetooth");
                return;
            }

            Intent discoverableIntent = new
            Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);
            text.setText("Discoverable!!");

            AcceptThread();
            run();

        }
    }

    public void changeT(String str)
    {
        text.setText(str);
    }

    public void AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("MYYAPP", MY_UUID_SECURE);

        } catch (IOException e) { }
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        while (true) {
            try {
                socket = mmServerSocket.accept();
                changeT("listening");
            } catch (IOException e) {
                break;
            }
            if (socket != null) {
                changeT("doneeeee");
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }   
}

The layout as requested:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="31dp"
        android:text="@string/but" 
        android:onClick="onClick"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginRight="43dp"
        android:layout_marginTop="15dp"
        android:text="@string/Output" />

</RelativeLayout>
like image 426
Shubham Saini Avatar asked May 21 '13 17:05

Shubham Saini


People also ask

What is Bluetooth socket in Android?

The most common type of Bluetooth socket is RFCOMM, which is the type supported by the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth. It is also known as the Serial Port Profile (SPP). To create a BluetoothSocket for connecting to a known device, use BluetoothDevice.

What is the purpose of Bluetooth server socket?

The interface for Bluetooth Sockets is similar to that of TCP sockets: Socket and ServerSocket . On the server side, use a BluetoothServerSocket to create a listening server socket. When a connection is accepted by the BluetoothServerSocket , it will return a new BluetoothSocket to manage the connection.

Does Android support Bluetooth SPP?

Android allows communication with Bluetooth devices which have SPP.


1 Answers

The problem was the AcceptThread() and run() functions were running before the adapter could be turned on. A single line before the AcceptThread() solved this.

while(mmServerSocket==null);

Also, run() has to be run in a different thread.

like image 64
Shubham Saini Avatar answered Nov 08 '22 12:11

Shubham Saini