Page 199 - MDP2022-2
P. 199
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.headthings.mdp_project_2022.Mqtt;
import com.headthings.mdp_project_2022.OneClickView;
import com.headthings.mdp_project_2022.R;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import butterknife.BindView;
import butterknife.ButterKnife;
public class HomeFragment extends Fragment {
@BindView(R.id.bulb)
OneClickView led;
@BindView(R.id.curtain)
OneClickView curtain;
@BindView(R.id.heating_pad)
OneClickView heatingPad;
@BindView(R.id.door)
OneClickView door;
MqttClient mqtt = Mqtt.getInstance();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, view);
led.setOnCheckedChangeListener((compoundButton, b) -> {
try {
if (b) {
Log.d("iot/led", "on");
mqtt.publish("iot/led", "on".getBytes(), 0, false);
} else {
Log.d("iot/led", "off");
mqtt.publish("iot/led", "off".getBytes(), 0, false);