• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ble_service_data.h"
17 
18 #include <algorithm>
19 
20 #include "array"
21 #include "map"
22 #include "vector"
23 
24 namespace OHOS {
25 namespace bluetooth {
26 /**
27  * @brief Represents scan settings.
28  *
29  * @since 6
30  */
31 
SetReportDelay(long reportDelayMillis)32 void BleScanSettingsImpl::SetReportDelay(long reportDelayMillis)
33 {
34     reportDelayMillis_ = reportDelayMillis;
35 }
36 
37 /**
38  * @brief Get the report delay time.
39  *
40  * @return Returns Report delay time.
41  * @since 6
42  */
GetReportDelayMillisValue() const43 long BleScanSettingsImpl::GetReportDelayMillisValue() const
44 {
45     return reportDelayMillis_;
46 }
47 
SetScanMode(int scanMode)48 void BleScanSettingsImpl::SetScanMode(int scanMode)
49 {
50     scanMode_ = scanMode;
51 }
52 
GetScanMode() const53 int BleScanSettingsImpl::GetScanMode() const
54 {
55     return scanMode_;
56 }
57 
SetLegacy(bool legacy)58 void BleScanSettingsImpl::SetLegacy(bool legacy)
59 {
60     legacy_ = legacy;
61 }
62 
GetLegacy() const63 bool BleScanSettingsImpl::GetLegacy() const
64 {
65     return legacy_;
66 }
67 
SetPhy(int phy)68 void BleScanSettingsImpl::SetPhy(int phy)
69 {
70     phy_ = phy;
71 }
72 
GetPhy() const73 int BleScanSettingsImpl::GetPhy() const
74 {
75     return phy_;
76 }
77 
SetDeviceId(std::string deviceId)78 void BleScanFilterImpl::SetDeviceId(std::string deviceId)
79 {
80     deviceId_ = deviceId;
81 }
82 
GetDeviceId() const83 std::string BleScanFilterImpl::GetDeviceId() const
84 {
85     return deviceId_;
86 }
87 
SetName(std::string name)88 void BleScanFilterImpl::SetName(std::string name)
89 {
90     name_ = name;
91 }
92 
GetName() const93 std::string BleScanFilterImpl::GetName() const
94 {
95     return name_;
96 }
97 
SetServiceUuid(const Uuid & serviceUuid)98 void BleScanFilterImpl::SetServiceUuid(const Uuid &serviceUuid)
99 {
100     serviceUuid_ = serviceUuid;
101     hasServiceUuid_ = true;
102 }
103 
HasServiceUuid() const104 bool BleScanFilterImpl::HasServiceUuid() const
105 {
106     return hasServiceUuid_;
107 }
108 
GetServiceUuid() const109 Uuid BleScanFilterImpl::GetServiceUuid() const
110 {
111     return serviceUuid_;
112 }
113 
SetServiceUuidMask(const Uuid & serviceUuidMask)114 void BleScanFilterImpl::SetServiceUuidMask(const Uuid &serviceUuidMask)
115 {
116     serviceUuidMask_ = serviceUuidMask;
117     hasServiceUuidMask_ = true;
118 }
119 
HasServiceUuidMask() const120 bool BleScanFilterImpl::HasServiceUuidMask() const
121 {
122     return hasServiceUuidMask_;
123 }
124 
GetServiceUuidMask() const125 Uuid BleScanFilterImpl::GetServiceUuidMask() const
126 {
127     return serviceUuidMask_;
128 }
129 
SetServiceSolicitationUuid(const Uuid & serviceSolicitationUuid)130 void BleScanFilterImpl::SetServiceSolicitationUuid(const Uuid &serviceSolicitationUuid)
131 {
132     serviceSolicitationUuid_ = serviceSolicitationUuid;
133     hasSolicitationUuid_ = true;
134 }
135 
HasSolicitationUuid() const136 bool BleScanFilterImpl::HasSolicitationUuid() const
137 {
138     return hasSolicitationUuid_;
139 }
140 
GetServiceSolicitationUuid() const141 Uuid BleScanFilterImpl::GetServiceSolicitationUuid() const
142 {
143     return serviceSolicitationUuid_;
144 }
145 
SetServiceSolicitationUuidMask(const Uuid & serviceSolicitationUuidMask)146 void BleScanFilterImpl::SetServiceSolicitationUuidMask(const Uuid &serviceSolicitationUuidMask)
147 {
148     serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
149     hasSolicitationUuidMask_ = true;
150 }
151 
HasSolicitationUuidMask() const152 bool BleScanFilterImpl::HasSolicitationUuidMask() const
153 {
154     return hasSolicitationUuidMask_;
155 }
156 
GetServiceSolicitationUuidMask() const157 Uuid BleScanFilterImpl::GetServiceSolicitationUuidMask() const
158 {
159     return serviceSolicitationUuidMask_;
160 }
161 
SetServiceData(std::vector<uint8_t> serviceData)162 void BleScanFilterImpl::SetServiceData(std::vector<uint8_t> serviceData)
163 {
164     serviceData_ = serviceData;
165 }
166 
GetServiceData() const167 std::vector<uint8_t> BleScanFilterImpl::GetServiceData() const
168 {
169     return serviceData_;
170 }
171 
SetServiceDataMask(std::vector<uint8_t> serviceDataMask)172 void BleScanFilterImpl::SetServiceDataMask(std::vector<uint8_t> serviceDataMask)
173 {
174     serviceDataMask_ = serviceDataMask;
175 }
176 
GetServiceDataMask() const177 std::vector<uint8_t> BleScanFilterImpl::GetServiceDataMask() const
178 {
179     return serviceDataMask_;
180 }
181 
SetManufacturerId(uint16_t manufacturerId)182 void BleScanFilterImpl::SetManufacturerId(uint16_t manufacturerId)
183 {
184     manufacturerId_ = manufacturerId;
185 }
186 
GetManufacturerId() const187 uint16_t BleScanFilterImpl::GetManufacturerId() const
188 {
189     return manufacturerId_;
190 }
191 
SetManufactureData(std::vector<uint8_t> manufactureData)192 void BleScanFilterImpl::SetManufactureData(std::vector<uint8_t> manufactureData)
193 {
194     manufactureData_ = manufactureData;
195 }
196 
GetManufactureData() const197 std::vector<uint8_t> BleScanFilterImpl::GetManufactureData() const
198 {
199     return manufactureData_;
200 }
201 
SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)202 void BleScanFilterImpl::SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)
203 {
204     manufactureDataMask_ = manufactureDataMask;
205 }
206 
GetManufactureDataMask() const207 std::vector<uint8_t> BleScanFilterImpl::GetManufactureDataMask() const
208 {
209     return manufactureDataMask_;
210 }
211 
SetClientId(int clientId)212 void BleScanFilterImpl::SetClientId(int clientId)
213 {
214     clientId_ = clientId;
215 }
216 
GetClientId() const217 int BleScanFilterImpl::GetClientId() const
218 {
219     return clientId_;
220 }
221 
SetFiltIndex(uint8_t filtIndex)222 void BleScanFilterImpl::SetFiltIndex(uint8_t filtIndex)
223 {
224     filtIndex_ = filtIndex;
225 }
226 
GetFiltIndex() const227 uint8_t BleScanFilterImpl::GetFiltIndex() const
228 {
229     return filtIndex_;
230 }
231 
SetFilterAction(uint8_t action)232 void BleScanFilterImpl::SetFilterAction(uint8_t action)
233 {
234     action_ = action;
235 }
236 
GetFilterAction() const237 uint8_t BleScanFilterImpl::GetFilterAction() const
238 {
239     return action_;
240 }
241 
242 /**
243  * @brief Check if the device service is connectable.
244  *
245  * @return Returns <b>true</b> if device service is connectable;
246  *         Returns <b>false</b> otherwise.
247  * @since 6
248  */
IsConnectable() const249 bool BleAdvertiserSettingsImpl::IsConnectable() const
250 {
251     return connectable_;
252 }
253 
254 /**
255  * @brief Set whether the device service is connectable.
256  *
257  * @param connectable Whether the device service is connectable.
258  * @since 6
259  */
SetConnectable(bool connectable)260 void BleAdvertiserSettingsImpl::SetConnectable(bool connectable)
261 {
262     connectable_ = connectable;
263 }
264 
265 /**
266  * @brief Check if the advertiser is in legacy mode.
267  *
268  * @return Returns <b>true</b> if the advertiser is in legacy mode;
269  *         Returns <b>false</b> otherwisee.
270  * @since 6
271  */
IsLegacyMode() const272 bool BleAdvertiserSettingsImpl::IsLegacyMode() const
273 {
274     return legacyMode_;
275 }
276 
277 /**
278  * @brief Set whether to enable the legacy mode.
279  *
280  * @param connectable Whether to enable the legacy mode
281  * @since 6
282  */
SetLegacyMode(bool legacyMode)283 void BleAdvertiserSettingsImpl::SetLegacyMode(bool legacyMode)
284 {
285     legacyMode_ = legacyMode;
286 }
287 
288 /**
289  * @brief Get advertise interval.
290  *
291  * @return Returns the advertising interval.
292  * @since 6
293  */
GetInterval() const294 int BleAdvertiserSettingsImpl::GetInterval() const
295 {
296     return interval_;
297 }
298 
299 /**
300  * @brief Set advertise interval.
301  *
302  * @param interval Advertise interval.
303  * @since 6
304  */
SetInterval(int interval)305 void BleAdvertiserSettingsImpl::SetInterval(int interval)
306 {
307     interval_ = interval;
308 }
309 
310 /**
311  * @brief Get the advertiser Tx power.
312  *
313  * @return Returns advertiser Tx power.
314  * @since 6
315  */
GetTxPower() const316 int BleAdvertiserSettingsImpl::GetTxPower() const
317 {
318     return txPower_;
319 }
320 
321 /**
322  * @brief Set the advertiser Tx power.
323  *
324  * @param txPowerthe advertiser Tx power.
325  * @since 6
326  */
SetTxPower(int txPower)327 int BleAdvertiserSettingsImpl::SetTxPower(int txPower)
328 {
329     if (txPower > BLE_ADV_TX_POWER_HIGH || txPower < BLE_ADV_TX_POWER_ULTRA_LOW) {
330         return RET_BAD_PARAM;
331     }
332     switch (txPower) {
333         case BLE_ADV_TX_POWER_ULTRA_LOW:
334             txPower_ = BLE_ADV_TX_POWER_ULTRA_LOW_VALUE;
335             break;
336         case BLE_ADV_TX_POWER_LOW:
337             txPower_ = BLE_ADV_TX_POWER_LOW_VALUE;
338             break;
339         case BLE_ADV_TX_POWER_MEDIUM:
340             txPower_ = BLE_ADV_TX_POWER_MEDIUM_VALUE;
341             break;
342         case BLE_ADV_TX_POWER_HIGH:
343             txPower_ = BLE_ADV_TX_POWER_HIGH_VALUE;
344             break;
345         default:
346             txPower_ = BLE_ADV_TX_POWER_LOW_VALUE;
347             break;
348     }
349 
350     return RET_NO_ERROR;
351 }
352 
353 /**
354  * @brief Get the primary phy.
355  *
356  * @return Returns the primary phy.
357  * @since 6
358  */
GetPrimaryPhy() const359 int BleAdvertiserSettingsImpl::GetPrimaryPhy() const
360 {
361     return primaryPhy_;
362 }
363 
364 /**
365  * @brief Set the primary phy.
366  *
367  * @param primaryPhy Primary phy.
368  * @since 6
369  */
SetPrimaryPhy(int primaryPhy)370 void BleAdvertiserSettingsImpl::SetPrimaryPhy(int primaryPhy)
371 {
372     primaryPhy_ = primaryPhy;
373 }
374 
375 /**
376  * @brief Get the secondary Phy.
377  *
378  * @return Returns primary phy.
379  * @since 6
380  */
GetSecondaryPhy() const381 int BleAdvertiserSettingsImpl::GetSecondaryPhy() const
382 {
383     return secondaryPhy_;
384 }
385 
386 /**
387  * @brief Set the secondary phy.
388  *
389  * @param secondaryPhy Secondary Phy.
390  * @since 6
391  */
SetSecondaryPhy(int secondaryPhy)392 void BleAdvertiserSettingsImpl::SetSecondaryPhy(int secondaryPhy)
393 {
394     secondaryPhy_ = secondaryPhy;
395 }
396 
397 /**
398  * @brief A constructor used to create a <b>BleAdvertiseDataInternal</b> instance.
399  *
400  * @since 6
401  */
BleAdvertiserDataImpl()402 BleAdvertiserDataImpl::BleAdvertiserDataImpl() : payload_()
403 {}
404 
405 /**
406  * @brief Add manufacturer data.
407  *
408  * @param manufacturerId manufacturer Id which addad data.
409  * @param data manufacturer data
410  * @since 6
411  */
AddManufacturerData(uint16_t manufacturerId,const std::string & data)412 int BleAdvertiserDataImpl::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
413 {
414     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
415     cdata[0] = static_cast<char>(manufacturerId & 0xFF);
416     cdata[1] = static_cast<char>((manufacturerId >> BLE_ONE_BYTE_LEN) & 0xFF);
417     SetManufacturerData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
418 
419     return RET_NO_ERROR;
420 }
421 
422 /**
423  * @brief Add service data.
424  *
425  * @param uuid Uuid of service data.
426  * @param data Service data.
427  * @since 6
428  */
AddServiceData(const Uuid & uuid,const std::string & data)429 void BleAdvertiserDataImpl::AddServiceData(const Uuid &uuid, const std::string &data)
430 {
431     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
432     switch (uuid.GetUuidType()) {
433         case Uuid::UUID16_BYTES_TYPE: {
434             /// [Len] [0x16] [UUID16] data
435             cdata[0] = data.length() + BLE_UUID_LEN_16 + 1;
436             cdata[1] = BLE_AD_TYPE_SERVICE_DATA;  /// 0x16
437             uint16_t uuid16 = uuid.ConvertTo16Bits();
438             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
439                     std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16) + data);
440             break;
441         }
442 
443         case Uuid::UUID32_BYTES_TYPE: {
444             /// [Len] [0x20] [UUID32] data
445             cdata[0] = data.length() + BLE_UUID_LEN_32 + 1;
446             cdata[1] = BLE_AD_TYPE_32SERVICE_DATA;  /// 0x20
447             uint32_t uuid32 = uuid.ConvertTo32Bits();
448             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
449                     std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32) + data);
450             break;
451         }
452 
453         case Uuid::UUID128_BYTES_TYPE: {
454             /// [Len] [0x21] [UUID128] data
455             cdata[0] = data.length() + BLE_UUID_LEN_128 + 1;
456             cdata[1] = BLE_AD_TYPE_128SERVICE_DATA;  /// 0x21
457             uint8_t uuidData[BLE_UUID_LEN_128];
458             uuid.ConvertToBytesLE(uuidData);
459             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
460                     std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128) + data);
461             break;
462         }
463 
464         default:
465             return;
466     }
467 }
468 
469 /**
470  * @brief Add characteristic value.
471  *
472  * @param advertiser type of the field.
473  * @param advertiser data.
474  * @since 6
475  */
AddCharacteristicValue(uint8_t adtype,const std::string & data)476 void BleAdvertiserDataImpl::AddCharacteristicValue(uint8_t adtype, const std::string &data)
477 {
478     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
479     cdata[0] = data.length() + 1;
480     cdata[1] = adtype;
481     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
482 }
483 
484 /**
485  * @brief Add service uuid.
486  *
487  * @param uuid Service uuid.
488  * @since 6
489  */
AddServiceUuid(const Uuid & uuid)490 void BleAdvertiserDataImpl::AddServiceUuid(const Uuid &uuid)
491 {
492     SetCompleteServices(uuid);
493 }
494 
495 /**
496  * @brief Set device appearance.
497  *
498  * @param appearance Device appearance.
499  * @since 6
500  */
SetAppearance(uint16_t appearance)501 void BleAdvertiserDataImpl::SetAppearance(uint16_t appearance)
502 {
503     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
504     cdata[0] = BLE_ADV_DATA_BYTE_FIELD_LEN;
505     cdata[1] = BLE_AD_TYPE_APPEARANCE;  /// 0x19
506     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
507             std::string(reinterpret_cast<char *>(&appearance), BLE_ADV_DATA_FIELD_TYPE_AND_LEN));
508 }
509 
510 /**
511  * @brief Set complete services.
512  *
513  * @param uuid Service uuid.
514  * @since 6
515  */
SetCompleteServices(const Uuid & uuid)516 void BleAdvertiserDataImpl::SetCompleteServices(const Uuid &uuid)
517 {
518     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
519     switch (uuid.GetUuidType()) {
520         case Uuid::UUID16_BYTES_TYPE: {
521             /// [Len] [0x02] [LL] [HH]
522             cdata[0] = BLE_UUID_LEN_16 + 1;
523             cdata[1] = BLE_AD_TYPE_16SRV_CMPL;  /// 0x03
524             uint16_t uuid16 = uuid.ConvertTo16Bits();
525             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
526                     std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16));
527             break;
528         }
529 
530         case Uuid::UUID32_BYTES_TYPE: {
531             /// [Len] [0x04] [LL] [LL] [HH] [HH]
532             cdata[0] = BLE_UUID_LEN_32 + 1;
533             cdata[1] = BLE_AD_TYPE_32SRV_CMPL;  /// 0x05
534             uint32_t uuid32 = uuid.ConvertTo32Bits();
535             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
536                     std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32));
537             break;
538         }
539 
540         case Uuid::UUID128_BYTES_TYPE: {
541             /// [Len] [0x04] [0] [1] ... [15]
542             cdata[0] = BLE_UUID_LEN_128 + 1;
543             cdata[1] = BLE_AD_TYPE_128SRV_CMPL;  /// 0x07
544             uint8_t uuidData[BLE_UUID_LEN_128];
545             uuid.ConvertToBytesLE(uuidData);
546             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
547                     std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128));
548             break;
549         }
550 
551         default:
552             return;
553     }
554 }
555 
556 /**
557  * @brief Set advertiser flag.
558  *
559  * @param flag Advertiser flag.
560  * @since 6
561  */
SetFlags(uint8_t flag)562 void BleAdvertiserDataImpl::SetFlags(uint8_t flag)
563 {
564     char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
565     cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
566     cdata[1] = BLE_AD_TYPE_FLAG;  /// 0x01
567     cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = flag;
568     advFlag_ = flag;
569     AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
570 }
571 
GetFlags() const572 uint8_t BleAdvertiserDataImpl::GetFlags() const
573 {
574     return advFlag_;
575 }
576 
577 /**
578  * @brief Set manufacturer data.
579  *
580  * @param data manufacturer data.
581  * @since 6
582  */
SetManufacturerData(const std::string & data)583 void BleAdvertiserDataImpl::SetManufacturerData(const std::string &data)
584 {
585     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
586     cdata[0] = data.length() + 1;
587     cdata[1] = BLE_AD_MANUFACTURER_SPECIFIC_TYPE;  /// 0xff
588     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
589 }
590 
591 /**
592  * @brief Set device name.
593  *
594  * @param name Device name.
595  * @since 6
596  */
SetDeviceName(const std::string & name)597 void BleAdvertiserDataImpl::SetDeviceName(const std::string &name)
598 {
599     if (name.length() > DEVICE_NAME_MAX_LEN) {
600         SetLongName(name);
601     } else {
602         SetShortName(name);
603     }
604 }
605 
606 /**
607  * @brief Set Tx power level.
608  *
609  * @param txPowerLevel Tx power level.
610  * @since 6
611  */
SetTxPowerLevel(uint8_t txPowerLevel)612 void BleAdvertiserDataImpl::SetTxPowerLevel(uint8_t txPowerLevel)
613 {
614     char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
615     cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
616     cdata[1] = BLE_AD_TYPE_TX_PWR;  /// 0x09
617     cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = txPowerLevel;
618     AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
619 }
620 
621 /**
622  * @brief Add service data.
623  *
624  * @param data Service data.
625  * @since 6
626  */
AddData(std::string data)627 void BleAdvertiserDataImpl::AddData(std::string data)
628 {
629     payload_.append(data);
630 }
631 
632 /**
633  * @brief Get advertiser data packet.
634  *
635  * @return Returns advertiser data packet.
636  * @since 6
637  */
SetPayload(const std::string & payload)638 void BleAdvertiserDataImpl::SetPayload(const std::string &payload)
639 {
640     payload_ = payload;
641 }
642 
643 /**
644  * @brief Get advertiser data packet.
645  *
646  * @return Returns advertiser data packet.
647  * @since 6
648  */
GetPayload() const649 std::string BleAdvertiserDataImpl::GetPayload() const
650 {
651     return payload_;
652 }
653 
654 /**
655  * @brief Set advertiser data long name.
656  *
657  * @param name Bluetooth device name.
658  * @since 6
659  */
SetLongName(const std::string & name)660 void BleAdvertiserDataImpl::SetLongName(const std::string &name)
661 {
662     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
663     cdata[0] = name.length() + 1;
664     cdata[1] = BLE_AD_TYPE_NAME_SHORT;  /// 0x08
665     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
666 }
667 
668 /**
669  * @brief Set advertiser data short name.
670  *
671  * @param name Bluetooth device name.
672  * @since 6
673  */
SetShortName(const std::string & name)674 void BleAdvertiserDataImpl::SetShortName(const std::string &name)
675 {
676     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
677     cdata[0] = name.length() + 1;
678     cdata[1] = BLE_AD_TYPE_NAME_CMPL;  /// 0x09
679     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
680 }
681 
BlePeripheralDevice()682 BlePeripheralDevice::BlePeripheralDevice()
683     : manufacturerData_(),
684       name_(),
685       serviceUUIDs_(),
686       txPower_(BLE_ADDR_TYPE_RANDOM),
687       serviceData_(),
688       serviceDataUUIDs_(),
689       pairState_(BLE_PAIR_NONE),
690       ioCapability_(BLE_IO_CAP_NONE),
691       aliasName_()
692 {
693     manufacturerData_.clear();
694     serviceDataUUIDs_.clear();
695     serviceData_.clear();
696     serviceUUIDs_.clear();
697 }
698 
~BlePeripheralDevice()699 BlePeripheralDevice::~BlePeripheralDevice()
700 {
701     manufacturerData_.clear();
702     serviceUUIDs_.clear();
703     serviceData_.clear();
704     serviceDataUUIDs_.clear();
705 }
706 
707 /**
708  * @brief Get device address.
709  *
710  * @return Returns device address.
711  * @since 6
712  */
GetRawAddress() const713 RawAddress BlePeripheralDevice::GetRawAddress() const
714 {
715     return address_;
716 }
717 
718 /**
719  * @brief Get device appearance.
720  *
721  * @return Returns the device appearance.
722  * @since 6
723  */
GetAppearance() const724 uint16_t BlePeripheralDevice::GetAppearance() const
725 {
726     return appearance_;
727 }
728 
729 /**
730  * @brief Get the manufacturer data.
731  *
732  * @return Returns the manufacturer data.
733  * @since 6
734  */
GetManufacturerData() const735 std::map<uint16_t, std::string> BlePeripheralDevice::GetManufacturerData() const
736 {
737     return manufacturerData_;
738 }
739 
740 /**
741  * @brief the device name.
742  *
743  * @return Returns device Name.
744  * @since 6
745  */
GetName() const746 std::string BlePeripheralDevice::GetName() const
747 {
748     return name_;
749 }
750 
751 /**
752  * @brief Get device RSSI.
753  *
754  * @return Returns device RSSI.
755  * @since 6
756  */
GetRSSI() const757 int8_t BlePeripheralDevice::GetRSSI() const
758 {
759     return rssi_;
760 }
761 
762 /**
763  * @brief Get service data.
764  *
765  * @return Returns service data.
766  * @since 6
767  */
GetServiceData() const768 std::vector<std::string> BlePeripheralDevice::GetServiceData() const
769 {
770     return serviceData_;
771 }
772 
773 /**
774  * @brief Get Service Data.
775  *
776  * @param index Service data index.
777  * @return Returns service data.
778  * @since 6
779  */
GetServiceData(int index) const780 std::string BlePeripheralDevice::GetServiceData(int index) const
781 {
782     return serviceData_.empty() ? "" : ((size_t)index < serviceData_.size() ? serviceData_[index] : "");
783 }
784 
785 /**
786  * @brief Get service data UUID.
787  *
788  * @return Returns service data UUID.
789  * @since 6
790  */
GetServiceDataUUID() const791 std::vector<Uuid> BlePeripheralDevice::GetServiceDataUUID() const
792 {
793     return serviceDataUUIDs_;
794 }
795 
796 /**
797  * @brief Get service data UUID.
798  *
799  * @param index Service data index.
800  * @return Returns service data UUID.
801  * @since 6
802  */
GetServiceDataUUID(int index) const803 Uuid BlePeripheralDevice::GetServiceDataUUID(int index) const
804 {
805     Uuid uuid {};
806     return serviceDataUUIDs_.empty() ? uuid : serviceDataUUIDs_[index];
807 }
808 
809 /**
810  * @brief Get the service UUID.
811  *
812  * @return Returns service UUID.
813  * @since 6
814  */
GetServiceUUID() const815 std::vector<Uuid> BlePeripheralDevice::GetServiceUUID() const
816 {
817     return serviceUUIDs_;
818 }
819 
820 /**
821  * @brief Get service UUID.
822  *
823  * @param index Service UUID index.
824  * @return Return service UUID.
825  * @since 6
826  */
GetServiceUUID(int index) const827 Uuid BlePeripheralDevice::GetServiceUUID(int index) const
828 {
829     Uuid uuid {};
830     return serviceUUIDs_.empty() ? uuid : serviceUUIDs_[index];
831 }
832 
833 /**
834  * @brief Get address type.
835  *
836  * @return Returns address type.
837  * @since 6
838  */
GetAddressType() const839 int BlePeripheralDevice::GetAddressType() const
840 {
841     return addressType_;
842 }
843 
844 /**
845  * @brief Set address type.
846  *
847  * @param type Address type.
848  * @since 6
849  */
SetAddressType(int type)850 void BlePeripheralDevice::SetAddressType(int type)
851 {
852     addressType_ = type;
853 }
854 
855 /**
856  * @brief Check if manufacturer data is included.
857  *
858  * @return Returns <b>true</b> if manufacturer data is included;
859  *         Returns <b>false</b> otherwise.
860  * @since 6
861  */
IsManufacturerData() const862 bool BlePeripheralDevice::IsManufacturerData() const
863 {
864     return isManufacturerData_;
865 }
866 
867 /**
868  * @brief Check if the device RSSI is included.
869  *
870  * @return Returns <b>true</b> if include device rssi;
871  *         Returns <b>false</b> otherwise.
872  * @since 6
873  */
IsRSSI() const874 bool BlePeripheralDevice::IsRSSI() const
875 {
876     return isRSSI_;
877 }
878 
879 /**
880  * @brief Check if service data is included.
881  *
882  * @return Returns <b>true</b> if include service data;
883  *         Returns <b>false</b> otherwise.
884  * @since 6
885  */
IsServiceData() const886 bool BlePeripheralDevice::IsServiceData() const
887 {
888     return isServiceData_;
889 }
890 
891 /**
892  * @brief Check if the service UUID is included.
893  *
894  * @return Returns <b>true</b> if the service UUID is included;
895  *         Returns <b>false</b> otherwise.
896  * @since 6
897  */
IsServiceUUID() const898 bool BlePeripheralDevice::IsServiceUUID() const
899 {
900     return isServiceUUID_;
901 }
902 
IsName(void) const903 bool BlePeripheralDevice::IsName(void) const
904 {
905     return isName_;
906 }
907 
908 /**
909  * @brief Set device address.
910  *
911  * @param address device address.
912  * @since 6
913  */
SetAddress(const RawAddress & address)914 void BlePeripheralDevice::SetAddress(const RawAddress &address)
915 {
916     address_ = address;
917 }
918 
919 /**
920  * @brief Set RSSI value.
921  *
922  * @param RSSI value.
923  * @since 6
924  */
SetRSSI(int8_t rssi)925 void BlePeripheralDevice::SetRSSI(int8_t rssi)
926 {
927     rssi_ = rssi;
928     isRSSI_ = true;
929 }
930 /**
931  * @brief Check whether device is connectable.
932  *
933  * @param [in] rssi value.
934  * return Returns <b>true</b> if device is connectable.
935  *        Returns <b>false</b> otherwisee.
936  */
IsConnectable() const937 bool BlePeripheralDevice::IsConnectable() const
938 {
939     return connectable_;
940 }
941 /**
942  * @brief Sets whether the peer device is connectable.
943  *
944  * @param peer device's connectable.
945  */
SetConnectable(bool connectable)946 void BlePeripheralDevice::SetConnectable(bool connectable)
947 {
948     connectable_ = connectable;
949 }
950 /**
951  * @brief Parse advertisement packets.
952  *
953  * @param payload Advertisement packet.
954  * @param total_len Advertisement packet length.
955  * @since 6
956  */
ParseAdvertiserment(BlePeripheralDeviceParseAdvData & parseAdvData)957 void BlePeripheralDevice::ParseAdvertiserment(BlePeripheralDeviceParseAdvData &parseAdvData)
958 {
959     size_t sizeConsumed = 0;
960     bool finished = false;
961     size_t totalLength = parseAdvData.length;
962     payload_ = parseAdvData.payload;
963     payloadLen_ = parseAdvData.length;
964 
965     while (!finished) {
966         size_t length = *parseAdvData.payload;
967         sizeConsumed += 1 + length;
968         if (sizeConsumed > totalLength) {
969             break;
970         }
971         parseAdvData.payload++;
972 
973         if (length != 0) {
974             uint8_t advType = *parseAdvData.payload;
975             parseAdvData.payload++;
976             length--;
977             parseAdvData.length = length;
978             BuildAdvertiserData(advType, parseAdvData);
979             parseAdvData.payload += length;
980         }
981         if (sizeConsumed >= totalLength) {
982             finished = true;
983         }
984     }
985 }
986 
BuildAdvertiserData(uint8_t advType,BlePeripheralDeviceParseAdvData & parseAdvData)987 void BlePeripheralDevice::BuildAdvertiserData(uint8_t advType, BlePeripheralDeviceParseAdvData &parseAdvData)
988 {
989     switch (advType) {
990         case BLE_AD_TYPE_NAME_CMPL:   /// Data Type: 0x09
991         case BLE_AD_TYPE_NAME_SHORT:  /// Data Type: 0x08
992             SetName(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
993             break;
994         case BLE_AD_TYPE_TX_PWR:  /// Data Type: 0x0A
995             SetTXPower(*parseAdvData.payload);
996             break;
997         case BLE_AD_TYPE_APPEARANCE:  /// Data Type: 0x19
998             SetAppearance(*reinterpret_cast<uint16_t *>(parseAdvData.payload));
999             break;
1000         case BLE_AD_TYPE_FLAG:  /// Data Type: 0x01
1001             SetAdFlag(*parseAdvData.payload);
1002             break;
1003         case BLE_AD_TYPE_16SRV_CMPL:
1004         case BLE_AD_TYPE_16SRV_PART:  /// Data Type: 0x02
1005             SetServiceUUID16Bits(parseAdvData);
1006             break;
1007         case BLE_AD_TYPE_32SRV_CMPL:
1008         case BLE_AD_TYPE_32SRV_PART:  /// Data Type: 0x04
1009             SetServiceUUID32Bits(parseAdvData);
1010             break;
1011         case BLE_AD_TYPE_128SRV_CMPL:  /// Data Type: 0x07
1012         case BLE_AD_TYPE_128SRV_PART:  /// Data Type: 0x06
1013             SetServiceUUID128Bits(parseAdvData);
1014             break;
1015         case BLE_AD_MANUFACTURER_SPECIFIC_TYPE:
1016             SetManufacturerData(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
1017             break;
1018         case BLE_AD_TYPE_SERVICE_DATA:  /// Data Type: 0x16
1019             SetServiceDataUUID16Bits(parseAdvData);
1020             break;
1021         case BLE_AD_TYPE_32SERVICE_DATA:  /// Data Type: 0x20
1022             SetServiceDataUUID32Bits(parseAdvData);
1023             break;
1024         case BLE_AD_TYPE_128SERVICE_DATA:  /// Data Type: 0x21
1025             SetServiceDataUUID128Bits(parseAdvData);
1026             break;
1027         default:
1028             break;
1029     }
1030 }
1031 
SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1032 void BlePeripheralDevice::SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1033 {
1034     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_16; ++var) {
1035         SetServiceUUID(
1036             Uuid::ConvertFrom16Bits(*reinterpret_cast<uint16_t *>(parseAdvData.payload + var * BLE_UUID_LEN_16)));
1037     }
1038 }
1039 
SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1040 void BlePeripheralDevice::SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1041 {
1042     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_32; ++var) {
1043         SetServiceUUID(
1044             Uuid::ConvertFrom32Bits(*reinterpret_cast<uint32_t *>(parseAdvData.payload + var * BLE_UUID_LEN_32)));
1045     }
1046 }
1047 
SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData & parseAdvData)1048 void BlePeripheralDevice::SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData &parseAdvData)
1049 {
1050     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_128; ++var) {
1051         std::array<uint8_t, BLE_UUID_LEN_128> data = {};
1052         for (int i = 0; i < BLE_UUID_LEN_128; i++) {
1053             data[i] = *(parseAdvData.payload + var * BLE_UUID_LEN_128 + i);
1054         }
1055         SetServiceUUID(Uuid::ConvertFromBytesLE(data.data()));
1056     }
1057 }
1058 
SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1059 void BlePeripheralDevice::SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1060 {
1061     if (parseAdvData.length < BLE_UUID_LEN_16) {
1062         return;
1063     }
1064     uint16_t uuid = *(reinterpret_cast<uint16_t *>(parseAdvData.payload));
1065     std::string data = "";
1066     if (parseAdvData.length > BLE_UUID_LEN_16) {
1067         data = std::string(
1068             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_16), parseAdvData.length - BLE_UUID_LEN_16);
1069     }
1070     SetServiceDataUUID(Uuid::ConvertFrom16Bits(uuid), data);
1071 }
1072 
SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1073 void BlePeripheralDevice::SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1074 {
1075     if (parseAdvData.length < BLE_UUID_LEN_32) {
1076         return;
1077     }
1078     uint32_t uuid = *(reinterpret_cast<uint32_t *>(parseAdvData.payload));
1079     std::string data = "";
1080     if (parseAdvData.length > BLE_UUID_LEN_32) {
1081         data = std::string(
1082             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_32), parseAdvData.length - BLE_UUID_LEN_32);
1083     }
1084     SetServiceDataUUID(Uuid::ConvertFrom32Bits(uuid), data);
1085 }
1086 
SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1087 void BlePeripheralDevice::SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1088 {
1089     if (parseAdvData.length < BLE_UUID_LEN_128) {
1090         return;
1091     }
1092     std::string data = "";
1093     if (parseAdvData.length > BLE_UUID_LEN_128) {
1094         data = std::string(
1095             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_128), parseAdvData.length - BLE_UUID_LEN_128);
1096     }
1097     SetServiceDataUUID(Uuid::ConvertFromBytesLE(parseAdvData.payload), data);
1098 }
1099 
1100 /**
1101  * @brief Set device name.
1102  *
1103  * @param name Device name.
1104  * @since 6
1105  */
SetName(const std::string & name)1106 void BlePeripheralDevice::SetName(const std::string &name)
1107 {
1108     name_ = name;
1109     isName_ = true;
1110 }
1111 
1112 /**
1113  * @brief Set device roles.
1114  *
1115  * @param roles Device roles.
1116  * @since 6
1117  */
SetRoles(uint8_t roles)1118 void BlePeripheralDevice::SetRoles(uint8_t roles)
1119 {
1120     roles_ = roles;
1121 }
1122 
1123 /**
1124  * @brief Set bonded from local.
1125  *
1126  * @param flag Advertiser flag.
1127  * @since 6
1128  */
SetBondedFromLocal(bool flag)1129 void BlePeripheralDevice::SetBondedFromLocal(bool flag)
1130 {
1131     bondFlag_ = flag;
1132 }
1133 
1134 /**
1135  * @brief Set acl connection state.
1136  *
1137  * @param connectState Acl connection state.
1138  * @since 6
1139  */
SetAclConnectState(int connectState)1140 void BlePeripheralDevice::SetAclConnectState(int connectState)
1141 {
1142     aclConnected_ = connectState;
1143 }
1144 
1145 /**
1146  * @brief Set acl connection handle.
1147  *
1148  * @param handle Acl connection handle.
1149  * @since 6
1150  */
SetConnectionHandle(int handle)1151 void BlePeripheralDevice::SetConnectionHandle(int handle)
1152 {
1153     connectionHandle_ = handle;
1154 }
1155 
1156 /**
1157  * @brief Check if device acl is connected.
1158  *
1159  * @return Returns <b>true</b> if device acl is connected;
1160  *         Returns <b>false</b> otherwise.
1161  * @since 6
1162  */
IsAclConnected() const1163 bool BlePeripheralDevice::IsAclConnected() const
1164 {
1165     if (aclConnected_ != BLE_CONNECTION_STATE_DISCONNECTED) {
1166         return true;
1167     } else {
1168         return false;
1169     }
1170 }
1171 
1172 /**
1173  * @brief Check if device acl Encrypted.
1174  *
1175  * @return Returns <b>true</b> if device acl is Encrypted;
1176  *         Returns <b>false</b> otherwise.
1177  * @since 6
1178  */
IsAclEncrypted() const1179 bool BlePeripheralDevice::IsAclEncrypted() const
1180 {
1181     if (aclConnected_ > BLE_CONNECTION_STATE_CONNECTED) {
1182         return true;
1183     } else {
1184         return false;
1185     }
1186 }
1187 
1188 /**
1189  * @brief Check if device was bonded from local.
1190  *
1191  * @return Returns <b>true</b> if device was bonded from local;
1192  *         Returns <b>false</b> otherwise.
1193  * @since 6
1194  */
IsBondedFromLocal() const1195 bool BlePeripheralDevice::IsBondedFromLocal() const
1196 {
1197     return bondFlag_;
1198 }
1199 
1200 /**
1201  * @brief Get acl connection handle.
1202  *
1203  * @return Returns acl connection handle;
1204  * @since 6
1205  */
GetConnectionHandle() const1206 int BlePeripheralDevice::GetConnectionHandle() const
1207 {
1208     return connectionHandle_;
1209 }
1210 
1211 /**
1212  * @brief Get device type.
1213  *
1214  * @return Returns device type.
1215  * @since 6
1216  */
GetDeviceType() const1217 uint8_t BlePeripheralDevice::GetDeviceType() const
1218 {
1219     if (adFlag_ & BLE_ADV_FLAG_BREDR_NOT_SPT) {
1220         return BLE_BT_DEVICE_TYPE_DUMO;
1221     }
1222     return BLE_BT_DEVICE_TYPE_BLE;
1223 }
1224 
1225 /**
1226  * @brief Get advertising flag.
1227  *
1228  * @return Returns advertising flag.
1229  * @since 6
1230  */
GetAdFlag() const1231 uint8_t BlePeripheralDevice::GetAdFlag() const
1232 {
1233     return adFlag_;
1234 }
1235 
1236 /**
1237  * @brief Get paired status.
1238  *
1239  * @return Returns paired status.
1240  * @since 6
1241  */
GetPairedStatus() const1242 uint8_t BlePeripheralDevice::GetPairedStatus() const
1243 {
1244     return pairState_;
1245 }
1246 
1247 /**
1248  * @brief Set paired status.
1249  *
1250  * @param status Paired status.
1251  * @return Returns <b>true</b> if the operation is successful;
1252  *         Returns <b>false</b> otherwise.
1253  * @since 6
1254  */
SetPairedStatus(uint8_t status)1255 bool BlePeripheralDevice::SetPairedStatus(uint8_t status)
1256 {
1257     if (BLE_PAIR_NONE > status || BLE_PAIR_CANCELING < status) {
1258         return false;
1259     }
1260     if (pairState_ == status) {
1261         return true;
1262     }
1263     pairState_ = status;
1264     return true;
1265 }
1266 
1267 /**
1268  * @brief Set alias name.
1269  *
1270  * @param name Device alias name.
1271  * @since 6
1272  */
SetAliasName(const std::string & name)1273 void BlePeripheralDevice::SetAliasName(const std::string &name)
1274 {
1275     aliasName_ = name;
1276 }
1277 
1278 /**
1279  * @brief Get alias name.
1280  *
1281  * @return Returns alias name.
1282  * @since 6
1283  */
GetAliasName() const1284 std::string BlePeripheralDevice::GetAliasName() const
1285 {
1286     return aliasName_;
1287 }
1288 
1289 /**
1290  * @brief Set IO capability.
1291  *
1292  * @param io IO capability
1293  * @since 6
1294  */
SetIoCapability(uint8_t io)1295 void BlePeripheralDevice::SetIoCapability(uint8_t io)
1296 {
1297     ioCapability_ = io;
1298 }
1299 
1300 /**
1301  * @brief Get IO capability.
1302  *
1303  * @return Returns IO capability.
1304  * @since 6
1305  */
GetIoCapability() const1306 uint8_t BlePeripheralDevice::GetIoCapability() const
1307 {
1308     return ioCapability_;
1309 }
1310 
1311 /**
1312  * @brief Set advertising flag.
1313  *
1314  * @param adFlag Advertising flag.
1315  * @since 6
1316  */
SetAdFlag(uint8_t adFlag)1317 void BlePeripheralDevice::SetAdFlag(uint8_t adFlag)
1318 {
1319     adFlag_ = adFlag;
1320 }
1321 
1322 /**
1323  * @brief Set device appearance.
1324  *
1325  * @param device Appearance.
1326  * @since 6
1327  */
SetAppearance(uint16_t appearance)1328 void BlePeripheralDevice::SetAppearance(uint16_t appearance)
1329 {
1330     appearance_ = appearance;
1331     isAppearance_ = true;
1332 }
1333 
1334 /**
1335  * @brief Set manufacturer data.
1336  *
1337  * @param manufacturerData Manufacturer data.
1338  * @since 6
1339  */
SetManufacturerData(std::string manufacturerData)1340 void BlePeripheralDevice::SetManufacturerData(std::string manufacturerData)
1341 {
1342     if (manufacturerData.size() > BLE_UUID_LEN_16) {
1343         uint16_t manufacturerId = uint8_t(manufacturerData[0]) | (uint16_t(manufacturerData[1]) << BLE_ONE_BYTE_LEN);
1344         auto iter = manufacturerData_.find(manufacturerId);
1345         if (iter == manufacturerData_.end()) {
1346             manufacturerData_.insert(std::make_pair(manufacturerId, manufacturerData.substr(BLE_UUID_LEN_16)));
1347         }
1348         isManufacturerData_ = true;
1349     } else {
1350         manufacturerData_.clear();
1351         isManufacturerData_ = false;
1352     }
1353 }
1354 
1355 /**
1356  * @brief Set service data UUID.
1357  *
1358  * @param uuid Service data UUID.
1359  * @since 6
1360  */
SetServiceDataUUID(Uuid uuid,std::string data)1361 void BlePeripheralDevice::SetServiceDataUUID(Uuid uuid, std::string data)
1362 {
1363     isServiceData_ = true;
1364     auto iter = std::find(serviceDataUUIDs_.begin(), serviceDataUUIDs_.end(), uuid);
1365     if (iter == serviceDataUUIDs_.end()) {
1366         serviceDataUUIDs_.push_back(uuid);
1367         serviceData_.push_back(data);
1368     }
1369 }
1370 
1371 /**
1372  * @brief Set service UUID.
1373  *
1374  * @param serviceUUID Service UUID.
1375  * @since 6
1376  */
SetServiceUUID(Uuid serviceUUID)1377 void BlePeripheralDevice::SetServiceUUID(Uuid serviceUUID)
1378 {
1379     isServiceUUID_ = true;
1380     auto iter = std::find(serviceUUIDs_.begin(), serviceUUIDs_.end(), serviceUUID);
1381     if (iter == serviceUUIDs_.end()) {
1382         serviceUUIDs_.push_back(serviceUUID);
1383     }
1384 }
1385 /**
1386  * @brief Set TX power.
1387  *
1388  * @param txPower TX power.
1389  * @since 6
1390  */
SetTXPower(int8_t txPower)1391 void BlePeripheralDevice::SetTXPower(int8_t txPower)
1392 {
1393     isTXPower_ = true;
1394     txPower_ = txPower;
1395 }
1396 /**
1397  * @brief Get peripheral device.
1398  *
1399  * @return Returns peripheral device pointer.
1400  * @since 6
1401  */
GetPeripheralDevice() const1402 BlePeripheralDevice BleScanResultImpl::GetPeripheralDevice() const
1403 {
1404     return peripheralDevice_;
1405 }
1406 
1407 /**
1408  * @brief Set peripheral device.
1409  *
1410  * @param dev Peripheral device.
1411  * @since 6
1412  */
SetPeripheralDevice(const BlePeripheralDevice & dev)1413 void BleScanResultImpl::SetPeripheralDevice(const BlePeripheralDevice &dev)
1414 {
1415     peripheralDevice_ = dev;
1416 }
1417 
1418 /**
1419  * @brief Get advertiser data packet.
1420  *
1421  * @return Returns advertiser data packet.
1422  * @since 6
1423  */
GetPayload() const1424 uint8_t *BlePeripheralDevice::GetPayload() const
1425 {
1426     return payload_;
1427 }
1428 
1429 /**
1430  * @brief Get advertising packet length.
1431  *
1432  * @return Returns advertising packet length.
1433  * @since 6
1434  */
GetPayloadLen() const1435 size_t BlePeripheralDevice::GetPayloadLen() const
1436 {
1437     return payloadLen_;
1438 }
1439 }  // namespace bluetooth
1440 }  // namespace OHOS
1441