Apollon77 / hap-controller-node

Node.js library to implement a HAP (HomeKit) controller
Mozilla Public License 2.0
55 stars 15 forks source link

Question: BLE pairing/get accessories #38

Closed Apollon77 closed 3 years ago

Apollon77 commented 3 years ago

Hi @mrstegeman, sorry It's me again :-)

I now started checking with a BLE device and yes seems very special.

Pairing worked directly which was great. Then I tried "get accessories" and directly had two issues where it would be cool to hear your opinion if you find some time to support me understanding it.

First of all the pair Verify failed because

https://github.com/Apollon77/hap-controller-node/blob/main/src/transport/ble/gatt-client.ts#L553-L556

returned not only one characteristic but (as it seems) all. So the assumption of "array only contains one item" was wrong.

I worked around that by adding an array filter, but I still need to understand why noble returned it that way ... strange

Then the verify completed, but then it fails at

https://github.com/Apollon77/hap-controller-node/blob/main/src/transport/ble/gatt-client.ts#L1175

and the device do not respond - and after that connection seems dead - so also the rest of the code (that works if i do not request the primary/hidden service info) is failing.

My question after also checking other libs is:

Should https://github.com/Apollon77/hap-controller-node/blob/main/src/transport/ble/gatt-client.ts#L1169 not be buildServiceSignatureReadRequest ?? (ok I tried it but device also do not answer to this)

{
  characteristic: Characteristic {
    _noble: Noble {
      initialized: true,
      address: 'unknown',
      _state: 'poweredOn',
      _bindings: [NobleMac],
      _peripherals: [Object],
      _services: [Object],
      _characteristics: [Object],
      _descriptors: [Object],
      _discoveredPeripheralUUids: [Array],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _allowDuplicates: true
    },
    _peripheralId: '909fb1661f5849618a26c8fa55f194f9',
    _serviceUuid: '000000a20000100080000026bb765291',
    uuid: '000000a50000100080000026bb765291',
    name: null,
    type: null,
    properties: [ 'read', 'write' ],
    descriptors: [ [Descriptor] ],
    _events: [Object: null prototype] {},
    _eventsCount: 0
  },
  iid: 12
}
Write 0006020c00
  hap-controller:gatt-connection 909fb1661f5849618a26c8fa55f194f9/e3-49-08-9c-a8-d8 Write for characteristic 000000a50000100080000026bb765291 0e29fb36ee33f0432dc9f8ade11247be0366c8a1b5 +2ms
{
  characteristic: Characteristic {
    _noble: Noble {
      initialized: true,
      address: 'unknown',
      _state: 'poweredOn',
      _bindings: [NobleMac],
      _peripherals: [Object],
      _services: [Object],
      _characteristics: [Object],
      _descriptors: [Object],
      _discoveredPeripheralUUids: [Array],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _allowDuplicates: true
    },
    _peripheralId: '909fb1661f5849618a26c8fa55f194f9',
    _serviceUuid: 'e863f007079e48ff8f279c2605a29f52',
    uuid: '000000a50000100080000026bb765291',
    name: null,
    type: null,
    properties: [ 'read', 'write' ],
    descriptors: [ [Descriptor] ],
    _events: [Object: null prototype] {},
    _eventsCount: 0
  },
  iid: 20
}
Write 0006031400
  hap-controller:gatt-connection 909fb1661f5849618a26c8fa55f194f9/e3-49-08-9c-a8-d8 Write for characteristic 000000a50000100080000026bb765291 6203412d05f9e1318f0058f9a5057b2a22057e9c69 +11s
{
  characteristic: Characteristic {
    _noble: Noble {
      initialized: true,
      address: 'unknown',
      _state: 'poweredOn',
      _bindings: [NobleMac],
      _peripherals: [Object],
      _services: [Object],
      _characteristics: [Object],
      _descriptors: [Object],
      _discoveredPeripheralUUids: [Array],
      _events: [Object: null prototype],
      _eventsCount: 6,
      _allowDuplicates: true
    },
    _peripheralId: '909fb1661f5849618a26c8fa55f194f9',
    _serviceUuid: '000000470000100080000026bb765291',
    uuid: '000000a50000100080000026bb765291',
    name: null,
    type: null,
    properties: [ 'read', 'write' ],
    descriptors: [ [Descriptor] ],
    _events: [Object: null prototype] {},
    _eventsCount: 0
  },
  iid: 39
}
Write 0006042700
  hap-controller:gatt-connection 909fb1661f5849618a26c8fa55f194f9/e3-49-08-9c-a8-d8 Write for characteristic 000000a50000100080000026bb765291 5248fcc57bc1d31248af65a95b0a24ea5743acbf4a +11s

was that working for your BLE device?

Apollon77 commented 3 years ago

Ha, I got it working ... I "Just" needed to init the crypto session later - so when I move "_pairVerify" one block down it works ... and then it works with both read requests (so characteristic and the ServiceSignature one) ... but no data returned in both cases ...

so whats the semantically correct read request? What do you think?

mrstegeman commented 3 years ago

I never had any issues with that method, so it's hard to say what's going on. I thought I had it all in the right order, but maybe not.

Apollon77 commented 3 years ago

Does your device have a primary or hidden service defined?

I think o finish my work and then write here. Maybe you find time foe a short test then. Otherwise will have users to test it soon too

mrstegeman commented 3 years ago

I'm pretty sure all devices have a primary service, as I think it's a requirement.

Apollon77 commented 3 years ago

According to specs (at least how I read them) it is optional ... and also my BLE device do now show any "primary:true" in jsons (nor the Hue Bridge - IP- or the Eve Energy BLE one

Apollon77 commented 3 years ago

As update, I got things working :-)

Additionally I now know what you mean with the BLE subscription issues ... It's a mess :-(

But in fact I make progress :-)

NebzHB commented 3 years ago

Hello, I'm using Eve Energy with the Eve Extend in the Network version of the lib, and it's the same for power consumption. even if there is a "ev" you only get it with a getCaracteristic... no event.

Apollon77 commented 3 years ago

@NebzHB With "Network" you mean "Wifi"? interesting ... But ok then I can stop searching ;-))

NebzHB commented 3 years ago

yes wifi

Apollon77 commented 3 years ago

Ok, in fact getAccessories is fixed on Gatt/BLE. I also added "linked services" but because lacking a devices with linked services Icould not really test it :-(

Anyone of you having such a device? ;-)

See GitHub version for now ... Need to test some more before releaseing it

Apollon77 commented 3 years ago

PS: I was also not possible to get a "list Pairing" response from my BLE device ... and my Hue Bridge returns only an emty array as listPairings response ... strange