Added disconnect toast message.

This commit is contained in:
Dennis Guse
2019-10-26 14:23:38 +02:00
parent 7d807a7f11
commit 3a6f417662
3 changed files with 25 additions and 15 deletions
@@ -23,9 +23,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -42,10 +40,10 @@ import de.dennisguse.opentracks.content.sensor.SensorState;
public class BluetoothConnectionManager {
// Message types sent to handler
static final int MESSAGE_DEVICE_NAME = 1;
static final int MESSAGE_READ = 2;
static final String KEY_DEVICE_NAME = "device_name";
static final int MESSAGE_CONNECTING = 1;
static final int MESSAGE_CONNECTED = 2;
static final int MESSAGE_READ = 3;
static final int MESSAGE_DISCONNECTED = 4;
private static final UUID HEART_RATE_SERVICE_UUID = new UUID(0x180D00001000L, 0x800000805f9b34fbL);
private static final UUID HEART_RATE_MEASUREMENT_CHAR_UUID = new UUID(0x2A3700001000L, 0x800000805f9b34fbL);
@@ -68,18 +66,15 @@ public class BluetoothConnectionManager {
case BluetoothProfile.STATE_CONNECTING:
Log.d(TAG, "Connecting to sensor: " + gatt.getDevice());
setState(SensorState.CONNECTING);
handler.obtainMessage(MESSAGE_CONNECTING, gatt.getDevice().getName()).sendToTarget();
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Connected to sensor: " + gatt.getDevice());
setState(SensorState.CONNECTED);
gatt.discoverServices();
//Inform about status change
Message message = handler.obtainMessage(MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(KEY_DEVICE_NAME, gatt.getDevice().getName());
message.setData(bundle);
handler.sendMessage(message);
handler.obtainMessage(MESSAGE_CONNECTED, gatt.getDevice().getName()).sendToTarget();
break;
case BluetoothProfile.STATE_DISCONNECTING:
Log.d(TAG, "Disconnecting from sensor: " + gatt.getDevice());
@@ -88,6 +83,8 @@ public class BluetoothConnectionManager {
case BluetoothProfile.STATE_DISCONNECTED:
Log.d(TAG, "Disconnected from sensor: " + gatt.getDevice());
setState(SensorState.DISCONNECTED);
handler.obtainMessage(MESSAGE_DISCONNECTED, gatt.getDevice().getName()).sendToTarget();
}
}
@@ -49,10 +49,13 @@ public class BluetoothRemoteSensorManager extends RemoteSensorManager {
private final Handler messageHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
String toastMessage;
switch (message.what) {
case BluetoothConnectionManager.MESSAGE_DEVICE_NAME:
String deviceName = message.getData().getString(BluetoothConnectionManager.KEY_DEVICE_NAME);
String toastMessage = context.getString(R.string.settings_sensor_connected, deviceName);
case BluetoothConnectionManager.MESSAGE_CONNECTING:
//Ignore for now.
break;
case BluetoothConnectionManager.MESSAGE_CONNECTED:
toastMessage = context.getString(R.string.settings_sensor_connected, message.obj);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
break;
case BluetoothConnectionManager.MESSAGE_READ:
@@ -63,7 +66,12 @@ public class BluetoothRemoteSensorManager extends RemoteSensorManager {
sensorDataSet = (SensorDataSet) message.obj;
}
break;
case BluetoothConnectionManager.MESSAGE_DISCONNECTED:
toastMessage = context.getString(R.string.settings_sensor_disconnected, message.obj);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
break;
default:
Log.e(TAG, "Got an undefined case. Please check.");
break;
}
}
+5
View File
@@ -1278,6 +1278,11 @@ limitations under the License.
%1$s sensor name. E.g., 'Polar Bluetooth'.">
Connected to %1$s
</string>
<string name="settings_sensor_disconnected" translation_description="A message when a sensor is connected.
%1$s sensor name. E.g., 'Polar Bluetooth'.">
Disconnected from %1$s
</string>
<!-- Settings Stats -->
<string name="settings_stats_coordinate" translation_description="In the 'Stats settings, the option to show latitude and longitude.