Search in sources :

Example 1 with BaseSensor

use of sample.ble.sensortag.sensor.BaseSensor in project BleSensorTag by StevenRudenko.

the class BleServicesAdapter method getChildView.

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final ChildViewHolder holder;
    if (convertView == null) {
        holder = new ChildViewHolder();
        convertView = inflater.inflate(R.layout.li_characteristic, parent, false);
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.uuid = (TextView) convertView.findViewById(R.id.uuid);
        holder.modes = (TextView) convertView.findViewById(R.id.modes);
        holder.seek = (SeekBar) convertView.findViewById(R.id.seek);
        holder.seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (serviceListener == null || def == null || !fromUser) {
                    return;
                }
                final BaseSensor<?> sensor = (BaseSensor<?>) def.getSensor(holder.service.getUuid().toString());
                if (sensor == null) {
                    return;
                }
                if (sensor instanceof TiPeriodicalSensor) {
                    final TiPeriodicalSensor periodicalSensor = (TiPeriodicalSensor) sensor;
                    periodicalSensor.setPeriod(progress + periodicalSensor.getMinPeriod());
                    serviceListener.onServiceUpdated(holder.service);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        convertView.setTag(holder);
    } else {
        holder = (ChildViewHolder) convertView.getTag();
    }
    final BluetoothGattCharacteristic item = getChild(groupPosition, childPosition);
    final String uuid = item.getUuid().toString();
    final String name;
    final String modes = getModeString(item.getProperties());
    holder.service = item.getService();
    final String serviceUUID = item.getService().getUuid().toString();
    final Sensor<?> sensor = def != null ? (Sensor<?>) def.getSensor(serviceUUID) : null;
    // reset visibility
    holder.uuid.setVisibility(View.VISIBLE);
    holder.seek.setVisibility(View.GONE);
    if (sensor != null) {
        name = sensor.getCharacteristicName(uuid);
        if (sensor instanceof BaseSensor) {
            final BaseSensor<?> tiSensor = (BaseSensor<?>) sensor;
            if (tiSensor.isConfigUUID(uuid)) {
                if (sensor instanceof TiPeriodicalSensor) {
                    final TiPeriodicalSensor periodicalSensor = (TiPeriodicalSensor) sensor;
                    final int max = periodicalSensor.getMaxPeriod() - periodicalSensor.getMinPeriod();
                    final int value = periodicalSensor.getPeriod() - periodicalSensor.getMinPeriod();
                    holder.seek.setMax(max);
                    holder.seek.setProgress(value);
                    holder.seek.setVisibility(View.VISIBLE);
                    holder.uuid.setVisibility(View.GONE);
                }
            }
        }
    } else {
        name = "Unknown";
    }
    holder.name.setText(name);
    holder.uuid.setText(uuid);
    holder.modes.setText(modes);
    return convertView;
}
Also used : SeekBar(android.widget.SeekBar) BaseSensor(sample.ble.sensortag.sensor.BaseSensor) TiPeriodicalSensor(sample.ble.sensortag.sensor.ti.TiPeriodicalSensor) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 2 with BaseSensor

use of sample.ble.sensortag.sensor.BaseSensor in project BleSensorTag by StevenRudenko.

the class DeviceServicesActivity method onChildClick.

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    if (gattServiceAdapter == null) {
        return false;
    }
    final BluetoothGattCharacteristic characteristic = gattServiceAdapter.getChild(groupPosition, childPosition);
    final DeviceDef def = getBleManager().getDeviceDefCollection().get(getDeviceName(), getDeviceAddress());
    if (def == null) {
        return false;
    }
    final Sensor<?> sensor = (Sensor<?>) def.getSensor(characteristic.getService().getUuid().toString());
    if (sensor == null) {
        return true;
    }
    if (sensor == activeSensor) {
        return true;
    }
    final String address = getDeviceAddress();
    if (sensor instanceof BaseSensor) {
        final BaseSensor<?> baseSensor = (BaseSensor<?>) sensor;
        if (activeSensor != null) {
            activeSensor.setEnabled(false);
            getBleManager().update(address, activeSensor, activeSensor.getConfigUUID(), null);
        }
        activeSensor = baseSensor;
        baseSensor.setEnabled(true);
        getBleManager().update(address, sensor, baseSensor.getConfigUUID(), null);
        getBleManager().listen(address, sensor, baseSensor.getDataUUID());
    } else {
        getBleManager().read(address, sensor, characteristic.getUuid().toString());
    }
    return true;
}
Also used : BaseSensor(sample.ble.sensortag.sensor.BaseSensor) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) DeviceDef(com.chimeraiot.android.ble.sensor.DeviceDef) TiPeriodicalSensor(sample.ble.sensortag.sensor.ti.TiPeriodicalSensor) Sensor(com.chimeraiot.android.ble.sensor.Sensor) BaseSensor(sample.ble.sensortag.sensor.BaseSensor)

Aggregations

BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)2 BaseSensor (sample.ble.sensortag.sensor.BaseSensor)2 TiPeriodicalSensor (sample.ble.sensortag.sensor.ti.TiPeriodicalSensor)2 SeekBar (android.widget.SeekBar)1 DeviceDef (com.chimeraiot.android.ble.sensor.DeviceDef)1 Sensor (com.chimeraiot.android.ble.sensor.Sensor)1