h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
713 stars 149 forks source link

NRA connection on NON-esp device (NRF81522) #708

Open VincentGijsen opened 2 months ago

VincentGijsen commented 2 months ago

hi,

i'm trying to connect as client to my hometrainer, alas so far i've not managed to do so. presently using an NRF58122, where it seems this function() is non-existing: void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA). however i'm not entirely sure its even the right function in the first place.

While testing on an ESP32S3, i have this function, but after exhausting most combinations of that function also failed to connect to the service, i just get (in both cases) a connection back, but then getting a REF to a BLE-service, fails

using the the ANOTHER libary (#include "BLEDevice.h"), i was able to connect to this hometrainer, using this call:

    bool _connected = pClient->connect(*myBike, BLE_ADDR_TYPE_RANDOM); //, BLE_ADDR_TYPE_PUBLIC);
    // the RANDOm bit is important, else it will not connect!

do note the explicit need to make clear the connect requires the BLE_ADDR_TYPE_RANDOM const, which is fed into the GATT object of the ESP-libary underlying.

hence I'm not entirely sure if the connect-argument of the stock-library, is 'similar' as the NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA)

Any pointers how how i could get the connection going? ideally I stick to the NRF with the great https://github.com/h2zero/platform-n-able.git@^1.1.0 library. as these chips are soo dirt cheap, and i'm really charmed by the full open-source setup possible here (no softdevice) :)

ultimate goal would be to have the NRF/ESP as client+server to rewrite some calls between trainer and application on the pc albeit challenging given the very limited ram/flash on the NRF. Debugging with the NRF is quite a bit faster/more pleasant compared to the ESP, with my Segger probe.

edit 1 after enabling debugging, and adding in this line (as per ESP32 implementation) NimBLEDevice::setSecurityInitKey(BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID); I see the following extra info:

starting
D NimBLEClient: >> connect(d0:33:36:33:1b:5b)
D NimBLEClient: Got Client event 
E NimBLEClient: Connection failed; status=13
Failed to connect

Thanks for the awesome work already; regardless of a solution for this one!

h2zero commented 2 months ago

Hi, I'm not sure what the issue is in this case as the nRF chips naturally have a random static address, so I would think it should work. That said the device you're connecting to might be expecting only a phone to connect which will have a random resovlable address, requiring bonding. In this case there would need to be some changes to the library to support this as it's not currently implemented. I will flag this to investigate further.

VincentGijsen commented 2 months ago

mm, sound not super trivial then?

anything i can do to 'log/dump' /handshake the bonding?

On Fri, Aug 30, 2024 at 1:47 AM h2zero @.***> wrote:

Hi, I'm not sure what the issue is in this case as the nRF chips naturally have a random static address, so I would think it should work. That said the device you're connecting to might be expecting only a phone to connect which will have a random resovlable address, requiring bonding. In this case there would need to be some changes to the library to support this as it's not currently implemented. I will flag this to investigate further.

— Reply to this email directly, view it on GitHub https://github.com/h2zero/NimBLE-Arduino/issues/708#issuecomment-2319476575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFQIY7NAU7SP53QUOCTME3ZT6XKJAVCNFSM6AAAAABMLADTEKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJZGQ3TMNJXGU . You are receiving this because you authored the thread.Message ID: @.***>

-- Met vriendelijke groet,

Vincent Gijsen http://www.vincentgijsen.nl

h2zero commented 1 month ago

Try this:

ble_addr_t addr;
ble_hs_id_gen_rnd(0, &addr);
ble_hs_id_set_rnd(addr.val);
VincentGijsen commented 1 month ago

i tried it,

in my init function, around the init of nimbestack, and (seperately just before the connect function to the scanned/found MAC, for my trainer:

btw, the trainer reports: - Resolvable Private Address Only [R] (0x2AC9) (NRFconnect); attached the full log; perhaps it tells you more then it tells mine. Tunturi E35 log NRFconnect.txt

    ble_addr_t addr;
    ble_hs_id_gen_rnd(0, &addr);
    ble_hs_id_set_rnd(addr.val);

    this->_pClient = NimBLEDevice::createClient();
    this->_pClient->setClientCallbacks(&clientCB, false);

//ble_addr_t addr;
    ble_hs_id_gen_rnd(0, &addr);
    ble_hs_id_set_rnd(addr.val);
<<<<LOG BELOW>>>
D NimBLEClient: Got Client event 
E NimBLEClient: Connection failed; status=13
Failed to connect
..

like so:

..
bool BikeClient::connectToTrainer()
{
    log_i("starting");

    ble_addr_t addr;
    ble_hs_id_gen_rnd(0, &addr);
    ble_hs_id_set_rnd(addr.val);

    doConnect = false;

        if (!_pClient->isConnected())
        {
            if (!_pClient->connect(advDevice,true))
            {
                Serial.println("Failed to connect");
                return false;

...

not sure if these are the right place to call those functions, irrespectively both flavors compile fine.

h2zero commented 1 month ago

As long as you set the address after init it should be fine. What is the error code when the connection fails?

VincentGijsen commented 1 month ago

Could it be‘’NimBLEClient: Connection failed; status=13’’’Or some lower level code I’m not printing On 21 Sep 2024, at 17:47, h2zero @.***> wrote: As long as you set the address after init it should be fine. What is the error code when the connection fails?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>