Android Open Source - BtDemo Server Binder Service






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;
// www .j  a v a2 s.c  o  m
import java.io.IOException;

import android.content.Intent;
import android.util.Log;
import cn.edu.hust.cm.bt.demo.BaseBinderService;

public class ServerBinderService extends BaseBinderService {

    public static final String TAG = "ServerBinderService";

    private BluetoothServer server;

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "all clients have disconnected from me!!!");

        // shutdown BT server to prevent Thread leaked
        shutdownServer();

        // TODO but in a real app, we SHOULD NOT shutdown BT server here
        return super.onUnbind(intent);
    }

    public String testMe() {
        Log.i(TAG, "in testMe");
        return "a value returned from testMe";
    }

    public boolean setupServer(String name, String uuid) {
        if (null != server && server.isRunning()) {
            Log.i(TAG, "BT server is already running");
            return true;
        }

        Log.i(TAG, "launching a BT server...");
        server = new BluetoothServer(name, uuid);
        try {
            server.setup();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            onSetupError();
            return false;
        }
    }

    public void shutdownServer() {
        if (null != server && server.isRunning()) {
            try {
                server.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
                onTeardownError();
            }
        }
    }

    private void onSetupError() {
        Log.e(TAG, "error occured when setup BT server");
    }

    private void onTeardownError() {
        Log.e(TAG, "error occured when teardown BT server");
    }
}




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