Android Open Source - BtDemo Bluetooth Server Thread






From Project

Back to project page BtDemo.

License

The source code is released under:

Apache License

If you think the Android project BtDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cn.edu.hust.cm.bt.demo.server;
//w  w  w  . j  ava  2 s  .  c o  m
import java.io.IOException;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import cn.edu.hust.cm.bt.demo.BaseBluetoothSocket;

public class BluetoothServerThread extends Thread {

    private BluetoothServer server;
    private BluetoothServerSocket serverSocket;
    private BluetoothAdapter adapter;

    private volatile boolean running;

    @SuppressLint("NewApi")
    public BluetoothServerThread(BluetoothServer server,
            BluetoothAdapter adapter) throws IOException {
        super("BluetoothServerThread");

        this.server = server;
        this.adapter = adapter;

        String name = server.getName();
        UUID uuid = server.getUuid();
        try {
            serverSocket = adapter.listenUsingRfcommWithServiceRecord(name,
                    uuid);
        } catch (IOException e) {
            throw new IOException(
                    "can not create a listening RFCOMM BluetoothServerSocket with name="
                            + name + ", uuid=" + uuid, e);
        } catch (Exception e) {
            throw new IOException("NOT support BT??", e);
        }
        running = true;
    }

    @Override
    public void run() {
        while (running) {
            try {
                BluetoothSocket clientSocket = serverSocket.accept();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // TODO handle clientSocket
        }
    }

    // TODO a new thread to handle client socket
    private void handleClientSocket(BluetoothSocket clientSocket) {
        BaseBluetoothSocket socket = new BaseBluetoothSocket(clientSocket);
        server.onConnected(socket);
    }

    @SuppressLint("NewApi")
    private void closeServerSocketQuietly() throws IOException {
        if (null != serverSocket) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                throw new IOException("can not close BluetoothServerSocket", e);
            } finally {
                serverSocket = null;
            }
        }
    }

    /**
     * Stop this thread and close BluetoothServerSocket
     * @throws IOException
     */
    public void shutdown() throws IOException {
        running = false;
        closeServerSocketQuietly();
    }

    /**
     * Returns true if BluetoothServerSocket is still listening
     * @return
     */
    public boolean isRunning() {
        return running;
    }
}




Java Source Code List

cn.edu.hust.cm.bt.demo.BaseBinderActivity.java
cn.edu.hust.cm.bt.demo.BaseBinderService.java
cn.edu.hust.cm.bt.demo.BaseBluetoothSocket.java
cn.edu.hust.cm.bt.demo.LocalBinder.java
cn.edu.hust.cm.bt.demo.MainActivity.java
cn.edu.hust.cm.bt.demo.client.ClientMainActivity.java
cn.edu.hust.cm.bt.demo.client.package-info.java
cn.edu.hust.cm.bt.demo.server.BluetoothServerThread.java
cn.edu.hust.cm.bt.demo.server.BluetoothServer.java
cn.edu.hust.cm.bt.demo.server.ServerBinderActivity.java
cn.edu.hust.cm.bt.demo.server.ServerBinderService.java
cn.edu.hust.cm.bt.demo.server.ServerMainActivity.java
cn.edu.hust.cm.bt.demo.server.package-info.java