• 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(const std::string & deviceId)78 void BleScanFilterImpl::SetDeviceId(const std::string &deviceId)
79 {
80     deviceId_ = deviceId;
81 }
82 
GetDeviceId() const83 std::string BleScanFilterImpl::GetDeviceId() const
84 {
85     return deviceId_;
86 }
87 
SetName(const std::string & name)88 void BleScanFilterImpl::SetName(const 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(const std::vector<uint8_t> & serviceData)162 void BleScanFilterImpl::SetServiceData(const 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(const std::vector<uint8_t> & serviceDataMask)172 void BleScanFilterImpl::SetServiceDataMask(const 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(const std::vector<uint8_t> & manufactureData)192 void BleScanFilterImpl::SetManufactureData(const 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(const std::vector<uint8_t> & manufactureDataMask)202 void BleScanFilterImpl::SetManufactureDataMask(const 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     // A shortened name only contain 26 contiguous characters from the beginning of the full name.
664     cdata[0] = DEVICE_NAME_MAX_LEN + 1;
665     cdata[1] = BLE_AD_TYPE_NAME_SHORT;  /// 0x08
666     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name.substr(0, DEVICE_NAME_MAX_LEN));
667 }
668 
669 /**
670  * @brief Set advertiser data short name.
671  *
672  * @param name Bluetooth device name.
673  * @since 6
674  */
SetShortName(const std::string & name)675 void BleAdvertiserDataImpl::SetShortName(const std::string &name)
676 {
677     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
678     cdata[0] = name.length() + 1;
679     cdata[1] = BLE_AD_TYPE_NAME_CMPL;  /// 0x09
680     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
681 }
682 
BlePeripheralDevice()683 BlePeripheralDevice::BlePeripheralDevice()
684     : manufacturerData_(),
685       name_(),
686       serviceUUIDs_(),
687       txPower_(BLE_ADDR_TYPE_RANDOM),
688       serviceData_(),
689       serviceDataUUIDs_(),
690       pairState_(BLE_PAIR_NONE),
691       ioCapability_(BLE_IO_CAP_NONE),
692       aliasName_()
693 {
694     manufacturerData_.clear();
695     serviceDataUUIDs_.clear();
696     serviceData_.clear();
697     serviceUUIDs_.clear();
698 }
699 
~BlePeripheralDevice()700 BlePeripheralDevice::~BlePeripheralDevice()
701 {
702     manufacturerData_.clear();
703     serviceUUIDs_.clear();
704     serviceData_.clear();
705     serviceDataUUIDs_.clear();
706 }
707 
708 /**
709  * @brief Get device address.
710  *
711  * @return Returns device address.
712  * @since 6
713  */
GetRawAddress() const714 RawAddress BlePeripheralDevice::GetRawAddress() const
715 {
716     return address_;
717 }
718 
719 /**
720  * @brief Get device appearance.
721  *
722  * @return Returns the device appearance.
723  * @since 6
724  */
GetAppearance() const725 uint16_t BlePeripheralDevice::GetAppearance() const
726 {
727     return appearance_;
728 }
729 
730 /**
731  * @brief Get the manufacturer data.
732  *
733  * @return Returns the manufacturer data.
734  * @since 6
735  */
GetManufacturerData() const736 std::map<uint16_t, std::string> BlePeripheralDevice::GetManufacturerData() const
737 {
738     return manufacturerData_;
739 }
740 
741 /**
742  * @brief the device name.
743  *
744  * @return Returns device Name.
745  * @since 6
746  */
GetName() const747 std::string BlePeripheralDevice::GetName() const
748 {
749     return name_;
750 }
751 
752 /**
753  * @brief Get device RSSI.
754  *
755  * @return Returns device RSSI.
756  * @since 6
757  */
GetRSSI() const758 int8_t BlePeripheralDevice::GetRSSI() const
759 {
760     return rssi_;
761 }
762 
763 /**
764  * @brief Get service data.
765  *
766  * @return Returns service data.
767  * @since 6
768  */
GetServiceData() const769 std::vector<std::string> BlePeripheralDevice::GetServiceData() const
770 {
771     return serviceData_;
772 }
773 
774 /**
775  * @brief Get Service Data.
776  *
777  * @param index Service data index.
778  * @return Returns service data.
779  * @since 6
780  */
GetServiceData(int index) const781 std::string BlePeripheralDevice::GetServiceData(int index) const
782 {
783     return serviceData_.empty() ? "" :
784         (static_cast<size_t>(index) < serviceData_.size() ? serviceData_[index] : "");
785 }
786 
787 /**
788  * @brief Get service data UUID.
789  *
790  * @return Returns service data UUID.
791  * @since 6
792  */
GetServiceDataUUID() const793 std::vector<Uuid> BlePeripheralDevice::GetServiceDataUUID() const
794 {
795     return serviceDataUUIDs_;
796 }
797 
798 /**
799  * @brief Get service data UUID.
800  *
801  * @param index Service data index.
802  * @return Returns service data UUID.
803  * @since 6
804  */
GetServiceDataUUID(int index) const805 Uuid BlePeripheralDevice::GetServiceDataUUID(int index) const
806 {
807     Uuid uuid {};
808     return serviceDataUUIDs_.empty() ? uuid : serviceDataUUIDs_[index];
809 }
810 
811 /**
812  * @brief Get the service UUID.
813  *
814  * @return Returns service UUID.
815  * @since 6
816  */
GetServiceUUID() const817 std::vector<Uuid> BlePeripheralDevice::GetServiceUUID() const
818 {
819     return serviceUUIDs_;
820 }
821 
822 /**
823  * @brief Get service UUID.
824  *
825  * @param index Service UUID index.
826  * @return Return service UUID.
827  * @since 6
828  */
GetServiceUUID(int index) const829 Uuid BlePeripheralDevice::GetServiceUUID(int index) const
830 {
831     Uuid uuid {};
832     return serviceUUIDs_.empty() ? uuid : serviceUUIDs_[index];
833 }
834 
835 /**
836  * @brief Get address type.
837  *
838  * @return Returns address type.
839  * @since 6
840  */
GetAddressType() const841 int BlePeripheralDevice::GetAddressType() const
842 {
843     return addressType_;
844 }
845 
846 /**
847  * @brief Set address type.
848  *
849  * @param type Address type.
850  * @since 6
851  */
SetAddressType(int type)852 void BlePeripheralDevice::SetAddressType(int type)
853 {
854     addressType_ = type;
855 }
856 
857 /**
858  * @brief Check if manufacturer data is included.
859  *
860  * @return Returns <b>true</b> if manufacturer data is included;
861  *         Returns <b>false</b> otherwise.
862  * @since 6
863  */
IsManufacturerData() const864 bool BlePeripheralDevice::IsManufacturerData() const
865 {
866     return isManufacturerData_;
867 }
868 
869 /**
870  * @brief Check if the device RSSI is included.
871  *
872  * @return Returns <b>true</b> if include device rssi;
873  *         Returns <b>false</b> otherwise.
874  * @since 6
875  */
IsRSSI() const876 bool BlePeripheralDevice::IsRSSI() const
877 {
878     return isRSSI_;
879 }
880 
881 /**
882  * @brief Check if service data is included.
883  *
884  * @return Returns <b>true</b> if include service data;
885  *         Returns <b>false</b> otherwise.
886  * @since 6
887  */
IsServiceData() const888 bool BlePeripheralDevice::IsServiceData() const
889 {
890     return isServiceData_;
891 }
892 
893 /**
894  * @brief Check if the service UUID is included.
895  *
896  * @return Returns <b>true</b> if the service UUID is included;
897  *         Returns <b>false</b> otherwise.
898  * @since 6
899  */
IsServiceUUID() const900 bool BlePeripheralDevice::IsServiceUUID() const
901 {
902     return isServiceUUID_;
903 }
904 
IsName(void) const905 bool BlePeripheralDevice::IsName(void) const
906 {
907     return isName_;
908 }
909 
910 /**
911  * @brief Set device address.
912  *
913  * @param address device address.
914  * @since 6
915  */
SetAddress(const RawAddress & address)916 void BlePeripheralDevice::SetAddress(const RawAddress &address)
917 {
918     address_ = address;
919 }
920 
921 /**
922  * @brief Set RSSI value.
923  *
924  * @param RSSI value.
925  * @since 6
926  */
SetRSSI(int8_t rssi)927 void BlePeripheralDevice::SetRSSI(int8_t rssi)
928 {
929     rssi_ = rssi;
930     isRSSI_ = true;
931 }
932 /**
933  * @brief Check whether device is connectable.
934  *
935  * @param [in] rssi value.
936  * return Returns <b>true</b> if device is connectable.
937  *        Returns <b>false</b> otherwisee.
938  */
IsConnectable() const939 bool BlePeripheralDevice::IsConnectable() const
940 {
941     return connectable_;
942 }
943 /**
944  * @brief Sets whether the peer device is connectable.
945  *
946  * @param peer device's connectable.
947  */
SetConnectable(bool connectable)948 void BlePeripheralDevice::SetConnectable(bool connectable)
949 {
950     connectable_ = connectable;
951 }
952 /**
953  * @brief Parse advertisement packets.
954  *
955  * @param payload Advertisement packet.
956  * @param total_len Advertisement packet length.
957  * @since 6
958  */
ParseAdvertiserment(BlePeripheralDeviceParseAdvData & parseAdvData)959 void BlePeripheralDevice::ParseAdvertiserment(BlePeripheralDeviceParseAdvData &parseAdvData)
960 {
961     size_t sizeConsumed = 0;
962     bool finished = false;
963     size_t totalLength = parseAdvData.length;
964     payload_ = parseAdvData.payload;
965     payloadLen_ = parseAdvData.length;
966 
967     while (!finished) {
968         size_t length = *parseAdvData.payload;
969         sizeConsumed += 1 + length;
970         if (sizeConsumed > totalLength) {
971             break;
972         }
973         parseAdvData.payload++;
974 
975         if (length != 0) {
976             uint8_t advType = *parseAdvData.payload;
977             parseAdvData.payload++;
978             length--;
979             parseAdvData.length = length;
980             BuildAdvertiserData(advType, parseAdvData);
981             parseAdvData.payload += length;
982         }
983         if (sizeConsumed >= totalLength) {
984             finished = true;
985         }
986     }
987 }
988 
BuildAdvertiserData(uint8_t advType,BlePeripheralDeviceParseAdvData & parseAdvData)989 void BlePeripheralDevice::BuildAdvertiserData(uint8_t advType, BlePeripheralDeviceParseAdvData &parseAdvData)
990 {
991     switch (advType) {
992         case BLE_AD_TYPE_NAME_CMPL:   /// Data Type: 0x09
993         case BLE_AD_TYPE_NAME_SHORT:  /// Data Type: 0x08
994             SetName(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
995             break;
996         case BLE_AD_TYPE_TX_PWR:  /// Data Type: 0x0A
997             SetTXPower(*parseAdvData.payload);
998             break;
999         case BLE_AD_TYPE_APPEARANCE:  /// Data Type: 0x19
1000             SetAppearance(*reinterpret_cast<uint16_t *>(parseAdvData.payload));
1001             break;
1002         case BLE_AD_TYPE_FLAG:  /// Data Type: 0x01
1003             SetAdFlag(*parseAdvData.payload);
1004             break;
1005         case BLE_AD_TYPE_16SRV_CMPL:
1006         case BLE_AD_TYPE_16SRV_PART:  /// Data Type: 0x02
1007             SetServiceUUID16Bits(parseAdvData);
1008             break;
1009         case BLE_AD_TYPE_32SRV_CMPL:
1010         case BLE_AD_TYPE_32SRV_PART:  /// Data Type: 0x04
1011             SetServiceUUID32Bits(parseAdvData);
1012             break;
1013         case BLE_AD_TYPE_128SRV_CMPL:  /// Data Type: 0x07
1014         case BLE_AD_TYPE_128SRV_PART:  /// Data Type: 0x06
1015             SetServiceUUID128Bits(parseAdvData);
1016             break;
1017         case BLE_AD_MANUFACTURER_SPECIFIC_TYPE:
1018             SetManufacturerData(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
1019             break;
1020         case BLE_AD_TYPE_SERVICE_DATA:  /// Data Type: 0x16
1021             SetServiceDataUUID16Bits(parseAdvData);
1022             break;
1023         case BLE_AD_TYPE_32SERVICE_DATA:  /// Data Type: 0x20
1024             SetServiceDataUUID32Bits(parseAdvData);
1025             break;
1026         case BLE_AD_TYPE_128SERVICE_DATA:  /// Data Type: 0x21
1027             SetServiceDataUUID128Bits(parseAdvData);
1028             break;
1029         default:
1030             break;
1031     }
1032 }
1033 
SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1034 void BlePeripheralDevice::SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1035 {
1036     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_16; ++var) {
1037         SetServiceUUID(
1038             Uuid::ConvertFrom16Bits(*reinterpret_cast<uint16_t *>(parseAdvData.payload + var * BLE_UUID_LEN_16)));
1039     }
1040 }
1041 
SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1042 void BlePeripheralDevice::SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1043 {
1044     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_32; ++var) {
1045         SetServiceUUID(
1046             Uuid::ConvertFrom32Bits(*reinterpret_cast<uint32_t *>(parseAdvData.payload + var * BLE_UUID_LEN_32)));
1047     }
1048 }
1049 
SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData & parseAdvData)1050 void BlePeripheralDevice::SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData &parseAdvData)
1051 {
1052     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_128; ++var) {
1053         std::array<uint8_t, BLE_UUID_LEN_128> data = {};
1054         for (int i = 0; i < BLE_UUID_LEN_128; i++) {
1055             data[i] = *(parseAdvData.payload + var * BLE_UUID_LEN_128 + i);
1056         }
1057         SetServiceUUID(Uuid::ConvertFromBytesLE(data.data()));
1058     }
1059 }
1060 
SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1061 void BlePeripheralDevice::SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1062 {
1063     if (parseAdvData.length < BLE_UUID_LEN_16) {
1064         return;
1065     }
1066     uint16_t uuid = *(reinterpret_cast<uint16_t *>(parseAdvData.payload));
1067     std::string data = "";
1068     if (parseAdvData.length > BLE_UUID_LEN_16) {
1069         data = std::string(
1070             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_16), parseAdvData.length - BLE_UUID_LEN_16);
1071     }
1072     SetServiceDataUUID(Uuid::ConvertFrom16Bits(uuid), data);
1073 }
1074 
SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1075 void BlePeripheralDevice::SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1076 {
1077     if (parseAdvData.length < BLE_UUID_LEN_32) {
1078         return;
1079     }
1080     uint32_t uuid = *(reinterpret_cast<uint32_t *>(parseAdvData.payload));
1081     std::string data = "";
1082     if (parseAdvData.length > BLE_UUID_LEN_32) {
1083         data = std::string(
1084             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_32), parseAdvData.length - BLE_UUID_LEN_32);
1085     }
1086     SetServiceDataUUID(Uuid::ConvertFrom32Bits(uuid), data);
1087 }
1088 
SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1089 void BlePeripheralDevice::SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1090 {
1091     if (parseAdvData.length < BLE_UUID_LEN_128) {
1092         return;
1093     }
1094     std::string data = "";
1095     if (parseAdvData.length > BLE_UUID_LEN_128) {
1096         data = std::string(
1097             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_128), parseAdvData.length - BLE_UUID_LEN_128);
1098     }
1099     SetServiceDataUUID(Uuid::ConvertFromBytesLE(parseAdvData.payload), data);
1100 }
1101 
1102 /**
1103  * @brief Set device name.
1104  *
1105  * @param name Device name.
1106  * @since 6
1107  */
SetName(const std::string & name)1108 void BlePeripheralDevice::SetName(const std::string &name)
1109 {
1110     name_ = name;
1111     isName_ = true;
1112 }
1113 
1114 /**
1115  * @brief Set device roles.
1116  *
1117  * @param roles Device roles.
1118  * @since 6
1119  */
SetRoles(uint8_t roles)1120 void BlePeripheralDevice::SetRoles(uint8_t roles)
1121 {
1122     roles_ = roles;
1123 }
1124 
1125 /**
1126  * @brief Set bonded from local.
1127  *
1128  * @param flag Advertiser flag.
1129  * @since 6
1130  */
SetBondedFromLocal(bool flag)1131 void BlePeripheralDevice::SetBondedFromLocal(bool flag)
1132 {
1133     bondFlag_ = flag;
1134 }
1135 
1136 /**
1137  * @brief Set acl connection state.
1138  *
1139  * @param connectState Acl connection state.
1140  * @since 6
1141  */
SetAclConnectState(int connectState)1142 void BlePeripheralDevice::SetAclConnectState(int connectState)
1143 {
1144     aclConnected_ = connectState;
1145 }
1146 
1147 /**
1148  * @brief Set acl connection handle.
1149  *
1150  * @param handle Acl connection handle.
1151  * @since 6
1152  */
SetConnectionHandle(int handle)1153 void BlePeripheralDevice::SetConnectionHandle(int handle)
1154 {
1155     connectionHandle_ = handle;
1156 }
1157 
1158 /**
1159  * @brief Check if device acl is connected.
1160  *
1161  * @return Returns <b>true</b> if device acl is connected;
1162  *         Returns <b>false</b> otherwise.
1163  * @since 6
1164  */
IsAclConnected() const1165 bool BlePeripheralDevice::IsAclConnected() const
1166 {
1167     if (aclConnected_ != BLE_CONNECTION_STATE_DISCONNECTED) {
1168         return true;
1169     } else {
1170         return false;
1171     }
1172 }
1173 
1174 /**
1175  * @brief Check if device acl Encrypted.
1176  *
1177  * @return Returns <b>true</b> if device acl is Encrypted;
1178  *         Returns <b>false</b> otherwise.
1179  * @since 6
1180  */
IsAclEncrypted() const1181 bool BlePeripheralDevice::IsAclEncrypted() const
1182 {
1183     if (aclConnected_ > BLE_CONNECTION_STATE_CONNECTED) {
1184         return true;
1185     } else {
1186         return false;
1187     }
1188 }
1189 
1190 /**
1191  * @brief Check if device was bonded from local.
1192  *
1193  * @return Returns <b>true</b> if device was bonded from local;
1194  *         Returns <b>false</b> otherwise.
1195  * @since 6
1196  */
IsBondedFromLocal() const1197 bool BlePeripheralDevice::IsBondedFromLocal() const
1198 {
1199     return bondFlag_;
1200 }
1201 
1202 /**
1203  * @brief Get acl connection handle.
1204  *
1205  * @return Returns acl connection handle;
1206  * @since 6
1207  */
GetConnectionHandle() const1208 int BlePeripheralDevice::GetConnectionHandle() const
1209 {
1210     return connectionHandle_;
1211 }
1212 
1213 /**
1214  * @brief Get device type.
1215  *
1216  * @return Returns device type.
1217  * @since 6
1218  */
GetDeviceType() const1219 uint8_t BlePeripheralDevice::GetDeviceType() const
1220 {
1221     if ((adFlag_ & BLE_ADV_FLAG_BREDR_NOT_SPT) > 0) {
1222         return BLE_BT_DEVICE_TYPE_DUMO;
1223     }
1224     return BLE_BT_DEVICE_TYPE_BLE;
1225 }
1226 
1227 /**
1228  * @brief Get advertising flag.
1229  *
1230  * @return Returns advertising flag.
1231  * @since 6
1232  */
GetAdFlag() const1233 uint8_t BlePeripheralDevice::GetAdFlag() const
1234 {
1235     return adFlag_;
1236 }
1237 
1238 /**
1239  * @brief Get paired status.
1240  *
1241  * @return Returns paired status.
1242  * @since 6
1243  */
GetPairedStatus() const1244 uint8_t BlePeripheralDevice::GetPairedStatus() const
1245 {
1246     return pairState_;
1247 }
1248 
1249 /**
1250  * @brief Set paired status.
1251  *
1252  * @param status Paired status.
1253  * @return Returns <b>true</b> if the operation is successful;
1254  *         Returns <b>false</b> otherwise.
1255  * @since 6
1256  */
SetPairedStatus(uint8_t status)1257 bool BlePeripheralDevice::SetPairedStatus(uint8_t status)
1258 {
1259     if (BLE_PAIR_CANCELING < status || status < BLE_PAIR_NONE) {
1260         return false;
1261     }
1262     if (pairState_ == status) {
1263         return true;
1264     }
1265     pairState_ = status;
1266     return true;
1267 }
1268 
1269 /**
1270  * @brief Set alias name.
1271  *
1272  * @param name Device alias name.
1273  * @since 6
1274  */
SetAliasName(const std::string & name)1275 void BlePeripheralDevice::SetAliasName(const std::string &name)
1276 {
1277     aliasName_ = name;
1278 }
1279 
1280 /**
1281  * @brief Get alias name.
1282  *
1283  * @return Returns alias name.
1284  * @since 6
1285  */
GetAliasName() const1286 std::string BlePeripheralDevice::GetAliasName() const
1287 {
1288     return aliasName_;
1289 }
1290 
1291 /**
1292  * @brief Set IO capability.
1293  *
1294  * @param io IO capability
1295  * @since 6
1296  */
SetIoCapability(uint8_t io)1297 void BlePeripheralDevice::SetIoCapability(uint8_t io)
1298 {
1299     ioCapability_ = io;
1300 }
1301 
1302 /**
1303  * @brief Get IO capability.
1304  *
1305  * @return Returns IO capability.
1306  * @since 6
1307  */
GetIoCapability() const1308 uint8_t BlePeripheralDevice::GetIoCapability() const
1309 {
1310     return ioCapability_;
1311 }
1312 
1313 /**
1314  * @brief Set advertising flag.
1315  *
1316  * @param adFlag Advertising flag.
1317  * @since 6
1318  */
SetAdFlag(uint8_t adFlag)1319 void BlePeripheralDevice::SetAdFlag(uint8_t adFlag)
1320 {
1321     adFlag_ = adFlag;
1322 }
1323 
1324 /**
1325  * @brief Set device appearance.
1326  *
1327  * @param device Appearance.
1328  * @since 6
1329  */
SetAppearance(uint16_t appearance)1330 void BlePeripheralDevice::SetAppearance(uint16_t appearance)
1331 {
1332     appearance_ = appearance;
1333     isAppearance_ = true;
1334 }
1335 
1336 /**
1337  * @brief Set manufacturer data.
1338  *
1339  * @param manufacturerData Manufacturer data.
1340  * @since 6
1341  */
SetManufacturerData(std::string manufacturerData)1342 void BlePeripheralDevice::SetManufacturerData(std::string manufacturerData)
1343 {
1344     if (manufacturerData.size() > BLE_UUID_LEN_16) {
1345         uint16_t manufacturerId = uint8_t(manufacturerData[0]) | (uint16_t(manufacturerData[1]) << BLE_ONE_BYTE_LEN);
1346         std::map<uint16_t, std::string>::const_iterator iter = manufacturerData_.find(manufacturerId);
1347         if (iter == manufacturerData_.cend()) {
1348             manufacturerData_.insert(std::make_pair(manufacturerId, manufacturerData.substr(BLE_UUID_LEN_16)));
1349         }
1350         isManufacturerData_ = true;
1351     } else {
1352         manufacturerData_.clear();
1353         isManufacturerData_ = false;
1354     }
1355 }
1356 
1357 /**
1358  * @brief Set service data UUID.
1359  *
1360  * @param uuid Service data UUID.
1361  * @since 6
1362  */
SetServiceDataUUID(Uuid uuid,std::string data)1363 void BlePeripheralDevice::SetServiceDataUUID(Uuid uuid, std::string data)
1364 {
1365     isServiceData_ = true;
1366     auto iter = std::find(serviceDataUUIDs_.begin(), serviceDataUUIDs_.end(), uuid);
1367     if (iter == serviceDataUUIDs_.end()) {
1368         serviceDataUUIDs_.push_back(uuid);
1369         serviceData_.push_back(data);
1370     }
1371 }
1372 
1373 /**
1374  * @brief Set service UUID.
1375  *
1376  * @param serviceUUID Service UUID.
1377  * @since 6
1378  */
SetServiceUUID(Uuid serviceUUID)1379 void BlePeripheralDevice::SetServiceUUID(Uuid serviceUUID)
1380 {
1381     isServiceUUID_ = true;
1382     auto iter = std::find(serviceUUIDs_.begin(), serviceUUIDs_.end(), serviceUUID);
1383     if (iter == serviceUUIDs_.end()) {
1384         serviceUUIDs_.push_back(serviceUUID);
1385     }
1386 }
1387 /**
1388  * @brief Set TX power.
1389  *
1390  * @param txPower TX power.
1391  * @since 6
1392  */
SetTXPower(int8_t txPower)1393 void BlePeripheralDevice::SetTXPower(int8_t txPower)
1394 {
1395     isTXPower_ = true;
1396     txPower_ = txPower;
1397 }
1398 /**
1399  * @brief Get peripheral device.
1400  *
1401  * @return Returns peripheral device pointer.
1402  * @since 6
1403  */
GetPeripheralDevice() const1404 BlePeripheralDevice BleScanResultImpl::GetPeripheralDevice() const
1405 {
1406     return peripheralDevice_;
1407 }
1408 
1409 /**
1410  * @brief Set peripheral device.
1411  *
1412  * @param dev Peripheral device.
1413  * @since 6
1414  */
SetPeripheralDevice(const BlePeripheralDevice & dev)1415 void BleScanResultImpl::SetPeripheralDevice(const BlePeripheralDevice &dev)
1416 {
1417     peripheralDevice_ = dev;
1418 }
1419 
1420 /**
1421  * @brief Get advertiser data packet.
1422  *
1423  * @return Returns advertiser data packet.
1424  * @since 6
1425  */
GetPayload() const1426 uint8_t *BlePeripheralDevice::GetPayload() const
1427 {
1428     return payload_;
1429 }
1430 
1431 /**
1432  * @brief Get advertising packet length.
1433  *
1434  * @return Returns advertising packet length.
1435  * @since 6
1436  */
GetPayloadLen() const1437 size_t BlePeripheralDevice::GetPayloadLen() const
1438 {
1439     return payloadLen_;
1440 }
1441 }  // namespace bluetooth
1442 }  // namespace OHOS
1443