iosbluetoothbluezgattancs

ANCS client: What is the basic procedure to be able to receive notifications?


I'm trying to implement an ANCS client in C on Linux with the BlueZ stack (newest version, 5.39). So far I can set the BLE advertisement data with the service soliciation UUID provided by Apple.

My device does show up in the iPhone (iOS 9) settings. I can also connect to the device, the connection is very unstable though, meaning, sometimes after a while, and always when I close the bluetooth settings submenu on the iPhone the connection is terminated. It's also not bonding, after the disconnect, the device is not showing up under "my devices" on the iPhone.

I looked at the hcidump, apparently is just reads a bunch of ATT data, then stops:

Pastebin link, because it didn't format it correctly on here

After the connection is terminated is outputs the lower part of the pastebin.

Now, my question is, what would be the next step to create a ANCS client that receives notifications from the iOS device? I have read countless articles and sample code about BLE and ANCS, but still I'm clueless.

Do I have to open a L2CAP socket in my C code to accept the connection from the iPhone? I already tried that, didn't work. I also tried to connect to the iPhone with gatttool, while it was connected, but it said "resource or device busy".

I'd be very thankful for some pointers in the right direction, on what to do next.


Solution

  • Ok, after a lot of research I can finally answer by own question:

    What I already accomplished before I asked this question:

    1. Set the advertising data to send out the primary service UUID of ANCS.

    What's necessary after that:

    1. Stop bluetooth-daemon (you can also stop it before setting the advertising data, but as far as I know it has to be run at some point to start some kernel modules.)
    2. Create L2CAP socket with CID (channel identifier 4 for ATT). The security level has to be MEDIUM, that way the kernel will automatically pair with the iPhone.
    3. Bind/Listen/Accept to wait for a connection from iOS-Device
    4. Write/read ATT messages like defined in the Bluetooth specification

    One should definitely look at tools/btgatt-client.c & tools/bt-gatt-server.c in the BlueZ source code. It shows exactly how to open the socket (with sockopt, etc.)

    Hope this helps anyone who is struggling to figure this out like I was.