导航菜单

页面标题

页面副标题

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

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

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


package com.eveningoutpost.dexdrip;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.cgm.medtrum.Medtrum;
import com.eveningoutpost.dexdrip.models.ActiveBluetoothDevice;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.Blukon;
import com.eveningoutpost.dexdrip.utilitymodels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.utilitymodels.Inevitable;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import com.eveningoutpost.dexdrip.utils.AndroidBarcode;
import com.eveningoutpost.dexdrip.utils.ListActivityWithMenu;
import com.eveningoutpost.dexdrip.utils.LocationHelper;
import com.eveningoutpost.dexdrip.wearintegration.WatchUpdaterService;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lecho.lib.hellocharts.util.ChartUtils;

@TargetApi(18)
public class BluetoothScan extends ListActivityWithMenu {
    private static final String TAG = "BluetoothScan";
    private BluetoothAdapter bluetooth_adapter;
    private boolean is_scanning;
    private BluetoothLeScanner lollipopScanner;
    private Handler mHandler;
    private LeDeviceListAdapter mLeDeviceListAdapter;
    private ScanCallback mScanCallback;
    private Map<String, byte[]> adverts = new HashMap();
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice bluetoothDevice, int i, final byte[] bArr) {
            BluetoothScan.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (bluetoothDevice.getName() == null || bluetoothDevice.getName().length() <= 0) {
                        return;
                    }
                    BluetoothScan.this.mLeDeviceListAdapter.addDevice(bluetoothDevice);
                    if (bArr != null) {
                        BluetoothScan.this.adverts.put(bluetoothDevice.getAddress(), bArr);
                    }
                    BluetoothScan.this.mLeDeviceListAdapter.notifyDataSetChanged();
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle bundle) {
        setTheme(2131820787);
        super.onCreate(bundle);
        setContentView(2131492899);
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService("bluetooth");
        this.bluetooth_adapter = bluetoothManager.getAdapter();
        this.mHandler = new Handler();
        if (this.bluetooth_adapter == null) {
            Toast.makeText(this, 2131755535, 1).show();
            finish();
            return;
        }
        if (!bluetoothManager.getAdapter().isEnabled()) {
            if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
                JoH.setBluetoothEnabled(getApplicationContext(), true);
                Toast.makeText(this, "Trying to turn Bluetooth on", 1).show();
            } else {
                Toast.makeText(this, "Please turn Bluetooth on!", 1).show();
            }
        }
        LocationHelper.requestLocationForBluetooth(this);
        initializeScannerCallback();
        LeDeviceListAdapter leDeviceListAdapter = new LeDeviceListAdapter();
        this.mLeDeviceListAdapter = leDeviceListAdapter;
        setListAdapter(leDeviceListAdapter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        scanLeDevice(false);
        this.mLeDeviceListAdapter.clear();
        this.mLeDeviceListAdapter.notifyDataSetChanged();
    }

    @Override
    public String getMenuName() {
        return getString(2131755234);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(2131558403, menu);
        if (!this.is_scanning) {
            menu.findItem(2131296741).setVisible(false);
            menu.findItem(2131296739).setVisible(true);
        } else {
            menu.findItem(2131296741).setVisible(true);
            menu.findItem(2131296739).setVisible(false);
        }
        return true;
    }

    private boolean doScan() {
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService("bluetooth");
        Toast.makeText(this, xdrip.gs(2131756235), 1).show();
        if (bluetoothManager == null) {
            Toast.makeText(this, "This device does not seem to support bluetooth", 1).show();
            return true;
        }
        if (!bluetoothManager.getAdapter().isEnabled()) {
            Toast.makeText(this, "Bluetooth is turned off on this device currently", 1).show();
            return true;
        }
        scanLeDeviceLollipop(true);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (LocationHelper.isLocationPermissionOk(this) && JoH.ratelimit("auto-start-bt-scan", 20)) {
            doScan();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == 2131296739) {
            return doScan();
        }
        return super.onOptionsItemSelected(menuItem);
    }

    @TargetApi(19)
    private synchronized void scanLeDevice(boolean z) {
        if (z) {
            UserError.Log.d(TAG, "Start scan 19");
            this.mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    BluetoothScan.this.is_scanning = false;
                    try {
                        if (BluetoothScan.this.bluetooth_adapter != null && BluetoothScan.this.mLeScanCallback != null) {
                            BluetoothScan.this.bluetooth_adapter.stopLeScan(BluetoothScan.this.mLeScanCallback);
                        }
                    } catch (NullPointerException unused) {
                    }
                    BluetoothScan.this.invalidateOptionsMenu();
                }
            }, 30000L);
            this.is_scanning = true;
            BluetoothAdapter bluetoothAdapter = this.bluetooth_adapter;
            if (bluetoothAdapter != null) {
                bluetoothAdapter.startLeScan(this.mLeScanCallback);
            }
        } else {
            this.is_scanning = false;
            BluetoothAdapter bluetoothAdapter2 = this.bluetooth_adapter;
            if (bluetoothAdapter2 != null && bluetoothAdapter2.isEnabled()) {
                try {
                    this.bluetooth_adapter.stopLeScan(this.mLeScanCallback);
                } catch (NullPointerException unused) {
                }
            }
        }
        invalidateOptionsMenu();
    }

    @TargetApi(21)
    private void initializeScannerCallback() {
        UserError.Log.d(TAG, "initializeScannerCallback");
        this.mScanCallback = new ScanCallback() {
            @Override
            public void onBatchScanResults(final List<ScanResult> list) {
                BluetoothScan.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (ScanResult scanResult : list) {
                            BluetoothDevice device = scanResult.getDevice();
                            if (device.getName() != null && device.getName().length() > 0) {
                                BluetoothScan.this.mLeDeviceListAdapter.addDevice(device);
                                try {
                                    if (scanResult.getScanRecord() != null) {
                                        BluetoothScan.this.adverts.put(device.getAddress(), scanResult.getScanRecord().getBytes());
                                    }
                                } catch (NullPointerException unused) {
                                }
                            }
                        }
                        BluetoothScan.this.mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onScanResult(int i, final ScanResult scanResult) {
                final BluetoothDevice device = scanResult.getDevice();
                BluetoothScan.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String name = device.getName();
                        if (name == null || name.length() <= 0) {
                            return;
                        }
                        BluetoothScan.this.mLeDeviceListAdapter.addDevice(device);
                        try {
                            if (scanResult.getScanRecord() != null) {
                                BluetoothScan.this.adverts.put(device.getAddress(), scanResult.getScanRecord().getBytes());
                            }
                        } catch (NullPointerException unused) {
                        }
                        BluetoothScan.this.mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
    }

    @TargetApi(21)
    private synchronized void scanLeDeviceLollipop(boolean z) {
        if (z) {
            BluetoothLeScanner bluetoothLeScanner = this.bluetooth_adapter.getBluetoothLeScanner();
            this.lollipopScanner = bluetoothLeScanner;
            if (bluetoothLeScanner != null) {
                UserError.Log.d(TAG, "Starting scanner 21");
                this.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        BluetoothScan.this.is_scanning = false;
                        if (BluetoothScan.this.bluetooth_adapter != null && BluetoothScan.this.bluetooth_adapter.isEnabled()) {
                            try {
                                BluetoothScan.this.lollipopScanner.stopScan(BluetoothScan.this.mScanCallback);
                            } catch (IllegalStateException e) {
                                JoH.static_toast_long(e.toString());
                                UserError.Log.e(BluetoothScan.TAG, "error stopping scan: " + e.toString());
                            }
                        }
                        BluetoothScan.this.invalidateOptionsMenu();
                    }
                }, 30000L);
                ScanSettings build = new ScanSettings.Builder().setScanMode(2).build();
                this.is_scanning = true;
                BluetoothAdapter bluetoothAdapter = this.bluetooth_adapter;
                if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
                    this.lollipopScanner.startScan((List<ScanFilter>) null, build, this.mScanCallback);
                }
            } else {
                try {
                    scanLeDevice(true);
                } catch (Exception e) {
                    UserError.Log.e(TAG, "Failed to scan for ble device", e);
                }
            }
        } else {
            this.is_scanning = false;
            BluetoothAdapter bluetoothAdapter2 = this.bluetooth_adapter;
            if (bluetoothAdapter2 != null && bluetoothAdapter2.isEnabled()) {
                this.lollipopScanner.stopScan(this.mScanCallback);
            }
        }
        invalidateOptionsMenu();
    }

    @Override
    public void onActivityResult(int i, int i2, Intent intent) {
        IntentResult parseActivityResult = IntentIntegrator.parseActivityResult(i, i2, intent);
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        if (parseActivityResult == null || parseActivityResult.getContents() == null || !parseActivityResult.getFormatName().equals("CODE_128")) {
            return;
        }
        UserError.Log.d(TAG, "Setting serial number to: " + parseActivityResult.getContents());
        defaultSharedPreferences.edit().putString("share_key", parseActivityResult.getContents()).apply();
        returnToHome();
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int i, long j) {
        boolean z;
        String str = TAG;
        UserError.Log.d(str, "Item Clicked");
        BluetoothDevice device = this.mLeDeviceListAdapter.getDevice(i);
        if (device == null || device.getName() == null) {
            return;
        }
        boolean z2 = true;
        Toast.makeText(this, 2131755374, 1).show();
        final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        synchronized (ActiveBluetoothDevice.table_lock) {
            ActiveBluetoothDevice activeBluetoothDevice = (ActiveBluetoothDevice) new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle();
            defaultSharedPreferences.edit().putString("last_connected_device_address", device.getAddress()).apply();
            Blukon.clearPin();
            if (activeBluetoothDevice == null) {
                ActiveBluetoothDevice activeBluetoothDevice2 = new ActiveBluetoothDevice();
                activeBluetoothDevice2.name = device.getName();
                activeBluetoothDevice2.address = device.getAddress();
                activeBluetoothDevice2.save();
            } else {
                activeBluetoothDevice.name = device.getName();
                activeBluetoothDevice.address = device.getAddress();
                activeBluetoothDevice.save();
            }
            Home.startWatchUpdaterService(this, WatchUpdaterService.ACTION_SYNC_ACTIVEBTDEVICE, str);
        }
        try {
            if ((device.getName().toLowerCase().contains("limitter") && this.adverts.containsKey(device.getAddress()) && (new String(this.adverts.get(device.getAddress()), "UTF-8").contains("eLeR") || new String(this.adverts.get(device.getAddress()), "UTF-8").contains("data"))) || device.getName().toLowerCase().contains("limitterd")) {
                UserError.Log.e(str, "Auto-detected transmiter_pl device!");
                JoH.static_toast_long("Auto-detected transmiter_pl device!");
                z = true;
            } else {
                z = false;
            }
            defaultSharedPreferences.edit().putBoolean("use_transmiter_pl_bluetooth", z).apply();
            if (device.getName().toLowerCase().contains("xbridge") && this.adverts.containsKey(device.getAddress()) && new String(this.adverts.get(device.getAddress()), "UTF-8").contains("rfduino")) {
                UserError.Log.e(str, "Auto-detected rfduino device!");
                JoH.static_toast_long("Auto-detected rfduino device!");
            } else {
                z2 = false;
            }
            defaultSharedPreferences.edit().putBoolean("use_rfduino_bluetooth", z2).apply();
            if (device.getName().toLowerCase().contains("dexcom")) {
                if (!CollectionServiceStarter.isBTShare(getApplicationContext())) {
                    defaultSharedPreferences.edit().putString("dex_collection_method", "DexcomShare").apply();
                    defaultSharedPreferences.edit().putBoolean("calibration_notifications", false).apply();
                }
                if (defaultSharedPreferences.getString("share_key", "SM00000000").compareTo("SM00000000") != 0 && defaultSharedPreferences.getString("share_key", "SM00000000").length() >= 10) {
                    returnToHome();
                    return;
                }
                requestSerialNumber(defaultSharedPreferences);
                return;
            }
            if (device.getName().toLowerCase().contains("bridge")) {
                if (!CollectionServiceStarter.isDexBridgeOrWifiandDexBridge()) {
                    defaultSharedPreferences.edit().putString("dex_collection_method", "DexbridgeWixel").apply();
                }
                if (defaultSharedPreferences.getString("dex_txid", "00000").compareTo("00000") != 0 && defaultSharedPreferences.getString("dex_txid", "00000").length() >= 5) {
                    returnToHome();
                    return;
                }
                requestTransmitterId(defaultSharedPreferences);
                return;
            }
            if (device.getName().toLowerCase().contains("drip")) {
                if ((!CollectionServiceStarter.isBTWixelOrLimiTTer(getApplicationContext()) && !CollectionServiceStarter.isWifiandBTWixel(getApplicationContext())) || CollectionServiceStarter.isLimitter()) {
                    defaultSharedPreferences.edit().putString("dex_collection_method", "BluetoothWixel").apply();
                }
                returnToHome();
                return;
            }
            if (device.getName().toLowerCase().contains("limitter")) {
                if (!CollectionServiceStarter.isLimitter()) {
                    defaultSharedPreferences.edit().putString("dex_collection_method", "LimiTTer").apply();
                }
                returnToHome();
                return;
            }
            if (device.getName().toLowerCase().contains("bluereader")) {
                if (!CollectionServiceStarter.isLimitter()) {
                    defaultSharedPreferences.edit().putString("dex_collection_method", "LimiTTer").apply();
                }
                returnToHome();
                return;
            }
            if (!device.getName().toLowerCase().contains("miaomiao") && !device.getName().toLowerCase().startsWith("watlaa")) {
                if (device.getName().toLowerCase().contains("sweetreader")) {
                    if (!CollectionServiceStarter.isLimitter()) {
                        defaultSharedPreferences.edit().putString("dex_collection_method", "LimiTTer").apply();
                    }
                    returnToHome();
                    return;
                } else {
                    if (device.getName().matches("^BLU[0-9][0-9][0-9][0-9][0-9].*$")) {
                        Blukon.doPinDialog(this, new Runnable() {
                            @Override
                            public void run() {
                                if (!CollectionServiceStarter.isLimitter()) {
                                    defaultSharedPreferences.edit().putString("dex_collection_method", "LimiTTer").apply();
                                }
                                BluetoothScan.this.returnToHome();
                            }
                        });
                        return;
                    }
                    if (device.getName().matches("MT")) {
                        if (Medtrum.saveSerialFromLegacy(this.adverts.get(device.getAddress()))) {
                            JoH.static_toast_long("Set Medtrum serial number");
                            CollectionServiceStarter.restartCollectionServiceBackground();
                            returnToHome();
                            return;
                        }
                        JoH.static_toast_long("Failed to find Medtrum serial number");
                        return;
                    }
                    returnToHome();
                    return;
                }
            }
            if (!CollectionServiceStarter.isLimitter() && !CollectionServiceStarter.isWifiandBTLibre()) {
                defaultSharedPreferences.edit().putString("dex_collection_method", "LimiTTer").apply();
            }
            returnToHome();
        } catch (UnsupportedEncodingException | NullPointerException e) {
            UserError.Log.d(TAG, "Got exception in listitemclick: " + Arrays.toString(e.getStackTrace()));
        }
    }

    public void returnToHome() {
        try {
            if (this.is_scanning) {
                this.is_scanning = false;
                this.bluetooth_adapter.stopLeScan(this.mLeScanCallback);
            }
        } catch (NullPointerException unused) {
        }
        Inevitable.task("restart-collector", 2000L, new Runnable() {
            @Override
            public final void run() {
                BluetoothScan.this.lambda$returnToHome$0();
            }
        });
        startActivity(new Intent(this, (Class<?>) Home.class));
        finish();
    }

    public void lambda$returnToHome$0() {
        CollectionServiceStarter.restartCollectionService(getApplicationContext());
    }

    private void requestSerialNumber(final SharedPreferences sharedPreferences) {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(2131492978);
        Button button = (Button) dialog.findViewById(2131296923);
        Button button2 = (Button) dialog.findViewById(2131296479);
        final EditText editText = (EditText) dialog.findViewById(2131296564);
        editText.setHint("SM00000000");
        ((TextView) dialog.findViewById(2131296680)).setText("Enter Your Dexcom Receiver Serial Number");
        dialog.findViewById(2131296934).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AndroidBarcode(BluetoothScan.this).scan();
                dialog.dismiss();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(editText.getText())) {
                    sharedPreferences.edit().putString("share_key", editText.getText().toString()).apply();
                }
                dialog.dismiss();
                BluetoothScan.this.returnToHome();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void requestTransmitterId(final SharedPreferences sharedPreferences) {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(2131492978);
        Button button = (Button) dialog.findViewById(2131296923);
        Button button2 = (Button) dialog.findViewById(2131296479);
        dialog.findViewById(2131296934).setVisibility(8);
        final EditText editText = (EditText) dialog.findViewById(2131296564);
        editText.setHint("00000");
        editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        ((TextView) dialog.findViewById(2131296680)).setText("Enter Your Dexcom Transmitter ID");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(editText.getText())) {
                    sharedPreferences.edit().putString("dex_txid", editText.getText().toString()).apply();
                }
                dialog.dismiss();
                BluetoothScan.this.returnToHome();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    static class ViewHolder {
        TextView deviceAddress;
        TextView deviceName;

        ViewHolder() {
        }
    }

    private class LeDeviceListAdapter extends BaseAdapter {
        private LayoutInflater mInflator;
        private ArrayList<BluetoothDevice> mLeDevices = new ArrayList<>();

        @Override
        public long getItemId(int i) {
            return i;
        }

        LeDeviceListAdapter() {
            this.mInflator = BluetoothScan.this.getLayoutInflater();
        }

        void addDevice(BluetoothDevice bluetoothDevice) {
            if (this.mLeDevices.contains(bluetoothDevice)) {
                return;
            }
            this.mLeDevices.add(bluetoothDevice);
            notifyDataSetChanged();
        }

        public BluetoothDevice getDevice(int i) {
            return this.mLeDevices.get(i);
        }

        public void clear() {
            this.mLeDevices.clear();
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return this.mLeDevices.size();
        }

        @Override
        public Object getItem(int i) {
            return this.mLeDevices.get(i);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            String deviceInfoStringFromLegacy;
            if (view == null) {
                view = this.mInflator.inflate(2131492997, (ViewGroup) null);
                viewHolder = new ViewHolder();
                viewHolder.deviceAddress = (TextView) view.findViewById(2131296547);
                viewHolder.deviceName = (TextView) view.findViewById(2131296549);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            BluetoothDevice bluetoothDevice = this.mLeDevices.get(i);
            if (bluetoothDevice != null) {
                String name = bluetoothDevice.getName();
                if (name == null) {
                    name = "";
                }
                if (Pref.getString("last_connected_device_address", "").equalsIgnoreCase(bluetoothDevice.getAddress())) {
                    TextView textView = viewHolder.deviceName;
                    int i2 = ChartUtils.COLOR_BLUE;
                    textView.setTextColor(i2);
                    viewHolder.deviceAddress.setTextColor(i2);
                }
                viewHolder.deviceName.setText(name);
                viewHolder.deviceAddress.setText(bluetoothDevice.getAddress());
                if (BluetoothScan.this.adverts.containsKey(bluetoothDevice.getAddress())) {
                    if (name.equals("MT") && (deviceInfoStringFromLegacy = Medtrum.getDeviceInfoStringFromLegacy((byte[]) BluetoothScan.this.adverts.get(bluetoothDevice.getAddress()))) != null) {
                        viewHolder.deviceName.setText(deviceInfoStringFromLegacy);
                    }
                    try {
                        if (Pref.getBooleanDefaultFalse("engineering_mode")) {
                            viewHolder.deviceAddress.append("   " + new String((byte[]) BluetoothScan.this.adverts.get(bluetoothDevice.getAddress()), "UTF-8"));
                        }
                    } catch (UnsupportedEncodingException unused) {
                    }
                }
            }
            return view;
        }
    }
}