<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/90208/how-to-reconnect-when-disconnection-appear-blehci-conn_failed_to_be_established-pc_ble_driver_py</link><description>Hello, I am using the nordic nRF52840-DK with the package pc-ble-driver-py and the example heart_rate_collector.py. 
 My problem is that sometimes the Bluetooth connection can&amp;#39;t connect to the device with this error : 
 New connection, conn_handle: 0</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 31 Aug 2022 06:24:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/90208/how-to-reconnect-when-disconnection-appear-blehci-conn_failed_to_be_established-pc_ble_driver_py" /><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/384038?ContentTypeID=1</link><pubDate>Wed, 31 Aug 2022 06:24:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9526d2e-57f8-4e15-92f0-9c6dfda6962b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Try replacing the&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="c_cpp"&gt;                raise NordicSemiException(
                    &amp;quot;conn_handle not available. common cause is disconnect after&amp;quot;
                    &amp;quot;starting service_discovery&amp;quot;)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;with&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;break&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Or even better, with something that starts scanning again.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I am not familiar enough with pc-ble-driver-py to tell you exactly what that needs to be.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Try to read from the logs when you start the application if you can deduct what functions that are called to start the scanning.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/383797?ContentTypeID=1</link><pubDate>Tue, 30 Aug 2022 07:34:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6530c84d-3213-4095-bde5-8d0490cc8e94</guid><dc:creator>gaetan14</dc:creator><description>&lt;p&gt;Yes I already try it but&amp;nbsp;&lt;span class="Y2IQFc" lang="en"&gt;when I have the connection pb the program remains in the while true and cannot get out of it&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/383717?ContentTypeID=1</link><pubDate>Mon, 29 Aug 2022 13:39:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:62e5036f-8eb1-4a1e-a6a5-7931b7350fdf</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Did you try to remove the line that raises the exception?&lt;/p&gt;
&lt;p&gt;If not, please try this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/383329?ContentTypeID=1</link><pubDate>Thu, 25 Aug 2022 14:22:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9b7a18d9-3695-4eea-9ab2-933b88f3233e</guid><dc:creator>gaetan14</dc:creator><description>&lt;p&gt;Yes it is exactly what i want to do ! I want to stop the service discovery after a disconnection and start scanning again after the disconnect.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But how can I do this because my code will continue to be execute ?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Gaetan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/381175?ContentTypeID=1</link><pubDate>Thu, 11 Aug 2022 11:17:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8347576e-15b8-49e6-a278-7379b2225bc3</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;My service discovery doesn&amp;#39;t look like that. I see that the latest version on github has the same implementation as you, though.&lt;/p&gt;
&lt;p&gt;FYI, mine looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    @NordicSemiErrorCheck(expected=BLEGattStatusCode.success)
    def service_discovery(self, conn_handle, uuid=None):
        vendor_services = []
        self.driver.ble_gattc_prim_srvc_disc(conn_handle, uuid, 0x0001)

        while True:
            response = self.evt_sync[conn_handle].wait(
                evt=BLEEvtID.gattc_evt_prim_srvc_disc_rsp
            )

            if response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.success:
                for s in response[&amp;quot;services&amp;quot;]:
                    if s.uuid.value == BLEUUID.Standard.unknown:
                        vendor_services.append(s)
                    else:
                        self.db_conns[conn_handle].services.append(s)
            elif response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.attribute_not_found:
                break
            else:
                return response[&amp;quot;status&amp;quot;]

            if response[&amp;quot;services&amp;quot;][-1].end_handle == 0xFFFF:
                break
            else:
                self.driver.ble_gattc_prim_srvc_disc(
                    conn_handle, uuid, response[&amp;quot;services&amp;quot;][-1].end_handle + 1
                )

        for s in vendor_services:
            # Read service handle to obtain full 128-bit UUID.
            self.driver.ble_gattc_read(conn_handle, s.start_handle, 0)
            response = self.evt_sync[conn_handle].wait(evt=BLEEvtID.gattc_evt_read_rsp)
            if response[&amp;quot;status&amp;quot;] != BLEGattStatusCode.success:
                continue

            # Check response length.
            if len(response[&amp;quot;data&amp;quot;]) != 16:
                continue

            # Create UUIDBase object and register it in softdevice
            base = BLEUUIDBase(
                response[&amp;quot;data&amp;quot;][::-1], driver.BLE_UUID_TYPE_VENDOR_BEGIN
            )
            self.driver.ble_vs_uuid_add(base)

            # Rediscover this service.
            self.driver.ble_gattc_prim_srvc_disc(conn_handle, uuid, s.start_handle)
            response = self.evt_sync[conn_handle].wait(
                evt=BLEEvtID.gattc_evt_prim_srvc_disc_rsp
            )
            if response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.success:
                # Assign UUIDBase manually
                # See:
                #  https://github.com/NordicSemiconductor/pc-ble-driver-py/issues/38
                for s in response[&amp;quot;services&amp;quot;]:
                    s.uuid.base = base
                self.db_conns[conn_handle].services.extend(response[&amp;quot;services&amp;quot;])

        for s in self.db_conns[conn_handle].services:
            self.driver.ble_gattc_char_disc(conn_handle, s.start_handle, s.end_handle)
            while True:
                response = self.evt_sync[conn_handle].wait(
                    evt=BLEEvtID.gattc_evt_char_disc_rsp
                )
                if response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.success:
                    for char in response[&amp;quot;characteristics&amp;quot;]:
                        s.char_add(char)
                elif response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.attribute_not_found:
                    break
                else:
                    return response[&amp;quot;status&amp;quot;]

                self.driver.ble_gattc_char_disc(
                    conn_handle,
                    response[&amp;quot;characteristics&amp;quot;][-1].handle_decl + 1,
                    s.end_handle,
                )

            for ch in s.chars:
                self.driver.ble_gattc_desc_disc(
                    conn_handle, ch.handle_value, ch.end_handle
                )
                while True:
                    response = self.evt_sync[conn_handle].wait(
                        evt=BLEEvtID.gattc_evt_desc_disc_rsp
                    )
                    if response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.success:
                        ch.descs.extend(response[&amp;quot;descriptors&amp;quot;])
                    elif response[&amp;quot;status&amp;quot;] == BLEGattStatusCode.attribute_not_found:
                        break
                    else:
                        return response[&amp;quot;status&amp;quot;]

                    if response[&amp;quot;descriptors&amp;quot;][-1].handle == ch.end_handle:
                        break
                    else:
                        self.driver.ble_gattc_desc_disc(
                            conn_handle,
                            response[&amp;quot;descriptors&amp;quot;][-1].handle + 1,
                            ch.end_handle,
                        )
        return BLEGattStatusCode.success&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Either way, if you want to continue after the disconnection, you should remove the exception that is raised. There is no need to continue the service discovery if they are not connected. Instead, you should start scanning again after the disconnect.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/381051?ContentTypeID=1</link><pubDate>Wed, 10 Aug 2022 14:38:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3b0c2f23-ad0f-4ee8-89f5-8b7c963a3721</guid><dc:creator>gaetan14</dc:creator><description>&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1660142046027v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Yes the function&amp;nbsp;&lt;span&gt;on_gap_evt_disconnected() is triggered when it disconnect but as you can see in the picture, a raise NordicSemiException appear and stop the code. Because of that exception the script stop and i can&amp;#39;t try to reconnect.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Do you have an idea to solve this ?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;BR,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Gaetan&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/380953?ContentTypeID=1</link><pubDate>Wed, 10 Aug 2022 08:11:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e9631ad-21fc-42bb-8494-9b85e6ba0f71</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So you have this disconnected callback function:&lt;br /&gt;on_gap_evt_disconnected()&lt;/p&gt;
