> For the complete documentation index, see [llms.txt](https://cavedu.gitbook.io/linkit-7697/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cavedu.gitbook.io/linkit-7697/linkit-7697-arduinoide/kai-fa-zhi-nan/shi-yong-ble-di-gong-hao-lan-ya/lble-api-zhi-nan/lbleperipheral.md).

# LBLEPeripheral

**LBLEPeripheral** 是一個代表在 LinkIt 7697 上面創建 BLE 周邊裝置的單件（Singleton）。開發者可以用這個單件來創造藍牙信標裝置，或是創造可以儲存各種資料特性的周邊裝置。

使用 LBLEPeripheral 可以分成幾個階段：

* 準備各種服務與資料特性：開發者須先設計好自己的周邊裝置會有哪些服務與資料特性，並將這些服務與資料特性定義為全域變數，如下：

  ```
  // Define a simple GATT service with only 1 characteristic
  LBLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214");
  LBLECharacteristicInt switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", LBLE_READ | LBLE_WRITE);
  ```
* 設定周邊裝置：需要將資料特性（Characteristic）加入對應的服務（Service）當中，然後將服務加入 LBLEPeripheral 之中：

  ```
    // Add characteristics into ledService
    ledService.addAttribute(switchCharacteristic);

    // Add service to GATT server (peripheral)
    LBLEPeripheral.addService(ledService);

    // start the GATT server - it is now 
    // available to connect
    LBLEPeripheral.begin();
  ```
* 接著，透過呼叫 **LBLEPeripheral.begin()** 啟動周邊裝置
* 最後，應該呼叫 **LBLEPeripheral.advertise()** 來進行廣播，這樣周圍的主控裝置才能知道此周邊裝置的存在。

### Methods <a href="#lbleperipheral-methods" id="lbleperipheral-methods"></a>

addService()&#x20;

begin()

advertise()&#x20;

advertiseAsBeacon()&#x20;

stopAdvertise()&#x20;

notifyAll()

{% tabs %}
{% tab title="addService() " %}
添加新的 GATT 服務。應該在呼叫 begin() 啟動 GATT 之前調用此 API。當 GATT 啟動之後，此 API 不起作用。

**Syntax**

*LBLEPeripheral.*&#x61;ddService(service);

**Parameters**

service: 要加入此周邊裝置的服務。注意的是，此物件會被周邊裝置的內部儲存結構參照，而不是複製一份，所以此物件必須一直處於有效的狀態，不能夠被摧毀。我們會建議，service 物件應該被定義在全域空間。

**Returns**

none
{% endtab %}

{% tab title="begin()" %}
在設定好 service 之後，可以調用此 API 來啟動周邊裝置上面的 GATT 伺服器。

**Syntax**

*LBLEPeripheral.*&#x62;egin();

**Parameters**

none

**Returns**

none
{% endtab %}

{% tab title="advertise() " %}
開始廣播，將此裝置宣布為可以被連接的裝置。

**Syntax**

*LBLEPeripheral.*&#x61;dvertise(advertisementData);

**Parameters**

advertisementData：一個 LBLEAdvertisementData 的物件，可使用 LBLEAdvertisementData 的各種API來設定要廣播的資料內容。

**Returns**

none
{% endtab %}

{% tab title="advertiseAsBeacon() " %}
開始廣播，將此裝置宣布為不能夠被連接的裝置（比如 iBeacon 信標）。

**Syntax**

*LBLEPeripheral.advertiseAsBeacon*();

**Parameters**

advertisementData: 一個 LBLEAdvertisementData 的物件，可使用 LBLEAdvertisementData 的各種API來設定要廣播的資料內容。

**Returns**

none
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="stopAdvertise()" %}
停止廣播。

**Syntax**

*LBLEPeripheral.*&#x73;topAdvertise();

**Parameters**

none

**Returns**

none
{% endtab %}

{% tab title="notifyAll()" %}
對所有已經連接的主控裝置發出通知。通知的內容是傳入的 characteristic 的值。

**Syntax**

*LBLEPeripheral.*&#x6E;otifyAll(*characterstic*);

**Parameters**

*characterstic:* 一個先前已經透過 service 添加進 LBLEPeriphal 的資料特性。此資料特性的數值會被拿來當作通知的內容。

**Returns**

none
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/linkit-7697/linkit-7697-arduinoide/kai-fa-zhi-nan/shi-yong-ble-di-gong-hao-lan-ya/lble-api-zhi-nan/lbleperipheral.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
