This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Pairing and key exhange with ANCS

Hi.

I've started to check if I can get ANCS compatible content provider to run in Android. The nrf52 is running out-of-box version of the nordic ancs example.

I can find it, see that it solicitates the ANCS service in the advertisement and I can try to connect it. However as it's not paired, the connection fails right away. I've read the apple developer page about the ANCS and also nordic explanation about the example.

So the problem is that I guess I need to bond the devices and exchange trusted keys. I've tried the bonding, but after the mBluetoothDevice.createBond(), the BluetoothDevice.ACTION_BOND_STATE_CHANGED- callback I get has status BOND_NONE and oldstate was BOND_BONDING. How this key-exchange is supposed to be handled? Am I missing something here?

So far I've added the service for the ANCS:

UUID: "7905F431-B5CE-4E99-A40F-4B1E122D00D0" (main service)

three characteristics for it:

  • Notification source (with CCCD)
  • Notification data source (with CCCD)
  • Notification control point (with CCCD)

The service is added to gatt server in the android side, I get the callback for the service added, and the device seems to try to connect the gatt service, but it drops it right away. I'm trying the pairing before any other connection to device or the callbacks to connectionStateChange.

EDIT: Log from nRF MCP:

I	14:51:29.845	[Server] Device with address CE:CE:67:C2:71:FE connected
D	14:51:29.858	[Broadcast] Action received:    android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_BONDING (11)
I	14:51:29.872	Connected to CE:CE:67:C2:71:FE
D	14:51:29.882	gatt.close()
D	14:51:29.892	[Server callback] Connection state changed with status: 0 and new state: DISCONNECTED (0)
D	14:51:30.054	wait(600ms)
I	14:51:30.077	[Server] Device disconnected
D	14:51:35.774	[Server callback] Connection state changed with status: 0 and new state: CONNECTED (2)
I	14:51:35.811	[Server] Device with address CE:CE:67:C2:71:FE connected
D	14:51:35.825	[Broadcast] Action received: android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_NONE (10)
I	14:51:35.841	Bonding failed
V	14:51:35.852	Connecting to CE:CE:67:C2:71:FE...
D	14:51:35.862	[Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED
D	14:51:35.884	[Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED
D	14:51:35.896	gatt = device.connectGatt(autoConnect = false)
D	14:51:35.914	[Server callback] Connection state changed with status: 0 and new state: DISCONNECTED (0)
D	14:51:35.934	gatt.close()
I	14:51:35.953	[Server] Device disconnected
D	14:51:35.968	[Server callback] Connection state changed with status: 0 and new state: DISCONNECTED (0)
I	14:51:35.982	[Server] Device disconnected
  • Hi Matts,

    You should have BOND_BONDED if it's successfully bonded. I suspect there could be an issue here. You may want to capture a sniffer trace to see what was wrong. If you use nRF Master Control Panel to connect and bond, do you have any problem ? Note that the key exchange process is handled by Android and the app doesn't need to do anything (except for triggering the bonding process)

    Could you explain "but it drops it right away" ? Does that mean the connection was dropped ? Again, a sniffer trace would reveal more info.

  • Hi Hung Bui!

    I cannot use the nRF MCP as I do not have windows machine. I have however iPhone 5 for testing purposes and it connects OK with the nRF52.

    Regarding that connection dropping. I have GattServer with that new service running in the android phone. I've tried connecting with the device both via regular gatt connection and server connection. Not sure which is the correct way, but result is the same.

    In onConnected callback I'll try to do the bonding (bluetoothdevice.createBond() -call). It gives me first BluetoothDevice.ACTION_BOND_STATE_CHANGED to BluetoothDevice.BOND_BONDING. However right after that (300ms later) I get onConnectionStateChange -callback with disconnected state and callback to BluetoothDevice.ACTION_BOND_STATE_CHANGED with value BOND_NONE.

    What most bothers me, that I get error status (on connection drop) that no read rights or value 62.

  • I've seen the apple document for content consumer developers. But is there any flow how the initial communication should go? I mean in the apple document there wasn't even mention about the CCCD:s in the characterics.

  • Also regarding the communication initialization:

    Does the Content Provider connect the Consumer directly? I get connection to GattServer just by making the bond call. If the CP should connect directly, should that be via GattServer.connect or via gatt client (bluetoothdevice.connectGatt)?

    Also should the Content provider read/write something to Content Consumer (nrf board)?

    Hopefully I can get the jlink ultra connected to this today and get more info what happens in the nrf board. Also trying to get HCL logs from the android side.

  • Hi Matts,

    I was talking about the nRF Master Control Panel on Android (or iOS). We also have an app for the mobile platforms.

    I don't think we have any further than the documentation from Apple about ANCS here.

    Could you clarify that you have tested and everything worked fine when you test with iOS device instead of Android ?

    The concept should be the same, on Android and iOS. The only difference is on Android you need to make the server yourself, and handle the notification provider on your own.

    The flow of communication is described in the diagram in the documentation from Apple above. You connect and bond from the phone (the ANCS server) after that the client should start to discover the service and subscribe to the characteristic(s).

    Seems that you has problem with bonding, please try to test bonding with other example, such as proximity example.

Related