Search in sources :

Example 11 with ActiveBluetoothDevice

use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip-plus by jamorham.

the class Amazfitservice method getCurrentDevice.

// TODO what does this do? Use DexCollectionType and make as unified as possible
public String getCurrentDevice() {
    activeBluetoothDevice = ActiveBluetoothDevice.first();
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    String currentdevice;
    if (activeBluetoothDevice != null) {
        currentdevice = activeBluetoothDevice.name;
    } else {
        currentdevice = "None Set";
    }
    String collection_method = prefs.getString("dex_collection_method", "BluetoothWixel");
    if (collection_method.compareTo("DexcomG5") == 0) {
        Transmitter defaultTransmitter = new Transmitter(prefs.getString("dex_txid", "ABCDEF"));
        if (Build.VERSION.SDK_INT >= 18) {
            mBluetoothAdapter = mBluetoothManager.getAdapter();
        }
        if (mBluetoothAdapter != null) {
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if ((pairedDevices != null) && (pairedDevices.size() > 0)) {
                for (BluetoothDevice device : pairedDevices) {
                    if (device.getName() != null) {
                        String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId);
                        String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName());
                        if (transmitterIdLastTwo.equals(deviceNameLastTwo)) {
                            currentdevice = defaultTransmitter.transmitterId;
                        }
                    }
                }
            }
        } else {
            currentdevice = "No Bluetooth";
        }
    }
    return currentdevice;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) Transmitter(com.eveningoutpost.dexdrip.G5Model.Transmitter)

Example 12 with ActiveBluetoothDevice

use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip-plus by jamorham.

the class DexCollectionService method checkConnection.

synchronized void checkConnection() {
    status("Attempting connection");
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null) {
        status("No bluetooth manager");
        setRetryTimer();
        return;
    }
    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        status("No bluetooth adapter");
        setRetryTimer();
        return;
    }
    if (!mBluetoothAdapter.isEnabled()) {
        // can't be connected if BT is disabled
        mConnectionState = STATE_DISCONNECTED;
        if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
            Log.i(TAG, "Turning bluetooth on as appears disabled");
            status("Turning bluetooth on");
            JoH.setBluetoothEnabled(getApplicationContext(), true);
        } else {
            Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
        }
    }
    if (device != null) {
        boolean found = false;
        for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
            if (bluetoothDevice.getAddress().equals(device.getAddress())) {
                found = true;
                if (mConnectionState != STATE_CONNECTED) {
                    UserError.Log.d(TAG, "Detected state change by checking connected devices");
                    handleConnectedStateChange();
                }
                break;
            }
        }
        if (!found) {
            if (mConnectionState == STATE_CONNECTED) {
                UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
                // not in connected list so should be disconnected we think
                mConnectionState = STATE_DISCONNECTED;
            }
        }
    } else {
        UserError.Log.d(TAG, "Device is null in checkConnection");
        // can't be connected if we don't know the device
        mConnectionState = STATE_DISCONNECTED;
    }
    Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
    if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
        final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            final String deviceAddress = btDevice.address;
            mDeviceAddress = deviceAddress;
            try {
                if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
                    if (useScanning()) {
                        status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
                    } else {
                        status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
                        connect(deviceAddress);
                    }
                    mStaticState = mConnectionState;
                    return;
                }
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e);
            }
        }
    } else if (mConnectionState == STATE_CONNECTING) {
        mStaticState = mConnectionState;
        if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
            Log.i(TAG, "Connecting for too long, shutting down");
            retry_backoff = 0;
            close();
        }
    } else if (mConnectionState == STATE_CONNECTED) {
        // WOOO, we are good to go, nothing to do here!
        status("Last Connected");
        Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
        retry_backoff = 0;
        mStaticState = mConnectionState;
        if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
            pollForData();
        }
        return;
    }
    setRetryTimer();
}
Also used : ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) BluetoothDevice(android.bluetooth.BluetoothDevice) ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) BluetoothManager(android.bluetooth.BluetoothManager)

Example 13 with ActiveBluetoothDevice

use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip-plus by jamorham.

the class ListenerService method syncActiveBtDeviceData.

