public void startService() { if (connectionType == Bluetooth) { if (mBluetoothConnectionService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mBluetoothConnectionService.getState() == BluetoothConnectionService.STATE_NONE) { // Start the BluetoothConnectionService mBluetoothConnectionService.start(); } } } else if (connectionType == WiFi) { if (mWiFiConnectionService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mWiFiConnectionService.getState() == WiFiConnectionService.STATE_NONE) { // Start the BluetoothConnectionService mWiFiConnectionService.start(); } } } }
@Override public void onTerminate() { super.onTerminate(); // Stop the connection services if (mBluetoothConnectionService != null) mBluetoothConnectionService.stop(); if (mWiFiConnectionService != null) { mWiFiConnectionService.stop(); mWiFiConnectionService = null; } stopService(intent); mLogArrayAdapter.clear(); Log.i(TAG, "onTerminated"); }
/** * Sends a message. * * @param message A string of text to send. */ public synchronized void sendMessage(String message) { if (connectionType == Bluetooth) { // Check that we're actually connected before trying anything if (mBluetoothConnectionService.getState() != BluetoothConnectionService.STATE_CONNECTED) { Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); return; } // Check that there's actually something to send if (message.length() > 0) { // Get the message bytes and tell the BluetoothConnectionService to write byte[] send = message.getBytes(); mBluetoothConnectionService.write(send); // Reset out string buffer to zero and clear the edit text field mOutStringBuffer.setLength(0); // mOutEditText.setText(mOutStringBuffer); } } else if (connectionType == WiFi) { // Check that we're actually connected before trying anything if (mWiFiConnectionService.getState() != WiFiConnectionService.STATE_CONNECTED) { Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); return; } // Check that there's actually something to send if (message.length() > 0) { // Get the message bytes and tell the WiFiConnectionService to write mWiFiConnectionService.sendMessage(message); // Reset out string buffer to zero and clear the edit text field mOutStringBuffer.setLength(0); // mOutEditText.setText(mOutStringBuffer); } } }
public void stopService() { if (mBluetoothConnectionService != null) mBluetoothConnectionService.stop(); if (mWiFiConnectionService != null) mWiFiConnectionService.stop(); }