A method for performing service discovery in a neighbor awareness network, NAN, performed by a second device, wherein the second device and a first device are devices in the NAN, the method comprising: • receiving a service message sent by the first device, wherein the service message comprises a first service identifier, the service message is used to query a service required by the first device or broadcast a service supported by the first device, wherein the first service identifier is dependent on change information and first service information in the NAN, the first service information is information about the service queried by the first device or information about the service supported by the first device, wherein the change information is information that is agreed between the first device and the second device in advance and that dynamically changes, and the change information comprises: global change information in the NAN, or change information of the first device, wherein the global change information comprises at least one of: an anchor master rank, AMR, a first master rank, first MR, a first random factor, first RF, an anchor master beacon transmission time, AMBTT, a first time synchronization function, first TSF, value, or a cluster grade CG; and the change information of the first device comprises at least one of: a second MR, an interface address, a second TSF value, or a second RF; • obtaining the change information, and generating a second service identifier according to the change information and second service information, wherein if the first service information is the information about the service supported by the first device, the corresponding second service information is information about a service queried by the second device, or if the first service information is the information about the service queried by the first device, the corresponding second service information is information about a service supported by the second device; and • matching the first service identifier and the second service identifier, and if the matching succeeds, sending a service response message to the first device. • receiving a service message sent by the first device, wherein the service message comprises a first service identifier, the service message is used to query a service required by the first device or broadcast a service supported by the first device, wherein the first service identifier is dependent on change information and first service information in the NAN, the first service information is information about the service queried by the first device or information about the service supported by the first device, wherein the change information is information that is agreed between the first device and the second device in advance and that dynamically changes, and the change information comprises: global change information in the NAN, or change information of the first device, wherein the global change information comprises at least one of: an anchor master rank, AMR, a first master rank, first MR, a first random factor, first RF, an anchor master beacon transmission time, AMBTT, a first time synchronization function, first TSF, value, or a cluster grade CG; and the change information of the first device comprises at least one of: a second MR, an interface address, a second TSF value, or a second RF; an anchor master rank, AMR, a first master rank, first MR, a first random factor, first RF, an anchor master beacon transmission time, AMBTT, a first time synchronization function, first TSF, value, or a cluster grade CG; and the change information of the first device comprises at least one of: a second MR, an interface address, a second TSF value, or a second RF; • obtaining the change information, and generating a second service identifier according to the change information and second service information, wherein if the first service information is the information about the service supported by the first device, the corresponding second service information is information about a service queried by the second device, or if the first service information is the information about the service queried by the first device, the corresponding second service information is information about a service supported by the second device; and • matching the first service identifier and the second service identifier, and if the matching succeeds, sending a service response message to the first device.