&lt;p&gt;Is that triggered when you fail to connect? If so, can you try to trigger a new connect from within this function, in addition to only printing&amp;nbsp; &amp;quot;Disconnected: {} {}&amp;quot;?&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/380565?ContentTypeID=1</link><pubDate>Mon, 08 Aug 2022 10:13:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8e5fa92-3883-42bb-afba-3f97db27f74c</guid><dc:creator>gaetan14</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;import sys
import time
import logging
import serial
from queue import Queue, Empty
from pc_ble_driver_py.observers import *

TARGET_DEV_NAME = &amp;quot;Nordic_HRM&amp;quot;
CONNECTIONS = 1
CFG_TAG = 1

global BLEDriver, BLEAdvData, BLEEvtID, BLEAdapter, BLEEnableParams, BLEGapTimeoutSrc, BLEUUID
from pc_ble_driver_py import config

config.__conn_ic_id__ = &amp;quot;NRF52&amp;quot;

from pc_ble_driver_py.ble_driver import (
    BLEDriver,
    BLEAdvData,
    BLEEvtID,
    BLEEnableParams,
    BLEGapTimeoutSrc,
    BLEUUID,
    BLEUUIDBase,
    BLEGapScanParams,
    BLEConfigCommon,
    BLEConfig,
    BLEConfigConnGatt,
)

from pc_ble_driver_py.ble_adapter import BLEAdapter, BLEGapConnParams

global nrf_sd_ble_api_ver
nrf_sd_ble_api_ver = config.sd_api_ver_get()


