From 3f58a7d10db9d5d15b40427296ad49869418f139 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Mon, 9 Jul 2018 14:06:24 +0200 Subject: [PATCH] update --- .idea/misc.xml | 2 +- Application/Application.iml | 1 - .../iconsole/BluetoothChatFragment.java | 285 +++++++++--------- .../iconsole/BluetoothChatService.java | 83 ++--- android_antlib_4-14/android_antlib_4-14.iml | 1 + 5 files changed, 180 insertions(+), 192 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 3b6674f..5bae4e2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + diff --git a/Application/Application.iml b/Application/Application.iml index 153210c..cf59ce6 100644 --- a/Application/Application.iml +++ b/Application/Application.iml @@ -91,7 +91,6 @@ - diff --git a/Application/src/main/java/org/surfsite/iconsole/BluetoothChatFragment.java b/Application/src/main/java/org/surfsite/iconsole/BluetoothChatFragment.java index 1746361..8087e1f 100644 --- a/Application/src/main/java/org/surfsite/iconsole/BluetoothChatFragment.java +++ b/Application/src/main/java/org/surfsite/iconsole/BluetoothChatFragment.java @@ -32,6 +32,7 @@ import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -42,8 +43,6 @@ import android.widget.Button; import android.widget.NumberPicker; import android.widget.TextView; import android.widget.Toast; -import android.util.Log; -import android.content.Intent; /** * This fragment controls Bluetooth to communicate with other devices. @@ -76,7 +75,7 @@ public class BluetoothChatFragment extends Fragment { /** * Array adapter for the conversation thread - private ArrayAdapter mConversationArrayAdapter; + private ArrayAdapter mConversationArrayAdapter; */ /** @@ -90,7 +89,132 @@ public class BluetoothChatFragment extends Fragment { private BluetoothChatService mChatService = null; private boolean mIsBound; private ChannelService.ChannelServiceComm mChannelService; + /** + * The Handler that gets information back from the BluetoothChatService + */ + private final Handler mHandler = new Handler() { + @SuppressLint("DefaultLocale") + @Override + public void handleMessage(Message msg) { + FragmentActivity activity = getActivity(); + switch (msg.what) { + case Constants.MESSAGE_STATE_CHANGE: + switch (msg.arg1) { + case BluetoothChatService.STATE_CONNECTED: + setStatus(getString(R.string.title_connected_to, mConnectedDeviceName)); + //mConversationArrayAdapter.clear(); + mStartButton.setEnabled(true); + mStopButton.setEnabled(true); + mDisconnectButton.setEnabled(true); + mLevel.setEnabled(true); + mLevel.setValue(1); + break; + case BluetoothChatService.STATE_CONNECTING: + setStatus(R.string.title_connecting); + mStartButton.setEnabled(false); + mStopButton.setEnabled(false); + mDisconnectButton.setEnabled(false); + mLevel.setEnabled(false); + break; + case BluetoothChatService.STATE_LISTEN: + case BluetoothChatService.STATE_NONE: + setStatus(R.string.title_not_connected); + mStartButton.setEnabled(false); + mStopButton.setEnabled(false); + mDisconnectButton.setEnabled(false); + mLevel.setEnabled(false); + break; + } + break; + case Constants.MESSAGE_DATA: + if (!(msg.obj instanceof IConsole.Data)) + return; + IConsole.Data data = (IConsole.Data) msg.obj; + mChannelService.setSpeed(data.mSpeed10 / 10.0); + mChannelService.setPower(data.mPower10 / 10); + mChannelService.setCadence(data.mRPM); + + mSpeedText.setText(String.format("% 3.1f", data.mSpeed10 / 10.0)); + mPowerText.setText(String.format("% 3.1f", data.mPower10 / 10.0)); + mRPMText.setText(String.format("%d", data.mRPM)); + mDistanceText.setText(String.format("% 3.1f", data.mDistance10 / 10.0)); + mCaloriesText.setText(String.format("% 3d", data.mCalories)); + mHFText.setText(String.format("%d", data.mHF)); + mTimeText.setText(String.format("%s", data.getTimeStr())); + //mLevel.setValue(data.mLevel); + break; + case Constants.MESSAGE_WRITE: + //byte[] writeBuf = (byte[]) msg.obj; + // construct a string from the buffer + //String writeMessage = new String(writeBuf); + //mConversationArrayAdapter.add("Me: " + writeMessage); + break; + case Constants.MESSAGE_READ: + //byte[] readBuf = (byte[]) msg.obj; + // construct a string from the valid bytes in the buffer + //String readMessage = new String(readBuf, 0, msg.arg1); + //mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage); + break; + case Constants.MESSAGE_DEVICE_NAME: + // save the connected device's name + mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME); + if (null != activity) { + Toast.makeText(activity, "Connected to " + + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); + } + break; + case Constants.MESSAGE_TOAST: + if (null != activity) { + Toast.makeText(activity, msg.getData().getString(Constants.TOAST), + Toast.LENGTH_SHORT).show(); + } + break; + } + } + }; private boolean mChannelServiceBound = false; + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + // This is called when the connection with the service has been + // established, giving us the service object we can use to + // interact with the service. Because we have bound to a explicit + // service that we know is running in our own process, we can + // cast its IBinder to a concrete class and directly access it. + mChatService = ((BluetoothChatService.BluetoothChatServiceI) service).getService(); + ((BluetoothChatService.BluetoothChatServiceI) service).setHandler(mHandler); + Log.d(TAG, "onServiceConnected()"); + } + + public void onServiceDisconnected(ComponentName className) { + // This is called when the connection with the service has been + // unexpectedly disconnected -- that is, its process crashed. + // Because it is running in our same process, we should never + // see this happen. + mChatService = null; + + } + }; + private ServiceConnection mChannelServiceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder serviceBinder) { + Log.v(TAG, "mChannelServiceConnection.onServiceConnected..."); + + mChannelService = (ChannelService.ChannelServiceComm) serviceBinder; + + + Log.v(TAG, "...mChannelServiceConnection.onServiceConnected"); + } + + @Override + public void onServiceDisconnected(ComponentName arg0) { + Log.v(TAG, "mChannelServiceConnection.onServiceDisconnected..."); + + // Clearing and disabling when disconnecting from ChannelService + mChannelService = null; + + Log.v(TAG, "...mChannelServiceConnection.onServiceDisconnected"); + } + }; @Override public void onCreate(Bundle savedInstanceState) { @@ -107,7 +231,6 @@ public class BluetoothChatFragment extends Fragment { } } - @Override public void onStart() { super.onStart(); @@ -120,7 +243,7 @@ public class BluetoothChatFragment extends Fragment { } else if (mChatService == null) { setupChat(); } - if(!mChannelServiceBound) doBindChannelService(); + if (!mChannelServiceBound) doBindChannelService(); } @@ -180,28 +303,6 @@ public class BluetoothChatFragment extends Fragment { mTimeText = (TextView) view.findViewById(R.id.Time); } - private ServiceConnection mConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName className, IBinder service) { - // This is called when the connection with the service has been - // established, giving us the service object we can use to - // interact with the service. Because we have bound to a explicit - // service that we know is running in our own process, we can - // cast its IBinder to a concrete class and directly access it. - mChatService = ((BluetoothChatService.BluetoothChatServiceI)service).getService(); - ((BluetoothChatService.BluetoothChatServiceI)service).setHandler(mHandler); - Log.d(TAG, "onServiceConnected()"); - } - - public void onServiceDisconnected(ComponentName className) { - // This is called when the connection with the service has been - // unexpectedly disconnected -- that is, its process crashed. - // Because it is running in our same process, we should never - // see this happen. - mChatService = null; - - } - }; - void doBindService() { Log.d(TAG, "doBindService()"); @@ -209,7 +310,7 @@ public class BluetoothChatFragment extends Fragment { // class name because we want a specific service implementation that // we know will be running in our own process (and thus won't be // supporting component replacement by other applications). - getActivity().bindService(new Intent(getActivity(), BluetoothChatService.class), mConnection , Context.BIND_AUTO_CREATE); + getActivity().bindService(new Intent(getActivity(), BluetoothChatService.class), mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; } @@ -221,53 +322,25 @@ public class BluetoothChatFragment extends Fragment { } } - private ServiceConnection mChannelServiceConnection = new ServiceConnection() - { - @Override - public void onServiceConnected(ComponentName name, IBinder serviceBinder) - { - Log.v(TAG, "mChannelServiceConnection.onServiceConnected..."); - - mChannelService = (ChannelService.ChannelServiceComm) serviceBinder; - - - Log.v(TAG, "...mChannelServiceConnection.onServiceConnected"); - } - - @Override - public void onServiceDisconnected(ComponentName arg0) - { - Log.v(TAG, "mChannelServiceConnection.onServiceDisconnected..."); - - // Clearing and disabling when disconnecting from ChannelService - mChannelService = null; - - Log.v(TAG, "...mChannelServiceConnection.onServiceDisconnected"); - } - }; - - private void doBindChannelService() - { + private void doBindChannelService() { Log.v(TAG, "doBindChannelService..."); // Binds to ChannelService. ChannelService binds and manages connection between the // app and the ANT Radio Service - mChannelServiceBound = getActivity().bindService(new Intent(getActivity(), ChannelService.class), mChannelServiceConnection , Context.BIND_AUTO_CREATE); + mChannelServiceBound = getActivity().bindService(new Intent(getActivity(), ChannelService.class), mChannelServiceConnection, Context.BIND_AUTO_CREATE); - if(!mChannelServiceBound) //If the bind returns false, run the unbind method to update the GUI + if (!mChannelServiceBound) //If the bind returns false, run the unbind method to update the GUI doUnbindChannelService(); - Log.i(TAG, " Channel Service binding = "+ mChannelServiceBound); + Log.i(TAG, " Channel Service binding = " + mChannelServiceBound); Log.v(TAG, "...doBindChannelService"); } - private void doUnbindChannelService() - { + private void doUnbindChannelService() { Log.v(TAG, "doUnbindChannelService..."); - if(mChannelServiceBound) - { + if (mChannelServiceBound) { getActivity().unbindService(mChannelServiceConnection); mChannelServiceBound = false; @@ -276,7 +349,6 @@ public class BluetoothChatFragment extends Fragment { Log.v(TAG, "...doUnbindChannelService"); } - /** * Set up the UI and background operations for chat. */ @@ -351,7 +423,7 @@ public class BluetoothChatFragment extends Fragment { if (mChatService == null) return; - // Check that we're actually connected before trying anything + // Check that we're actually connected before trying anything if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show(); return; @@ -394,91 +466,6 @@ public class BluetoothChatFragment extends Fragment { actionBar.setSubtitle(subTitle); } - - /** - * The Handler that gets information back from the BluetoothChatService - */ - private final Handler mHandler = new Handler() { - @SuppressLint("DefaultLocale") - @Override - public void handleMessage(Message msg) { - FragmentActivity activity = getActivity(); - switch (msg.what) { - case Constants.MESSAGE_STATE_CHANGE: - switch (msg.arg1) { - case BluetoothChatService.STATE_CONNECTED: - setStatus(getString(R.string.title_connected_to, mConnectedDeviceName)); - //mConversationArrayAdapter.clear(); - mStartButton.setEnabled(true); - mStopButton.setEnabled(true); - mDisconnectButton.setEnabled(true); - mLevel.setEnabled(true); - mLevel.setValue(1); - break; - case BluetoothChatService.STATE_CONNECTING: - setStatus(R.string.title_connecting); - mStartButton.setEnabled(false); - mStopButton.setEnabled(false); - mDisconnectButton.setEnabled(false); - mLevel.setEnabled(false); - break; - case BluetoothChatService.STATE_LISTEN: - case BluetoothChatService.STATE_NONE: - setStatus(R.string.title_not_connected); - mStartButton.setEnabled(false); - mStopButton.setEnabled(false); - mDisconnectButton.setEnabled(false); - mLevel.setEnabled(false); - break; - } - break; - case Constants.MESSAGE_DATA: - if (!(msg.obj instanceof IConsole.Data)) - return; - IConsole.Data data = (IConsole.Data) msg.obj; - mChannelService.setSpeed(data.mSpeed10 / 10.0); - mChannelService.setPower(data.mPower10 / 10); - mChannelService.setCadence(data.mRPM); - - mSpeedText.setText(String.format("% 3.1f", data.mSpeed10 / 10.0)); - mPowerText.setText(String.format("% 3.1f", data.mPower10 / 10.0)); - mRPMText.setText(String.format("%d", data.mRPM)); - mDistanceText.setText(String.format("% 3.1f", data.mDistance10 / 10.0)); - mCaloriesText.setText(String.format("% 3d", data.mCalories)); - mHFText.setText(String.format("%d", data.mHF)); - mTimeText.setText(String.format("%s",data.getTimeStr())); - //mLevel.setValue(data.mLevel); - break; - case Constants.MESSAGE_WRITE: - //byte[] writeBuf = (byte[]) msg.obj; - // construct a string from the buffer - //String writeMessage = new String(writeBuf); - //mConversationArrayAdapter.add("Me: " + writeMessage); - break; - case Constants.MESSAGE_READ: - //byte[] readBuf = (byte[]) msg.obj; - // construct a string from the valid bytes in the buffer - //String readMessage = new String(readBuf, 0, msg.arg1); - //mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage); - break; - case Constants.MESSAGE_DEVICE_NAME: - // save the connected device's name - mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME); - if (null != activity) { - Toast.makeText(activity, "Connected to " - + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); - } - break; - case Constants.MESSAGE_TOAST: - if (null != activity) { - Toast.makeText(activity, msg.getData().getString(Constants.TOAST), - Toast.LENGTH_SHORT).show(); - } - break; - } - } - }; - public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CONNECT_DEVICE_SECURE: diff --git a/Application/src/main/java/org/surfsite/iconsole/BluetoothChatService.java b/Application/src/main/java/org/surfsite/iconsole/BluetoothChatService.java index 8bd3b96..1085fc7 100644 --- a/Application/src/main/java/org/surfsite/iconsole/BluetoothChatService.java +++ b/Application/src/main/java/org/surfsite/iconsole/BluetoothChatService.java @@ -23,14 +23,12 @@ import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; -import android.content.Context; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; - import android.util.Log; import java.io.IOException; @@ -45,32 +43,32 @@ import java.util.UUID; * thread for performing data transmissions when connected. */ public class BluetoothChatService extends Service { - // Debugging - private static final String TAG = "BluetoothChatService"; - // Name for the SDP record when creating server socket - private static final String NAME_SECURE = "BluetoothChatSecure"; - private static final String NAME_INSECURE = "BluetoothChatInsecure"; - - // Unique UUID for this application - private static final UUID SERIAL_PORT_CLASS = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); - // Member fields - private final BluetoothAdapter mAdapter; - private Handler mHandler; - - private ConnectThread mConnectThread; - private ConnectedThread mConnectedThread; - private int mState; - private int mNewState; - // Constants that indicate the current connection state public static final int STATE_NONE = 0; // we're doing nothing public static final int STATE_LISTEN = 1; // now listening for incoming connections public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection public static final int STATE_CONNECTED = 3; // now connected to a remote device + // Debugging + private static final String TAG = "BluetoothChatService"; + // Name for the SDP record when creating server socket + private static final String NAME_SECURE = "BluetoothChatSecure"; + private static final String NAME_INSECURE = "BluetoothChatInsecure"; + // Unique UUID for this application + private static final UUID SERIAL_PORT_CLASS = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); + // Member fields + private final BluetoothAdapter mAdapter; + // This is the object that receives interactions from clients. See + // RemoteService for a more complete example. + private final IBinder mBinder = new BluetoothChatServiceI(); + private Handler mHandler; + private ConnectThread mConnectThread; + private ConnectedThread mConnectedThread; + private int mState; + private int mNewState; private NotificationManager mNM; - private int NOTIFICATION = R.string.local_service_started; + public BluetoothChatService() { super(); mAdapter = BluetoothAdapter.getDefaultAdapter(); @@ -78,21 +76,11 @@ public class BluetoothChatService extends Service { mNewState = mState; } - - public class BluetoothChatServiceI extends Binder { - BluetoothChatService getService() { - return BluetoothChatService.this; - } - void setHandler(Handler handler) { - mHandler = handler; - } - } - @Override public void onCreate() { super.onCreate(); - mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @@ -144,11 +132,6 @@ public class BluetoothChatService extends Service { return mBinder; } - - // This is the object that receives interactions from clients. See - // RemoteService for a more complete example. - private final IBinder mBinder = new BluetoothChatServiceI(); - /** * Update UI title according to the current state of the chat connection */ @@ -186,6 +169,7 @@ public class BluetoothChatService extends Service { super.onDestroy(); } + /** * Show a notification while this service is running. */ @@ -288,7 +272,6 @@ public class BluetoothChatService extends Service { updateUserInterfaceTitle(); } - /** * Indicate that the connection attempt failed and notify the UI Activity. */ @@ -327,12 +310,22 @@ public class BluetoothChatService extends Service { BluetoothChatService.this.startBT(); } + public class BluetoothChatServiceI extends Binder { + BluetoothChatService getService() { + return BluetoothChatService.this; + } + + void setHandler(Handler handler) { + mHandler = handler; + } + } + /** * This thread runs while attempting to make an outgoing connection * with a device. It runs straight through; the connection either * succeeds or fails. */ - class ConnectThread extends Thread { + class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; @@ -481,9 +474,17 @@ public class BluetoothChatService extends Service { } - public boolean setLevel(int level) { return mmIConsole.setLevel(level); } - public boolean startIConsole() { return mmIConsole.start(); } - public boolean stopIConsole() { return mmIConsole.stop(); } + public boolean setLevel(int level) { + return mmIConsole.setLevel(level); + } + + public boolean startIConsole() { + return mmIConsole.start(); + } + + public boolean stopIConsole() { + return mmIConsole.stop(); + } public void cancel() { mmIConsole.stop(); diff --git a/android_antlib_4-14/android_antlib_4-14.iml b/android_antlib_4-14/android_antlib_4-14.iml index 18306c8..47b5059 100644 --- a/android_antlib_4-14/android_antlib_4-14.iml +++ b/android_antlib_4-14/android_antlib_4-14.iml @@ -21,5 +21,6 @@ + \ No newline at end of file