Detach a device

Detach a device from a gateway.

Code sample

C#

public static object DetachDevice(MqttClient client, string deviceId)
{
    var detachTopic = $"/devices/{deviceId}/detach";
    Console.WriteLine("Detaching: {0}", detachTopic);
    var BinaryData = Encoding.UTF8.GetBytes("{}");
    client.Publish(detachTopic, BinaryData, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
    return 0;
}

Go


// detatchDevice detaches a device from a gateway.
func detachDevice(deviceID string, client mqtt.Client, jwt string) error {
	detachTopic := fmt.Sprintf("/devices/%s/detach", deviceID)
	fmt.Printf("Detaching device: %s\n", detachTopic)

	detachPayload := "{}"
	if jwt != "" {
		detachPayload = fmt.Sprintf("{ 'authorization' : %s }", jwt)
	}

	if token := client.Publish(detachTopic, 1, false, detachPayload); token.Wait() && token.Error() != nil {
		return token.Error()
	}
	return nil
}

Java

final String detachTopic = String.format("/devices/%s/detach", deviceId);
System.out.println(String.format("Detaching: %s", detachTopic));
String attachPayload = "{}";
MqttMessage message = new MqttMessage(attachPayload.getBytes(StandardCharsets.UTF_8.name()));
message.setQos(1);
client.publish(detachTopic, message);

Python

detach_topic = "/devices/{}/detach".format(device_id)
print("Detaching: {}".format(detach_topic))
client.publish(detach_topic, "{}", qos=1)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser