Back to project page BtDemo.
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.
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"); } }