# 如何讓 AMB23 的 BLE範例 自動重新Scan裝置

**URL:** <https://forum.amebaiot.com/t/amb23-ble-scan/2172>\
**Category:** BT\
**Tags:** 中文, rtl872xd\
**Created:** [August 23, 2023, 2:26am UTC](https://forum.amebaiot.com/t/amb23-ble-scan/2172 "2023-08-23T02:26:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thLU](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@thLU](https://forum.amebaiot.com/u/thLU)\
**Post date:** [August 23, 2023, 2:26am UTC](https://forum.amebaiot.com/t/amb23-ble-scan/2172/1 "2023-08-23T02:26:34Z")

</div>

請問在 BLEUartServer 和 BLEUartClient 的範例程式碼中，只有一次互相連線的內容，即只會scan一次，找到符合條件之裝置並連線後，如果裝置間斷掉連線，裝置互相就不會再進行第二次或更多次的自動scan。

請問是否有辦法讓裝置在斷線後，自動重新搜尋裝置以連線呢？謝謝。

---

<div class="post-metadata">

**Author:** ![dakamaster](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/dakamaster/32/1744_2.png) [@dakamaster](https://forum.amebaiot.com/u/dakamaster)\
**Post date:** [August 25, 2023, 1:05am UTC](https://forum.amebaiot.com/t/amb23-ble-scan/2172/2 "2023-08-25T01:05:41Z")

</div>

@thLU 您好,

關於您的問題我做了如下測試

1. 在兩個 AMB23 開發板中分別運行 BLE UART Client [`CENTRAL`], BLE UART Service [`PERIPHERAL`] 的範例

2. 先運行 BLE UART Service 進行 BLE `PERIPHERAL` 端的advertisement, 再運行 BLE UART Client 進行`CENTRAL` 端的 Scan, Discover 和連接

3. 斷電運行BLE UART Client `CENTRAL` 的 AMB23 幾秒之後， BLE UART Services 端的log截圖顯示 `PERIPHERAL` 重新自動開始 advertisement（出現紅色 log 然後藍色log）。證明斷開后自動連接的機制存在，但事實上`PERIPHERAL`方向上的。再次打開BLE UART Client `CENTRAL` 的 AMB23后會顯示重新連接到（詳情見下圖）  

* * *

但是驗證反方向斷電 BLE `PERIPHERAL` 時，`CENTRAL`并不會 rescan。這是因爲底層程式碼沒有寫入這個機制。

所以想先請問您，上述的第一種重連符合您的專案需求嗎？

謝謝

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [August 25, 2023, 2:20am UTC](https://forum.amebaiot.com/t/amb23-ble-scan/2172/3 "2023-08-25T02:20:44Z")

</div>

可以在loop里面跑

---

<div class="post-metadata">

**Author:** ![thLU](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@thLU](https://forum.amebaiot.com/u/thLU)\
**Post date:** [August 25, 2023, 8:43am UTC](https://forum.amebaiot.com/t/amb23-ble-scan/2172/4 "2023-08-25T08:43:33Z")

</div>

@dakamaster 您好，

首先感謝您的回覆，目前關於第一個重連部分，我需要一些時間去進行確認及資訊的消化。  
我這邊遇到的情況因為正好屬於第二個central不會rescan的情況，所以可能需要些時間去釐清更多資訊，再看看是否有其他的解決辦法。

若後續有更新一步的進展或問題，有機會會再來進行更多的討論，謝謝。
