导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - ShareTest.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 ShareTest.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.eveningoutpost.dexdrip;

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.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.activeandroid.query.Select;
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.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.DexShareAttributes;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import rx.Observable;
import rx.functions.Action1;

public class ShareTest extends BaseActivity {
    private static final String TAG = "ShareTest";
    Button bondButton;
    Button button;
    Button closeButton;
    public int currentGattTask;
    TextView details;
    private BluetoothDevice device;
    private BluetoothGattCharacteristic mAuthenticationCharacteristic;
    private BluetoothAdapter mBluetoothAdapter;
    private String mBluetoothDeviceAddress;
    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;
    Button readButton;
    public int recordType;
    public int step;
    public int successfulWrites;
    public List<byte[]> writePackets;
    private boolean is_connected = false;
    private boolean reconnecting = false;
    private int mConnectionState = 0;
    private Context mContext = null;
    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;
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
            if (i2 != 2) {
                if (i2 == 0) {
                    ShareTest.this.mConnectionState = 0;
                    ActiveBluetoothDevice.disconnected();
                    UserError.Log.w(ShareTest.TAG, "Disconnected from GATT server.");
                    return;
                }
                return;
            }
            ShareTest.this.mBluetoothGatt = bluetoothGatt;
            ShareTest.this.mConnectionState = 2;
            ActiveBluetoothDevice.connected();
            UserError.Log.i(ShareTest.TAG, "Connected to GATT server.");
            UserError.Log.i(ShareTest.TAG, "Connection state: Bonded - " + ShareTest.this.device.getBondState());
            if (ShareTest.this.device.getBondState() != 12) {
                ShareTest.this.device.setPin("000000".getBytes());
                ShareTest.this.device.createBond();
            } else {
                ShareTest shareTest = ShareTest.this;
                shareTest.currentGattTask = 1;
                shareTest.mBluetoothGatt.discoverServices();
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
            if (i == 0) {
                UserError.Log.i(ShareTest.TAG, "Services Discovered: " + i);
                ShareTest.this.authenticateConnection(bluetoothGatt);
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            if (i == 0) {
                UserError.Log.i(ShareTest.TAG, "Characteristic Read");
                byte[] value = bluetoothGattCharacteristic.getValue();
                if (value != null) {
                    UserError.Log.i(ShareTest.TAG, "VALUE" + value);
                } else {
                    UserError.Log.w(ShareTest.TAG, "Characteristic was null");
                }
                ShareTest.this.nextGattStep();
                return;
            }
            UserError.Log.w(ShareTest.TAG, "Characteristic failed to read");
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            UserError.Log.i(ShareTest.TAG, "Characteristic changed");
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            UserError.Log.i(ShareTest.TAG, "Characteristic Update Received: " + uuid);
            if (uuid.compareTo(ShareTest.this.mResponseCharacteristic.getUuid()) == 0) {
                UserError.Log.i(ShareTest.TAG, "mResponseCharacteristic Update");
            }
            if (uuid.compareTo(ShareTest.this.mCommandCharacteristic.getUuid()) == 0) {
                UserError.Log.i(ShareTest.TAG, "mCommandCharacteristic Update");
            }
            if (uuid.compareTo(ShareTest.this.mHeartBeatCharacteristic.getUuid()) == 0) {
                UserError.Log.i(ShareTest.TAG, "mHeartBeatCharacteristic Update");
            }
            if (uuid.compareTo(ShareTest.this.mReceiveDataCharacteristic.getUuid()) == 0) {
                UserError.Log.i(ShareTest.TAG, "mReceiveDataCharacteristic Update");
                byte[] value = bluetoothGattCharacteristic.getValue();
                if (value != null) {
                    UserError.Log.i(ShareTest.TAG, "Characteristic: " + value);
                    UserError.Log.i(ShareTest.TAG, "Characteristic: " + value.toString());
                    UserError.Log.i(ShareTest.TAG, "Characteristic getstring: " + bluetoothGattCharacteristic.getStringValue(0));
                    UserError.Log.i(ShareTest.TAG, "SUBSCRIBED TO RESPONSE LISTENER");
                    Observable.just(bluetoothGattCharacteristic.getValue()).subscribe(ShareTest.this.mDataResponseListener);
                } else {
                    UserError.Log.w(ShareTest.TAG, "Characteristic was null");
                }
            }
            UserError.Log.i(ShareTest.TAG, "NEW VALUE: " + bluetoothGattCharacteristic.getValue().toString());
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
            UserError.Log.i(ShareTest.TAG, "Wrote a discriptor, status: " + i);
            ShareTest shareTest = ShareTest.this;
            int i2 = shareTest.step;
            if (i2 == 2 && shareTest.currentGattTask == 1) {
                shareTest.setListeners(2);
                return;
            }
            if (i2 == 3) {
                shareTest.setListeners(3);
            } else if (i2 == 4) {
                shareTest.setListeners(4);
            } else if (i2 == 5) {
                UserError.Log.i(ShareTest.TAG, "Done setting Listeners");
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            UserError.Log.i(ShareTest.TAG, "Wrote a characteristic: " + i);
            ShareTest.this.nextGattStep();
        }
    };
    private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("android.bluetooth.device.action.BOND_STATE_CHANGED".equals(intent.getAction())) {
                int intExtra = intent.getIntExtra("android.bluetooth.device.extra.BOND_STATE", RecyclerView.UNDEFINED_DURATION);
                intent.getIntExtra("android.bluetooth.device.extra.PREVIOUS_BOND_STATE", RecyclerView.UNDEFINED_DURATION);
                if (intExtra == 12) {
                    UserError.Log.d(ShareTest.TAG, "CALLBACK RECIEVED Bonded");
                    ShareTest shareTest = ShareTest.this;
                    shareTest.currentGattTask = 1;
                    shareTest.mBluetoothGatt.discoverServices();
                    return;
                }
                if (intExtra == 10) {
                    UserError.Log.d(ShareTest.TAG, "CALLBACK RECIEVED: Not Bonded");
                    Toast.makeText(ShareTest.this.getApplicationContext(), "unBonded", 1).show();
                } else if (intExtra == 11) {
                    UserError.Log.d(ShareTest.TAG, "CALLBACK RECIEVED: Trying to bond");
                    Toast.makeText(ShareTest.this.getApplicationContext(), "trying to bond", 1).show();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(2131492940);
        this.button = (Button) findViewById(2131296516);
        this.closeButton = (Button) findViewById(2131296509);
        this.bondButton = (Button) findViewById(2131296400);
        this.readButton = (Button) findViewById(2131296882);
        this.details = (TextView) findViewById(2131296517);
        addListenerOnButton();
        addListenerOnCloseButton();
        registerReceiver(this.mPairReceiver, new IntentFilter("android.bluetooth.device.action.BOND_STATE_CHANGED"));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        close();
        UserError.Log.i(TAG, "CLOSING CONNECTION");
    }

    public void addListenerOnButton() {
        this.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareTest.this.attemptConnection();
            }
        });
        this.readButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareTest.this.attemptRead();
            }
        });
        this.bondButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareTest shareTest = ShareTest.this;
                shareTest.bond(shareTest.mBluetoothGatt);
            }
        });
    }

    public void addListenerOnCloseButton() {
        this.closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareTest.this.close();
                ShareTest.this.details.setText("");
            }
        });
    }

    public void attemptConnection() {
        ActiveBluetoothDevice activeBluetoothDevice;
        this.mBluetoothManager = (BluetoothManager) getSystemService("bluetooth");
        if (this.device != null) {
            this.details.append("\nConnection state:  Device is not null");
            this.mConnectionState = this.mBluetoothManager.getConnectionState(this.device, 7);
        }
        UserError.Log.i(TAG, "Connection state: " + this.mConnectionState);
        this.details.append("\nConnection state: " + this.mConnectionState);
        int i = this.mConnectionState;
        if ((i == 0 || i == 3) && (activeBluetoothDevice = (ActiveBluetoothDevice) new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle()) != null) {
            this.details.append("\nBT Device: " + activeBluetoothDevice.name);
            this.mDeviceName = activeBluetoothDevice.name;
            this.mDeviceAddress = activeBluetoothDevice.address;
            this.mBluetoothAdapter = this.mBluetoothManager.getAdapter();
            this.is_connected = connect(this.mDeviceAddress);
            this.details.append("\nConnecting...: ");
        }
    }

    class AnonymousClass6 implements Action1<Long> {
        final ReadDataShare val$readData;

        AnonymousClass6(ReadDataShare readDataShare) {
            this.val$readData = readDataShare;
        }

        public void call(Long l) {
            UserError.Log.d(ShareTest.TAG, "Made the full round trip, got " + l + " as the system time");
            UserError.Log.d("SYSTTIME", "Made the full round trip, got " + l + " as the system time");
            long time = new Date().getTime() - l.longValue();
            UserError.Log.d(ShareTest.TAG, "Made the full round trip, got " + time + " offset");
            UserError.Log.d("SYSTTIME", "Made the full round trip, got " + time + " offset");
            this.val$readData.getRecentCalRecords(new AnonymousClass1(time));
        }

        class AnonymousClass1 implements Action1<CalRecord[]> {
            final long val$addativeSystemTimeOffset;

            AnonymousClass1(long j) {
                this.val$addativeSystemTimeOffset = j;
            }

            public void call(CalRecord[] calRecordArr) {
                UserError.Log.d(ShareTest.TAG, "Made the full round trip, got " + calRecordArr.length + " Cal Records");
                Calibration.create(calRecordArr, this.val$addativeSystemTimeOffset, ShareTest.this.getApplicationContext());
                AnonymousClass6.this.val$readData.getRecentSensorRecords(new Action1<SensorRecord[]>() {
                    public void call(SensorRecord[] sensorRecordArr) {
                        UserError.Log.d(ShareTest.TAG, "Made the full round trip, got " + sensorRecordArr.length + " Sensor Records");
                        AnonymousClass1 anonymousClass1 = AnonymousClass1.this;
                        BgReading.create(sensorRecordArr, anonymousClass1.val$addativeSystemTimeOffset, ShareTest.this.getApplicationContext());
                        AnonymousClass6.this.val$readData.getRecentEGVs(new Action1<EGVRecord[]>() {
                            public void call(EGVRecord[] eGVRecordArr) {
                                UserError.Log.d(ShareTest.TAG, "Made the full round trip, got " + eGVRecordArr.length + " EVG Records");
                                AnonymousClass1 anonymousClass12 = AnonymousClass1.this;
                                BgReading.create(eGVRecordArr, anonymousClass12.val$addativeSystemTimeOffset, ShareTest.this.getApplicationContext());
                            }
                        });
                    }
                });
            }
        }
    }

    public void attemptRead() {
        ReadDataShare readDataShare = new ReadDataShare(this);
        readDataShare.readSystemTime(new AnonymousClass6(readDataShare));
    }

    public void bond(BluetoothGatt bluetoothGatt) {
        this.reconnecting = true;
        attemptConnection();
    }

    public boolean connect(String str) {
        this.details.append("\nConnecting to device");
        String str2 = TAG;
        UserError.Log.i(str2, "CONNECTING TO DEVICE");
        if (this.mBluetoothAdapter == null || str == null) {
            this.details.append("\nBT adapter is null");
            UserError.Log.w(str2, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }
        String str3 = this.mBluetoothDeviceAddress;
        if (str3 != null && str.equals(str3) && this.mBluetoothGatt != null) {
            this.details.append("\nTrying to use an existing mBluetoothGatt for connection.");
            if (!this.mBluetoothGatt.connect()) {
                return false;
            }
            this.mConnectionState = 1;
            return true;
        }
        BluetoothDevice remoteDevice = this.mBluetoothAdapter.getRemoteDevice(str);
        this.device = remoteDevice;
        remoteDevice.setPin("000000".getBytes());
        BluetoothDevice bluetoothDevice = this.device;
        if (bluetoothDevice == null) {
            UserError.Log.w(str2, "Device not found.  Unable to connect.");
            this.details.append("\nDevice not found.  Unable to connect.");
            return false;
        }
        this.mBluetoothGatt = bluetoothDevice.connectGatt(getApplicationContext(), true, this.mGattCallback);
        UserError.Log.i(str2, "Trying to create a new connection.");
        this.details.append("\nTrying to create a new connection to device");
        this.mConnectionState = 1;
        return true;
    }

    public void authenticateConnection(BluetoothGatt bluetoothGatt) {
        String str = TAG;
        UserError.Log.i(str, "Trying to auth");
        String upperCase = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("share_key", "SM00000000").toUpperCase();
        if (bluetoothGatt != null) {
            this.mBluetoothGatt = bluetoothGatt;
            BluetoothGattService service = bluetoothGatt.getService(DexShareAttributes.CradleService);
            this.mShareService = service;
            if (service != null) {
                BluetoothGattCharacteristic characteristic = service.getCharacteristic(DexShareAttributes.AuthenticationCode);
                this.mAuthenticationCharacteristic = characteristic;
                if (characteristic != null) {
                    UserError.Log.i(str, "Auth Characteristic found: " + this.mAuthenticationCharacteristic.toString());
                    this.mAuthenticationCharacteristic.setValue((upperCase + "000000").getBytes(StandardCharsets.US_ASCII));
                    this.currentGattTask = 1;
                    this.step = 1;
                    bluetoothGatt.writeCharacteristic(this.mAuthenticationCharacteristic);
                    return;
                }
                UserError.Log.w(str, "Authentication Characteristic IS NULL");
                return;
            }
            UserError.Log.w(str, "CRADLE SERVICE IS NULL");
        }
    }

    public void assignCharacteristics() {
        this.mSendDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageReceiver);
        this.mReceiveDataCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.ShareMessageResponse);
        this.mHeartBeatCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.HeartBeat);
        this.mCommandCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Command);
        this.mResponseCharacteristic = this.mShareService.getCharacteristic(DexShareAttributes.Response);
    }

    public void setListeners(int i) {
        UserError.Log.i(TAG, "Setting Listener: #" + i);
        if (i == 1) {
            this.step = 3;
            setCharacteristicIndication(this.mReceiveDataCharacteristic);
        } else if (i == 3) {
            setCharacteristicIndication(this.mResponseCharacteristic);
            this.step = 5;
        }
    }

    public void disconnect() {
        BluetoothGatt bluetoothGatt;
        if (this.mBluetoothAdapter == null || (bluetoothGatt = this.mBluetoothGatt) == null) {
            UserError.Log.w(TAG, "BluetoothAdapter not initialized");
        } else {
            bluetoothGatt.disconnect();
        }
    }

    public void close() {
        disconnect();
        BluetoothGatt bluetoothGatt = this.mBluetoothGatt;
        if (bluetoothGatt == null) {
            return;
        }
        bluetoothGatt.close();
        this.mBluetoothGatt = null;
        this.mConnectionState = 0;
        UserError.Log.w(TAG, "bt Disconnected");
    }

    public void setCharacteristicIndication(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        setCharacteristicIndication(bluetoothGattCharacteristic, true);
    }

    public void setCharacteristicIndication(BluetoothGattCharacteristic bluetoothGattCharacteristic, boolean z) {
        String str = TAG;
        UserError.Log.i(str, "Characteristic setting notification");
        this.mBluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, z);
        UserError.Log.i(str, "UUID FOUND: " + bluetoothGattCharacteristic.getUuid());
        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 nextGattStep() {
        String str = TAG;
        UserError.Log.d(str, "Next Gatt Step");
        this.step++;
        int i = this.currentGattTask;
        if (i == 0) {
            UserError.Log.d(str, "Next NOTHING: " + this.step);
            return;
        }
        if (i == 1) {
            UserError.Log.d(str, "Next GATT SETUP: " + this.step);
            gattSetupStep();
            return;
        }
        if (i != 2) {
            return;
        }
        UserError.Log.d(str, "Next GATT WRITING: " + this.step);
        gattWritingStep();
    }

    public void clearGattTask() {
        this.currentGattTask = 0;
        this.step = 0;
    }

    private void gattSetupStep() {
        this.step = 1;
        assignCharacteristics();
        setListeners(1);
    }

    private void gattWritingStep() {
        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);
            this.mSendDataCharacteristic.setValue(this.writePackets.get(i));
            this.mBluetoothGatt.writeCharacteristic(this.mSendDataCharacteristic);
            return;
        }
        clearGattTask();
    }
}