private void syncActiveBtDeviceData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncActiveBtDeviceData");
    if (dataMap != null) {
        String name = dataMap.getString("name", "");
        String address = dataMap.getString("address", "");
        Boolean connected = dataMap.getBoolean("connected", false);
        Log.d(TAG, "syncActiveBtDeviceData add ActiveBluetoothDevice for name=" + name + " address=" + address + " connected=" + connected);
        // ensure database has already been initialized
        Sensor.InitDb(context);
        if (name != null && !name.isEmpty() && address != null && !address.isEmpty()) {
            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            synchronized (ActiveBluetoothDevice.table_lock) {
                ActiveBluetoothDevice btDevice = new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle();
                prefs.edit().putString("last_connected_device_address", address).apply();
                if (btDevice == null) {
                    ActiveBluetoothDevice newBtDevice = new ActiveBluetoothDevice();
                    newBtDevice.name = name;
                    newBtDevice.address = address;
                    newBtDevice.connected = connected;
                    newBtDevice.save();
                } else {
                    btDevice.name = name;
                    btDevice.address = address;
                    btDevice.connected = connected;
                    btDevice.save();
                }
            }
        }
    }
}
Also used : ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) SharedPreferences(android.content.SharedPreferences) Select(com.activeandroid.query.Select)

Example 14 with ActiveBluetoothDevice

use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.

the class WatchUpdaterService method sendActiveBtDeviceData.

private void sendActiveBtDeviceData() {
    // KS
    if (is_using_bt) {
        // only required for Collector running on watch
        forceGoogleApiConnect();
        ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
        if (btDevice != null) {
            if (wear_integration) {
                DataMap dataMap = new DataMap();
                Log.d(TAG, "sendActiveBtDeviceData name=" + btDevice.name + " address=" + btDevice.address + " connected=" + btDevice.connected);
                // MOST IMPORTANT LINE FOR TIMESTAMP
                dataMap.putLong("time", new Date().getTime());
                dataMap.putString("name", btDevice.name);
                dataMap.putString("address", btDevice.address);
                dataMap.putBoolean("connected", btDevice.connected);
                new SendToDataLayerThread(WEARABLE_ACTIVEBTDEVICE_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, dataMap);
            }
        }
    } else {
        Log.d(TAG, "Not sending activebluetoothdevice data as we are not using bt");
    }
}
Also used : ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap)

Example 15 with ActiveBluetoothDevice

use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.

the class ListenerService method sendCollectorStatus.

private void sendCollectorStatus(Context context, String path) {
    String msg;
    // long last_timestamp = 0;
    DataMap dataMap = new DataMap();
    switch(DexCollectionType.getDexCollectionType()) {
        case DexcomG5:
            if (DexCollectionType.getCollectorServiceClass() == G5CollectionService.class) {
                // msg, last_timestamp
                dataMap = G5CollectionService.getWatchStatus();
            } else {
                // msg, last_timestamp
                dataMap = Ob1G5CollectionService.getWatchStatus();
            }
            break;
        case // TODO getLastState() in non-G5 Services
        DexcomShare:
            BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
            ActiveBluetoothDevice activeBluetoothDevice = ActiveBluetoothDevice.first();
            boolean connected = false;
            if (mBluetoothManager != null && activeBluetoothDevice != null) {
                for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
                    if (bluetoothDevice.getAddress().compareTo(activeBluetoothDevice.address) == 0) {
                        connected = true;
                    }
                }
            }
            if (connected) {
                msg = "Connected on watch";
            } else {
                msg = "Not Connected";
            }
            dataMap.putString("lastState", msg);
            break;
        default:
            dataMap = DexCollectionService.getWatchStatus();
            break;
    }
    if (dataMap != null) {
        dataMap.putString("action_path", path);
    }
    // sendReplyMsg (msg, last_timestamp, path, false);
    sendData(WEARABLE_REPLYMSG_PATH, dataMap.toByteArray());
}
Also used : ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) BluetoothDevice(android.bluetooth.BluetoothDevice) ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) BluetoothManager(android.bluetooth.BluetoothManager) DataMap(com.google.android.gms.wearable.DataMap)

Aggregations

ActiveBluetoothDevice (com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice)28 BluetoothDevice (android.bluetooth.BluetoothDevice)18 Transmitter (com.eveningoutpost.dexdrip.G5Model.Transmitter)10 Select (com.activeandroid.query.Select)6 BluetoothManager (android.bluetooth.BluetoothManager)4 SharedPreferences (android.content.SharedPreferences)4 DataMap (com.google.android.gms.wearable.DataMap)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2