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; /*from w w w .j a v a 2 s . c o m*/ import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; import cn.edu.hust.cm.bt.demo.BaseBinderActivity; import cn.edu.hust.cm.bt.demo.R; public class ServerBinderActivity extends BaseBinderActivity { public static final String TAG = "ServerBinderActivity"; private static final int REQUEST_CODE_ENABLE_BT = 100; private ServerBinderService mService; public static void startMe(Context context) { Intent intent = new Intent(context, ServerBinderActivity.class); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_server_binder); doBind(new Intent(this, ServerBinderService.class)); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.i(TAG, "requestCode=" + requestCode + ", resultCode=" + resultCode); switch (requestCode) { case REQUEST_CODE_ENABLE_BT: if (RESULT_OK == resultCode) { // we can setup bt server now doSetupServer(null); } else if (RESULT_CANCELED == resultCode) { Toast.makeText(this, R.string.can_not_enable_bt, Toast.LENGTH_SHORT).show(); } break; default: break; } } @Override protected void postConnected() { super.postConnected(); Log.i(TAG, "ServiceConnection established"); mService = (ServerBinderService) super.mService; } public void onClick(View view) { int id = view.getId(); switch (id) { case R.id.btn_run_test: doRunTest(view); break; case R.id.btn_disable_bt: doDisableBt(view); break; case R.id.btn_setup_server: doSetupServer(view); break; case R.id.btn_shutdown_server: doShutdownServer(view); break; default: break; } } private void doRunTest(View view) { if (mIsBound) { String ret = mService.testMe(); Log.i(TAG, "ret=" + ret); Toast.makeText(this, "ret=" + ret, Toast.LENGTH_SHORT).show(); } } private void doDisableBt(View view) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (null != adapter && adapter.isEnabled()) { // FIXME this is an async call. We shoud listen for ACTION_STATE_CHANGED adapter.disable(); } } private void doSetupServer(View view) { if (!isBtSupported()) { Log.e(TAG, "this device is not support BT"); return; } if (!isBtOn()) { // bt is disabled and not ready for use Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_CODE_ENABLE_BT); return; } if (mIsBound) { boolean result = mService.setupServer("test_name", "c30c02b3-453d-4a7d-8a4d-0cc6c1ee1888"); Toast.makeText(this, "setupServer=" + result, Toast.LENGTH_SHORT) .show(); } } private void doShutdownServer(View view) { if (mIsBound) { mService.shutdownServer(); } } private boolean isBtOn() { // TODO move this method to an Util class BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (null != adapter) { return adapter.isEnabled(); } return false; } private boolean isBtSupported() { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); return null != adapter; } }