class HRCollector(BLEDriverObserver, BLEAdapterObserver):
    def __init__(self, adapter):
        super(HRCollector, self).__init__()
        self.adapter = adapter
        self.conn_q = Queue()
        self.adapter.observer_register(self)
        self.adapter.driver.observer_register(self)
        self.adapter.default_mtu = 250

    def open(self):
        self.adapter.driver.open()
        gatt_cfg = BLEConfigConnGatt()
        gatt_cfg.att_mtu = self.adapter.default_mtu
        gatt_cfg.tag = CFG_TAG
        self.adapter.driver.ble_cfg_set(BLEConfig.conn_gatt, gatt_cfg)

        self.adapter.driver.ble_enable()

    def close(self):
        self.adapter.driver.close()

    def connect_and_discover(self):
        scan_duration = 15
        params = BLEGapScanParams(interval_ms=200, window_ms=150, timeout_s=scan_duration)

        self.adapter.driver.ble_gap_scan_start(scan_params=params)

        try:
            new_conn = self.conn_q.get(timeout=scan_duration)
            self.adapter.service_discovery(new_conn)

            self.adapter.enable_notification(new_conn, BLEUUID(BLEUUID.Standard.battery_level))
            self.adapter.enable_notification(new_conn, BLEUUID(BLEUUID.Standard.heart_rate))
            return new_conn
        except Empty:
            print(f&amp;quot;No heart rate collector advertising with name {TARGET_DEV_NAME} found.&amp;quot;)
            return None

    def on_gap_evt_connected(
            self, ble_driver, conn_handle, peer_addr, role, conn_params
    ):
        print(&amp;quot;New connection: {}&amp;quot;.format(conn_handle))
        self.conn_q.put(conn_handle)

    def on_gap_evt_disconnected(self, ble_driver, conn_handle, reason):
        print(&amp;quot;Disconnected: {} {}&amp;quot;.format(conn_handle, reason))

    def on_gap_evt_adv_report(
            self, ble_driver, conn_handle, peer_addr, rssi, adv_type, adv_data
    ):
        if BLEAdvData.Types.complete_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.complete_local_name]

        elif BLEAdvData.Types.short_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.short_local_name]

        else:
            return

        dev_name = &amp;quot;&amp;quot;.join(chr(e) for e in dev_name_list)
        address_string = &amp;quot;&amp;quot;.join(&amp;quot;{0:02X}&amp;quot;.format(b) for b in peer_addr.addr)
        print(
            &amp;quot;Received advertisment report, address: 0x{}, device_name: {}&amp;quot;.format(
                address_string, dev_name
            )
        )

        if dev_name == TARGET_DEV_NAME:
            conn_params = BLEGapConnParams(min_conn_interval_ms=100, max_conn_interval_ms=200, conn_sup_timeout_ms=4000, slave_latency=0)
            self.adapter.connect(peer_addr, conn_params=conn_params, tag=CFG_TAG)

    def on_notification(self, ble_adapter, conn_handle, uuid, data):
        if len(data) &amp;gt; 32:
            data = &amp;quot;({}...)&amp;quot;.format(data[0:10])
        print(&amp;quot;Connection: {}, {} = {}&amp;quot;.format(conn_handle, uuid, data))


def main():
    selected_serial_port = &amp;quot;COM60&amp;quot;
    print(&amp;quot;Serial port used: {}&amp;quot;.format(selected_serial_port))
    driver = BLEDriver(
        serial_port=selected_serial_port, auto_flash=False, baud_rate=1000000, log_severity_level=&amp;quot;info&amp;quot;
    )

    adapter = BLEAdapter(driver)
    collector = HRCollector(adapter)
    collector.open()
    conn = collector.connect_and_discover()

    if conn is not None:
        time.sleep(15)
    if conn is None:
        print(&amp;quot;disconnect&amp;quot;)
        collector.close()
    collector.close()


if __name__ == &amp;quot;__main__&amp;quot;:
    main()&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In the first place, I connect the same way as the example heart_rate_collector.py. But unfortunately this script seems to have some issue with the connection&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/379828?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2022 09:00:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1180c37d-c039-4645-9b11-7439125b6049</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Again, pc_ble_driver_py is not my strong side.&lt;/p&gt;
[quote user="gaetan14"] how to reconnect to the device after a random disconnection in my scrip[/quote]
&lt;p&gt;How do you connect in the first place? I assume that it is some API that you have already used to connect to your device? Perhaps you can share with me what your current script looks like?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/379753?ContentTypeID=1</link><pubDate>Tue, 02 Aug 2022 14:37:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a0c23ba0-43fd-456b-a164-487a648b7845</guid><dc:creator>gaetan14</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I have check but the disconnection is not because of the range. Indeed, the driver works most of the time.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Also&amp;nbsp;I already check if the peripherals keep advertising and it doesn&amp;#39;t seem to be related because they keep advertising.&lt;/p&gt;
&lt;p&gt;Concerning the python script, it doesn&amp;#39;t crash when I disconnect, It crash when a random disconnect appear.&lt;/p&gt;
&lt;p&gt;My question is this one, how to reconnect to the device after a random disconnection in my script.&lt;/p&gt;
&lt;p&gt;Gaetan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to reconnect when Disconnection appear : BLEHci.conn_failed_to_be_established [pc_ble_driver_py]</title><link>https://devzone.nordicsemi.com/thread/378126?ContentTypeID=1</link><pubDate>Thu, 21 Jul 2022 13:45:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4120889d-84ad-4e4f-a4df-0956c3739def</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Please note that I am not that familiar with pc_ble_driver_py. I just know the SDK, but the principles are the same.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When it disconnects, is it because of range? Because in that case, it may not be able to reconnect either, if the scanner is not able to &amp;quot;hear&amp;quot; the advertisements from the peripheral. Also, make sure that the peripheral is still advertising after the disconnect events.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Why does the python script stop when you disconnect? Again, not that familiar with the pc_ble_driver_py, but when seeing it as a normal BLE application, can&amp;#39;t you just keep scanning for the device whenever it fails to connect?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>