正在查看: xDrip+ v04633772025.07.16 应用的 DexShareCollectionService.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: xDrip+ v04633772025.07.16 应用的 DexShareCollectionService.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.eveningoutpost.dexdrip.services;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.importedlibraries.dexcom.ReadDataShare;
import com.eveningoutpost.dexdrip.importedlibraries.dexcom.records.CalRecord;
import com.eveningoutpost.dexdrip.importedlibraries.dexcom.records.EGVRecord;
import com.eveningoutpost.dexdrip.importedlibraries.dexcom.records.SensorRecord;
import com.eveningoutpost.dexdrip.models.ActiveBluetoothDevice;
import com.eveningoutpost.dexdrip.models.BgReading;
import com.eveningoutpost.dexdrip.models.Calibration;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.Sensor;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.utilitymodels.DexShareAttributes;
import com.eveningoutpost.dexdrip.utilitymodels.ForegroundServiceStarter;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import rx.Observable;
import rx.functions.Action1;
@TargetApi(19)
public class DexShareCollectionService extends Service {
private static final String TAG = "DexShareCollectionService";
private static PendingIntent pendingIntent;
private static int statusErrors;
public int currentGattTask;
private BluetoothDevice device;
private ForegroundServiceStarter foregroundServiceStarter;
private BluetoothGattCharacteristic mAuthenticationCharacteristic;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
private BluetoothManager mBluetoothManager;
private BluetoothGattCharacteristic mCommandCharacteristic;
Action1<byte[]> mDataResponseListener;
private String mDeviceAddress;
private String mDeviceName;
private BluetoothGattCharacteristic mHeartBeatCharacteristic;
private BluetoothGattCharacteristic mReceiveDataCharacteristic;
private BluetoothGattCharacteristic mResponseCharacteristic;
private BluetoothGattCharacteristic mSendDataCharacteristic;
private BluetoothGattService mShareService;
SharedPreferences prefs;
ReadDataShare readData;
public int recordType;
public Service service;
public int step;
public int successfulWrites;
public List<byte[]> writePackets;
private boolean is_connected = false;
private boolean reconnecting = false;
private int mConnectionState = 0;
public final int GATT_NOTHING = 0;
public final int GATT_SETUP = 1;
public final int GATT_WRITING_COMMANDS = 2;
public final int GATT_READING_RESPONSE = 3;
public boolean state_authSucess = false;
public boolean state_authInProgress = false;
public boolean state_notifSetupSucess = false;
public boolean shouldDisconnect = false;
public boolean share2 = false;
private long lastHeartbeat = 0;
private int heartbeatCount = 0;
private double instance = BgReading.BESTOFFSET;
public SharedPreferences.OnSharedPreferenceChangeListener prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String str) {
if (str.compareTo("run_service_in_foreground") == 0) {
UserError.Log.d("FOREGROUND", "run_service_in_foreground changed!");
if (sharedPreferences.getBoolean("run_service_in_foreground", false)) {
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
dexShareCollectionService.foregroundServiceStarter = new ForegroundServiceStarter(dexShareCollectionService.getApplicationContext(), DexShareCollectionService.this.service);
DexShareCollectionService.this.foregroundServiceStarter.start();
UserError.Log.i(DexShareCollectionService.TAG, "Moving to foreground");
return;
}
DexShareCollectionService.this.service.stopForeground(true);
UserError.Log.i(DexShareCollectionService.TAG, "Removing from foreground");
}
}
};
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
if (DexShareCollectionService.this.mBluetoothGatt != null && DexShareCollectionService.this.mBluetoothGatt.getDevice() != null && bluetoothDevice != null && !bluetoothDevice.getAddress().equals(DexShareCollectionService.this.mBluetoothGatt.getDevice().getAddress())) {
UserError.Log.d(DexShareCollectionService.TAG, "Bond state wrong device");
} else if ("android.bluetooth.device.action.BOND_STATE_CHANGED".equals(action) && intent.getIntExtra("android.bluetooth.device.extra.BOND_STATE", RecyclerView.UNDEFINED_DURATION) == 12) {
DexShareCollectionService.this.authenticateConnection();
}
}
};
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
UserError.Log.i(DexShareCollectionService.TAG, "Gatt state change status: " + i + " new state: " + i2);
if (i == 133) {
DexShareCollectionService.access$208();
UserError.Log.e(DexShareCollectionService.TAG, "Got the status 133 bug, bad news! count:" + DexShareCollectionService.statusErrors + " - Might require devices to forget each other: instance uptime: " + JoH.qs((JoH.ts() - DexShareCollectionService.this.instance) / 1000.0d, 0));
if (DexShareCollectionService.statusErrors > 4) {
UserError.Log.wtf(DexShareCollectionService.TAG, "Forcing bluetooth reset to try to combat errors");
int unused = DexShareCollectionService.statusErrors = 0;
JoH.niceRestartBluetooth(DexShareCollectionService.this.getApplicationContext());
DexShareCollectionService.this.setRetryTimer();
DexShareCollectionService.this.close();
DexShareCollectionService.this.stopSelf();
return;
}
}
if (i2 != 2) {
if (i2 == 0) {
DexShareCollectionService.this.mConnectionState = 0;
ActiveBluetoothDevice.disconnected();
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
if (dexShareCollectionService.shouldDisconnect) {
dexShareCollectionService.stopSelf();
} else {
dexShareCollectionService.setRetryTimer();
}
UserError.Log.d(DexShareCollectionService.TAG, "Disconnected from GATT server.");
return;
}
UserError.Log.d(DexShareCollectionService.TAG, "Gatt callback... strange state.");
return;
}
DexShareCollectionService.this.mBluetoothGatt = bluetoothGatt;
DexShareCollectionService dexShareCollectionService2 = DexShareCollectionService.this;
dexShareCollectionService2.device = dexShareCollectionService2.mBluetoothGatt.getDevice();
DexShareCollectionService.this.mConnectionState = 2;
ActiveBluetoothDevice.connected();
UserError.Log.i(DexShareCollectionService.TAG, "Connected to GATT server.");
UserError.Log.i(DexShareCollectionService.TAG, "discovering services");
DexShareCollectionService dexShareCollectionService3 = DexShareCollectionService.this;
dexShareCollectionService3.currentGattTask = 1;
if (dexShareCollectionService3.mBluetoothGatt == null || !DexShareCollectionService.this.mBluetoothGatt.discoverServices()) {
UserError.Log.w(DexShareCollectionService.TAG, "discovering failed");
DexShareCollectionService dexShareCollectionService4 = DexShareCollectionService.this;
if (dexShareCollectionService4.shouldDisconnect) {
dexShareCollectionService4.stopSelf();
} else {
dexShareCollectionService4.setRetryTimer();
}
}
}
@Override
public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
UserError.Log.d(DexShareCollectionService.TAG, "services discovered " + i);
if (i != 0 || DexShareCollectionService.this.mBluetoothGatt == null) {
UserError.Log.w(DexShareCollectionService.TAG, "No Services Discovered!");
return;
}
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
dexShareCollectionService.mShareService = dexShareCollectionService.mBluetoothGatt.getService(DexShareAttributes.CradleService);
if (DexShareCollectionService.this.mShareService == null) {
DexShareCollectionService dexShareCollectionService2 = DexShareCollectionService.this;
dexShareCollectionService2.mShareService = dexShareCollectionService2.mBluetoothGatt.getService(DexShareAttributes.CradleService2);
DexShareCollectionService.this.share2 = true;
} else {
DexShareCollectionService.this.share2 = false;
}
DexShareCollectionService.this.assignCharacteristics();
DexShareCollectionService.this.authenticateConnection();
DexShareCollectionService.this.gattSetupStep();
}
@Override
public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
if (i == 0) {
UserError.Log.v(DexShareCollectionService.TAG, "Characteristic Read " + bluetoothGattCharacteristic.getUuid());
if (DexShareCollectionService.this.mHeartBeatCharacteristic.getUuid().equals(bluetoothGattCharacteristic.getUuid())) {
UserError.Log.v(DexShareCollectionService.TAG, "Characteristic Read " + bluetoothGattCharacteristic.getUuid() + " " + bluetoothGattCharacteristic.getValue());
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
dexShareCollectionService.setCharacteristicNotification(dexShareCollectionService.mHeartBeatCharacteristic);
}
bluetoothGatt.readCharacteristic(DexShareCollectionService.this.mHeartBeatCharacteristic);
return;
}
UserError.Log.e(DexShareCollectionService.TAG, "Characteristic failed to read");
}
@Override
public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
UUID uuid = bluetoothGattCharacteristic.getUuid();
UserError.Log.d(DexShareCollectionService.TAG, "Characteristic Update Received: " + uuid);
if (uuid.compareTo(DexShareCollectionService.this.mReceiveDataCharacteristic.getUuid()) == 0) {
UserError.Log.d(DexShareCollectionService.TAG, "mCharReceiveData Update");
if (bluetoothGattCharacteristic.getValue() != null) {
Observable.just(bluetoothGattCharacteristic.getValue()).subscribe(DexShareCollectionService.this.mDataResponseListener);
return;
}
return;
}
if (uuid.compareTo(DexShareCollectionService.this.mHeartBeatCharacteristic.getUuid()) == 0) {
long currentTimeMillis = System.currentTimeMillis();
UserError.Log.d(DexShareCollectionService.TAG, "Heartbeat delta: " + (currentTimeMillis - DexShareCollectionService.this.lastHeartbeat));
if (currentTimeMillis - DexShareCollectionService.this.lastHeartbeat < 59000 || DexShareCollectionService.this.heartbeatCount > 5) {
UserError.Log.d(DexShareCollectionService.TAG, "Early heartbeat. Fetching data.");
((AlarmManager) DexShareCollectionService.this.getSystemService("alarm")).cancel(DexShareCollectionService.pendingIntent);
DexShareCollectionService.this.heartbeatCount = 0;
DexShareCollectionService.this.attemptConnection();
}
DexShareCollectionService.access$1212(DexShareCollectionService.this, 1);
DexShareCollectionService.this.lastHeartbeat = currentTimeMillis;
}
}
@Override
public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
if (i == 0) {
BluetoothGattCharacteristic characteristic = bluetoothGattDescriptor.getCharacteristic();
UserError.Log.d(DexShareCollectionService.TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
if (DexShareCollectionService.this.mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
dexShareCollectionService.state_notifSetupSucess = true;
dexShareCollectionService.setCharacteristicIndication(dexShareCollectionService.mReceiveDataCharacteristic);
}
if (DexShareCollectionService.this.mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
DexShareCollectionService dexShareCollectionService2 = DexShareCollectionService.this;
dexShareCollectionService2.setCharacteristicIndication(dexShareCollectionService2.mResponseCharacteristic);
}
if (DexShareCollectionService.this.mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
DexShareCollectionService.this.attemptRead();
return;
}
return;
}
if ((i & 5) == 0 && (i & 15) == 0) {
UserError.Log.e(DexShareCollectionService.TAG, "Unknown error writing descriptor");
return;
}
if (bluetoothGatt.getDevice().getBondState() != 10) {
UserError.Log.e(DexShareCollectionService.TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: " + i + "bond: " + bluetoothGatt.getDevice().getBondState());
return;
}
DexShareCollectionService.this.device = bluetoothGatt.getDevice();
DexShareCollectionService dexShareCollectionService3 = DexShareCollectionService.this;
dexShareCollectionService3.state_authInProgress = true;
dexShareCollectionService3.bondDevice();
}
@Override
public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
UserError.Log.d(DexShareCollectionService.TAG, "characteristic wrote " + i);
if (i == 0) {
UserError.Log.d(DexShareCollectionService.TAG, "Wrote a characteristic successfully " + bluetoothGattCharacteristic.getUuid());
if (DexShareCollectionService.this.mAuthenticationCharacteristic.getUuid().equals(bluetoothGattCharacteristic.getUuid())) {
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
dexShareCollectionService.state_authSucess = true;
bluetoothGatt.readCharacteristic(dexShareCollectionService.mHeartBeatCharacteristic);
return;
}
return;
}
if ((i & 5) == 0 && (i & 15) == 0) {
UserError.Log.e(DexShareCollectionService.TAG, "Unknown error writing Characteristic");
return;
}
if (bluetoothGatt.getDevice().getBondState() != 10) {
UserError.Log.e(DexShareCollectionService.TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: oncharacteristicwrite code: " + i + "bond: " + bluetoothGatt.getDevice().getBondState());
return;
}
DexShareCollectionService.this.device = bluetoothGatt.getDevice();
DexShareCollectionService dexShareCollectionService2 = DexShareCollectionService.this;
dexShareCollectionService2.state_authInProgress = true;
dexShareCollectionService2.bondDevice();
}
};
static int access$1212(DexShareCollectionService dexShareCollectionService, int i) {
int i2 = dexShareCollectionService.heartbeatCount + i;
dexShareCollectionService.heartbeatCount = i2;
return i2;
}
static int access$208() {
int i = statusErrors;
statusErrors = i + 1;
return i;
}
@Override
public void onCreate() {
super.onCreate();
this.readData = new ReadDataShare(this);
this.service = this;
ForegroundServiceStarter foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), this.service);
this.foregroundServiceStarter = foregroundServiceStarter;
foregroundServiceStarter.start();
registerReceiver(this.mPairReceiver, new IntentFilter("android.bluetooth.device.action.BOND_STATE_CHANGED"));
this.prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
listenForChangeInSettings();
this.instance = JoH.ts();
}
@Override
public int onStartCommand(Intent intent, int i, int i2) {
PowerManager.WakeLock newWakeLock = ((PowerManager) getApplicationContext().getSystemService("power")).newWakeLock(1, "DexShareCollectionStart");
newWakeLock.acquire(40000L);
String str = TAG;
UserError.Log.d(str, "onStartCommand");
try {
if (shouldServiceRun(getApplicationContext())) {
setFailoverTimer();
if (Sensor.currentSensor() == null) {
setRetryTimer();
return 2;
}
UserError.Log.i(str, "STARTING SERVICE");
attemptConnection();
if (newWakeLock.isHeld()) {
newWakeLock.release();
}
return 1;
}
stopSelf();
if (newWakeLock.isHeld()) {
newWakeLock.release();
}
return 2;
} finally {
if (newWakeLock.isHeld()) {
newWakeLock.release();
}
}
}
private static boolean shouldServiceRun(Context context) {
boolean z = CollectionServiceStarter.isBTShare(context) && !Home.get_forced_wear();
UserError.Log.d(TAG, "shouldServiceRun() returning: " + z);
return z;
}
@Override
public void onDestroy() {
super.onDestroy();
close();
if (shouldServiceRun(getApplicationContext())) {
setRetryTimer();
} else if (pendingIntent != null) {
UserError.Log.d(TAG, "onDestroy stop Alarm serviceIntent");
((AlarmManager) getSystemService("alarm")).cancel(pendingIntent);
}
this.foregroundServiceStarter.stop();
unregisterReceiver(this.mPairReceiver);
UserError.Log.i(TAG, "SERVICE STOPPED");
}
public void listenForChangeInSettings() {
this.prefs.registerOnSharedPreferenceChangeListener(this.prefListener);
}
public void setRetryTimer() {
if (CollectionServiceStarter.isBTShare(getApplicationContext())) {
BgReading last = BgReading.last();
long min = last != null ? Math.min(Math.max(30000L, (300000 - (new Date().getTime() - last.timestamp)) + 5000), 300000L) : 20000L;
UserError.Log.d(TAG, "Restarting in: " + (min / 60000) + " minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService("alarm");
PendingIntent pendingIntent2 = pendingIntent;
if (pendingIntent2 != null) {
alarmManager.cancel(pendingIntent2);
}
long timeInMillis = calendar.getTimeInMillis() + min;
pendingIntent = PendingIntent.getService(this, 0, new Intent(this, getClass()), 134217728);
alarmManager.setExactAndAllowWhileIdle(0, timeInMillis, pendingIntent);
}
}
public void setFailoverTimer() {
if (CollectionServiceStarter.isBTShare(getApplicationContext())) {
UserError.Log.d(TAG, "Fallover Restarting in: 5 minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService("alarm");
PendingIntent pendingIntent2 = pendingIntent;
if (pendingIntent2 != null) {
alarmManager.cancel(pendingIntent2);
}
long timeInMillis = calendar.getTimeInMillis() + 300000;
pendingIntent = PendingIntent.getService(this, 0, new Intent(this, getClass()), 0);
alarmManager.setExactAndAllowWhileIdle(0, timeInMillis, pendingIntent);
return;
}
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public void attemptConnection() {
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService("bluetooth");
this.mBluetoothManager = bluetoothManager;
if (bluetoothManager != null) {
if (this.device != null) {
this.mConnectionState = 0;
Iterator<BluetoothDevice> it = bluetoothManager.getConnectedDevices(7).iterator();
while (it.hasNext()) {
if (it.next().getAddress().compareTo(this.device.getAddress()) == 0) {
this.mConnectionState = 2;
}
}
}
String str = TAG;
UserError.Log.i(str, "Connection state: " + this.mConnectionState);
int i = this.mConnectionState;
if (i != 0 && i != 3) {
if (i == 2) {
UserError.Log.i(str, "Looks like we are already connected, going to read!");
attemptRead();
return;
} else {
setRetryTimer();
return;
}
}
ActiveBluetoothDevice first = ActiveBluetoothDevice.first();
if (first != null) {
this.mDeviceName = first.name;
this.mDeviceAddress = first.address;
BluetoothAdapter adapter = this.mBluetoothManager.getAdapter();
this.mBluetoothAdapter = adapter;
try {
if (adapter.isEnabled() && this.mBluetoothAdapter.getRemoteDevice(this.mDeviceAddress) != null) {
connect(this.mDeviceAddress);
return;
} else {
UserError.Log.w(str, "Bluetooth is disabled or BT device cant be found");
setRetryTimer();
return;
}
} catch (IllegalArgumentException e) {
if (JoH.ratelimit("dex-share-error-log", 180)) {
UserError.Log.wtf(TAG, "Error connecting: " + e);
return;
}
return;
}
}
UserError.Log.w(str, "No bluetooth device to try and connect to");
setRetryTimer();
return;
}
setRetryTimer();
}
public void requestHighPriority() {
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt != null) {
bluetoothGatt.requestConnectionPriority(1);
}
}
public void requestLowPriority() {
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt != null) {
bluetoothGatt.requestConnectionPriority(2);
}
}
public void attemptRead() {
PowerManager.WakeLock newWakeLock = ((PowerManager) getApplicationContext().getSystemService("power")).newWakeLock(1, "ReadingShareData");
newWakeLock.acquire(60000L);
requestHighPriority();
UserError.Log.d(TAG, "Attempting to read data");
this.readData.readSystemTime(new AnonymousClass2(newWakeLock));
}
class AnonymousClass2 implements Action1<Long> {
final PowerManager.WakeLock val$wakeLock1;
AnonymousClass2(PowerManager.WakeLock wakeLock) {
this.val$wakeLock1 = wakeLock;
}
public void call(Long l) {
if (l != null) {
UserError.Log.d(DexShareCollectionService.TAG, "Made the full round trip, got " + l + " as the system time");
final long time = new Date().getTime() - l.longValue();
DexShareCollectionService.this.readData.readDisplayTimeOffset(new Action1<Long>() {
public void call(Long l2) {
if (l2 != null) {
UserError.Log.d(DexShareCollectionService.TAG, "Made the full round trip, got " + l2 + " as the display time offset");
final long longValue = time - (l2.longValue() * 1000);
UserError.Log.d(DexShareCollectionService.TAG, "Making " + longValue + " the the total time offset");
final Action1<EGVRecord[]> action1 = new Action1<EGVRecord[]>() {
public void call(EGVRecord[] eGVRecordArr) {
if (eGVRecordArr != null) {
UserError.Log.d(DexShareCollectionService.TAG, "Made the full round trip, got " + eGVRecordArr.length + " EVG Records");
AnonymousClass1 anonymousClass1 = AnonymousClass1.this;
BgReading.create(eGVRecordArr, time, DexShareCollectionService.this.getApplicationContext());
int unused = DexShareCollectionService.statusErrors = 0;
UserError.Log.d(DexShareCollectionService.TAG, "Releasing wl in egv");
DexShareCollectionService.this.requestLowPriority();
PowerManager.WakeLock wakeLock = AnonymousClass2.this.val$wakeLock1;
if (wakeLock != null && wakeLock.isHeld()) {
AnonymousClass2.this.val$wakeLock1.release();
}
UserError.Log.d(DexShareCollectionService.TAG, "released");
DexShareCollectionService dexShareCollectionService = DexShareCollectionService.this;
if (dexShareCollectionService.shouldDisconnect) {
dexShareCollectionService.stopSelf();
} else {
dexShareCollectionService.setRetryTimer();
}
}
}
};
final Action1<SensorRecord[]> action12 = new Action1<SensorRecord[]>() {
public void call(SensorRecord[] sensorRecordArr) {
if (sensorRecordArr != null) {
UserError.Log.d(DexShareCollectionService.TAG, "Made the full round trip, got " + sensorRecordArr.length + " Sensor Records");
AnonymousClass1 anonymousClass1 = AnonymousClass1.this;
BgReading.create(sensorRecordArr, time, DexShareCollectionService.this.getApplicationContext());
int unused = DexShareCollectionService.statusErrors = 0;
DexShareCollectionService.this.readData.getRecentEGVs(action1);
}
}
};
DexShareCollectionService.this.readData.getRecentCalRecords(new Action1<CalRecord[]>() {
public void call(CalRecord[] calRecordArr) {
if (calRecordArr != null) {
UserError.Log.d(DexShareCollectionService.TAG, "Made the full round trip, got " + calRecordArr.length + " Cal Records");
Calibration.create(calRecordArr, longValue, DexShareCollectionService.this.getApplicationContext());
int unused = DexShareCollectionService.statusErrors = 0;
DexShareCollectionService.this.readData.getRecentSensorRecords(action12);
}
}
});
return;
}
PowerManager.WakeLock wakeLock = AnonymousClass2.this.val$wakeLock1;
if (wakeLock == null || !wakeLock.isHeld()) {
return;
}
AnonymousClass2.this.val$wakeLock1.release();
}
});
return;
}
PowerManager.WakeLock wakeLock = this.val$wakeLock1;
if (wakeLock == null || !wakeLock.isHeld()) {
return;
}
this.val$wakeLock1.release();
}
}
public synchronized boolean connect(String str) {
((PowerManager) getApplicationContext().getSystemService("power")).newWakeLock(1, "DexShareCollectionStart").acquire(30000L);
String str2 = TAG;
UserError.Log.i(str2, "going to connect to device at address" + str);
if (this.mBluetoothAdapter != null && str != null) {
if (this.mBluetoothGatt != null) {
UserError.Log.i(str2, "BGatt isnt null, Closing.");
try {
this.mBluetoothGatt.close();
} catch (NullPointerException unused) {
UserError.Log.d(TAG, "concurrency related null pointer exception in connect");
}
this.mBluetoothGatt = null;
}
for (BluetoothDevice bluetoothDevice : this.mBluetoothAdapter.getBondedDevices()) {
if (bluetoothDevice.getAddress().compareTo(str) == 0) {
UserError.Log.v(TAG, "Device found, already bonded, going to connect");
if (this.mBluetoothAdapter.getRemoteDevice(bluetoothDevice.getAddress()) != null) {
this.device = bluetoothDevice;
this.mBluetoothGatt = bluetoothDevice.connectGatt(getApplicationContext(), false, this.mGattCallback);
return true;
}
}
}
BluetoothDevice remoteDevice = this.mBluetoothAdapter.getRemoteDevice(str);
this.device = remoteDevice;
if (remoteDevice == null) {
UserError.Log.w(TAG, "Device not found. Unable to connect.");
setRetryTimer();
return false;
}
UserError.Log.i(TAG, "Trying to create a new connection.");
this.mBluetoothGatt = this.device.connectGatt(getApplicationContext(), false, this.mGattCallback);
this.mConnectionState = 1;
return true;
}
UserError.Log.w(str2, "BluetoothAdapter not initialized or unspecified address.");
setRetryTimer();
return false;
}
public void authenticateConnection() {
String str = TAG;
UserError.Log.i(str, "Trying to auth");
String str2 = this.prefs.getString("share_key", "SM00000000").toUpperCase() + "000000";
if (str2.compareTo("SM00000000000000") == 0) {
setRetryTimer();
return;
}
byte[] bytes = str2.getBytes(StandardCharsets.US_ASCII);
if (this.mBluetoothGatt != null) {
BluetoothGattService bluetoothGattService = this.mShareService;
if (bluetoothGattService != null) {
if (!this.share2) {
this.mAuthenticationCharacteristic = bluetoothGattService.getCharacteristic(DexShareAttributes.AuthenticationCode);
} else {
this.mAuthenticationCharacteristic = bluetoothGattService.getCharacteristic(DexShareAttributes.AuthenticationCode2);
}
if (this.mAuthenticationCharacteristic != null) {
UserError.Log.v(str, "Auth Characteristic found: " + this.mAuthenticationCharacteristic.toString());
if (this.mAuthenticationCharacteristic.setValue(bytes)) {
this.mBluetoothGatt.writeCharacteristic(this.mAuthenticationCharacteristic);
return;
} else {
setRetryTimer();
return;
}
}
UserError.Log.w(str, "Authentication Characteristic IS NULL");
setRetryTimer();
return;
}
UserError.Log.w(str, "CRADLE SERVICE IS NULL");
return;
}
setRetryTimer();
}
public void assignCharacteristics() {
if (!this.share2) {
UserError.Log.d(TAG, "Setting #1 characteristics");
this.mSendDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageReceiver);
this.mReceiveDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageResponse);
this.mCommandCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Command);
this.mResponseCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Response);
this.mHeartBeatCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.HeartBeat);
return;
}
UserError.Log.d(TAG, "Setting #1 characteristics");
this.mSendDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageReceiver2);
this.mReceiveDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageResponse2);
this.mCommandCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Command2);
this.mResponseCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Response2);
this.mHeartBeatCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.HeartBeat2);
}
public void setListeners(int i) {
UserError.Log.i(TAG, "Setting Listener: #" + i);
if (i == 1) {
this.step = 2;
setCharacteristicIndication(this.mReceiveDataCharacteristic);
} else {
this.step = 3;
attemptRead();
}
}
public void close() {
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt == null) {
return;
}
try {
bluetoothGatt.close();
} catch (NullPointerException unused) {
UserError.Log.d(TAG, "concurrency related null pointer exception in close");
}
setRetryTimer();
this.mBluetoothGatt = null;
this.mConnectionState = 0;
UserError.Log.i(TAG, "bt Disconnected");
}
public void setCharacteristicNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
setCharacteristicNotification(bluetoothGattCharacteristic, true);
}
public void setCharacteristicNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic, boolean z) {
String str = TAG;
UserError.Log.i(str, "Characteristic setting notification");
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt != null) {
bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, z);
BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
UserError.Log.i(str, "Descriptor found: " + descriptor.getUuid());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
this.mBluetoothGatt.writeDescriptor(descriptor);
}
}
public void setCharacteristicIndication(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
setCharacteristicIndication(bluetoothGattCharacteristic, true);
}
public void setCharacteristicIndication(BluetoothGattCharacteristic bluetoothGattCharacteristic, boolean z) {
String str = TAG;
UserError.Log.i(str, "Characteristic setting indication");
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt != null) {
bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, z);
BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
UserError.Log.i(str, "Descriptor found: " + descriptor.getUuid());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
this.mBluetoothGatt.writeDescriptor(descriptor);
}
}
public void writeCommand(List<byte[]> list, int i, Action1<byte[]> action1) {
this.mDataResponseListener = action1;
this.successfulWrites = 0;
this.writePackets = list;
this.recordType = i;
this.step = 0;
this.currentGattTask = 2;
gattWritingStep();
}
public void clearGattTask() {
this.currentGattTask = 0;
this.step = 0;
}
public void gattSetupStep() {
this.step = 1;
if (this.share2) {
assignCharacteristics();
}
setListeners(1);
}
private void gattWritingStep() {
List<byte[]> list;
String str = TAG;
UserError.Log.d(str, "Writing command to the Gatt, step: " + this.step);
int i = this.step;
if (i <= this.writePackets.size() - 1) {
UserError.Log.d(str, "Writing: " + this.writePackets.get(i) + " index: " + i);
BluetoothGattCharacteristic bluetoothGattCharacteristic = this.mSendDataCharacteristic;
if (bluetoothGattCharacteristic == null || (list = this.writePackets) == null) {
return;
}
bluetoothGattCharacteristic.setValue(list.get(i));
BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
if (bluetoothGatt == null || !bluetoothGatt.writeCharacteristic(this.mSendDataCharacteristic)) {
return;
}
UserError.Log.d(str, "Wrote Successfully");
return;
}
UserError.Log.d(str, "Done Writing commands");
clearGattTask();
}
public void bondDevice() {
registerReceiver(this.mPairReceiver, new IntentFilter("android.bluetooth.device.action.BOND_STATE_CHANGED"));
if (!this.share2) {
this.device.setPin("000000".getBytes());
}
this.device.createBond();
}
}