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
670 stars 138 forks source link

Can't connect device #618

Closed stefano88ita closed 6 months ago

stefano88ita commented 6 months ago

Hi! I have a fireplace (Dimplex). A couple of years ago i wrote an implementation in node.js to be used directly from home assistant and alexa. The code is pretty simple after sniffing the correct service and characteristics, here's an extract:

const {createBluetooth} = require('node-ble')
const {bluetooth, destroy} = createBluetooth()
bluetooth.defaultAdapter().then((adapter)=>{
    adapter.getDevice('00:A0:50:DC:98:13').then(device => {
        device.connect().then(status => {
            device.gatt().then(gatt => {
                           turn(device, gatt, process.argv[3] == "on");
                         });
                });
        });
});

function turn(device, gatt, on){
    gatt.getPrimaryService("00000000-0000-1000-8000-00805f9b34fb").then(service => {                                    
        service.getCharacteristic('00004008-0000-1000-8000-00805f9b34fb').then(characteristic => {
            const buffer = Buffer.from(on?"0601":"0000", 'hex');
            characteristic.writeValue(buffer).then(() => { disconnect(device) })
            process.stdout.write(on?"ON":"OFF")
        })
    })
}

I want to port this code in an esp32 to shut down the raspberry pi that runs the node.js code. I tried in many ways, like:

NimBLEDevice::init("");
pClient = NimBLEDevice::createClient();
pClient->connect(NimBLEAddress("00:A0:50:DC:98:13", 1)); // tried lower case too
Serial.println(pClient->isConnected()?"Connected":"Non connected");
  NimBLEDevice::init("");
  pClient = NimBLEDevice::createClient();
  pClient->connect(NimBLEAddress("00:A0:50:DC:98:13")); // tried lower case too
  Serial.println(pClient->isConnected()?"Connected":"Non connected");
  NimBLEScan *pScan = NimBLEDevice::getScan();
  NimBLEScanResults results = pScan->start(10);
  for(int i = 0; i < results.getCount(); i++) {
    NimBLEAdvertisedDevice device = results.getDevice(i);
    if(String(device.getAddress()) == "688551401491"){ // hex to int conversion
      NimBLEClient *pClient = NimBLEDevice::createClient();
      pClient->connect(&device, true); //tried with second param true, false and no second param
      Serial.println(pClient->isConnected()?"Connected":"Not connected");

Connection result is always false. I'm i doing something wrong? Thanks!

stefano88ita commented 6 months ago

Trying to investigate the type of device is legacy, event type and adv type are BLE_HCI_ADV_RPT_EVTYPE_ADV_IND and BLE_HCI_ADV_TYPE_ADV_IND. In fact, the device is not instantiated in the scan callback (setAdvertisedDeviceCallbacks). To make a try i inserted: else{ pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); } after line 154 in NIMBleScan.cpp. The Advertising device has a correct manufaturer name, but still unable to connect. Error code in connection is 574 E NimBLEClient: Connection failed; status=574

Any suggestion to make the connection? Thanks!

stefano88ita commented 6 months ago

Resolved by changing esp 32 with another one, maybe faulty