{"action":"created","issue":{"url":"https://api.github.com/repos/labapart/gattlib/issues/2","repository_url":"https://api.github.com/repos/labapart/gattlib","labels_url":"https://api.github.com/repos/labapart/gattlib/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/labapart/gattlib/issues/2/comments","events_url":"https://api.github.com/repos/labapart/gattlib/issues/2/events","html_url":"https://github.com/labapart/gattlib/issues/2","id":194352838,"number":2,"title":"gattlib_connect does not succeed all the time when it should","user":{"login":"cryptax","id":2493690,"avatar_url":"https://avatars.githubusercontent.com/u/2493690?v=3","gravatar_id":"","url":"https://api.github.com/users/cryptax","html_url":"https://github.com/cryptax","followers_url":"https://api.github.com/users/cryptax/followers","following_url":"https://api.github.com/users/cryptax/following{/other_user}","gists_url":"https://api.github.com/users/cryptax/gists{/gist_id}","starred_url":"https://api.github.com/users/cryptax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cryptax/subscriptions","organizations_url":"https://api.github.com/users/cryptax/orgs","repos_url":"https://api.github.com/users/cryptax/repos","events_url":"https://api.github.com/users/cryptax/events{/privacy}","received_events_url":"https://api.github.com/users/cryptax/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2016-12-08T14:43:30Z","updated_at":"2017-02-19T09:55:42Z","closed_at":null,"body":"I wrote a program using gattlib that connects to a BLE device. Unfortunately, although the device is in a correct state, **the connection fails many times before it eventually works**. I consequently have to perform several connection attemps before I actually manage to connect.\r\n\r\nThis issue does not occur when I use _gatttool_: I always connect (perhaps sometimes a bit longer) provided my device/host/dongle is in a normal state of course.\r\n\r\nThat's the piece of code I use for connection:\r\n\r\n```\r\ngatt_connection = gattlib_connect(NULL, addr, BDADDR_LE_RANDOM, BT_IO_SEC_LOW, 0, 0);\r\nif(gatt_connection == NULL){\r\n  printf(\"[-] Connection failed\\n\");\r\n```\r\n\r\nSample output of my program:\r\n```\r\n[>>>] Connecting to xx:xx:xx:xx:xx:xx... (attempt no=1)\r\n[-] Connection failed\r\n[>>>] Trying again...\r\n[>>>] Connecting to xx:xx:xx:xx:xx:xx... (attempt no=2)\r\n[+] Connected to xx:xx:xx:xx:xx:xx\r\n```\r\n\r\nWhy is that? Is there a way I can change some settings etc so that the connection always succeeds (provided the device is there ;)?\r\n\r\nFor example, with `gatttool`:\r\n\r\n```\r\n$ gatttool -I -b xx:xx:xx:xx:xx:xx -t random\r\n[xx:xx:xx:xx:xx:xx][LE]> connect\r\nAttempting to connect to xx:xx:xx:xx:xx:xx\r\nConnection successful\r\n[xx:xx:xx:xx:xx:xx][LE]>\r\n```\r\n\r\nI have **bluez 5.37** and the latest checkout of **gattlib**."},"comment":{"url":"https://api.github.com/repos/labapart/gattlib/issues/comments/280908153","html_url":"https://github.com/labapart/gattlib/issues/2#issuecomment-280908153","issue_url":"https://api.github.com/repos/labapart/gattlib/issues/2","id":280908153,"user":{"login":"stepen","id":6130916,"avatar_url":"https://avatars.githubusercontent.com/u/6130916?v=3","gravatar_id":"","url":"https://api.github.com/users/stepen","html_url":"https://github.com/stepen","followers_url":"https://api.github.com/users/stepen/followers","following_url":"https://api.github.com/users/stepen/following{/other_user}","gists_url":"https://api.github.com/users/stepen/gists{/gist_id}","starred_url":"https://api.github.com/users/stepen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stepen/subscriptions","organizations_url":"https://api.github.com/users/stepen/orgs","repos_url":"https://api.github.com/users/stepen/repos","events_url":"https://api.github.com/users/stepen/events{/privacy}","received_events_url":"https://api.github.com/users/stepen/received_events","type":"User","site_admin":false},"created_at":"2017-02-19T09:55:42Z","updated_at":"2017-02-19T09:55:42Z","body":"Here is some additional data.\r\n\r\nRun #1: NEW gattlib-gatttool, Device=20:CD:39:B1:8E:E3: 59 calls, 9 fail-to-read, 3 resource busy.\r\nRun #2: NEW gattlib-gatttool, Device=20:CD:39:B1:8E:E3: (with CONNECTION_TIMEOUT=4s):  10 calls, 1 fail-to-read, 4 resource busy.\r\nRun #3: ORIGINAL gatttool, Device=20:CD:39:B1:8E:E3 (BlueZ, 5.23): 69 calls: 0 failed-to-read, 0 resource busy\r\nRun #4: NEW gattlib-gatttool: Different Device (a kitchen termometer): 52 calls, 0 failed-to-read, 0 resource busy.\r\n\r\nI've only included data for run #1 and run#2. And as I said earliere, it MIGHT well be a problem with the actual device (20:CD:39:B1:8E:E3=prototype), which the results partly hints at.\r\n\r\n```\r\nRUN #1\r\n\r\n$ time ./gatttool -b 20:CD:39:B1:8E:E3 --char-read -a 0x0040\r\nCharacteristic value/descriptor: 5a\r\n\r\nreal    0m1.660s  // OK\r\nuser    0m0.000s\r\nsys     0m0.010s\r\n\r\nSubsequent:\r\n\r\nreal    0m0.147s  // OK\r\nreal    0m0.113s  // OK\r\nreal    0m0.131s  // OK\r\nreal    0m0.130s  // OK\r\nreal    0m0.095s  // OK\r\nreal    0m0.110s  // OK\r\nreal    0m0.143s  // OK\r\nreal    0m0.105s  // OK\r\nreal    0m0.106s  // OK\r\nreal    0m0.114s  // OK\r\nreal    0m0.093s  // OK\r\nreal    0m0.111s  // OK\r\nreal    0m0.129s  // OK\r\nreal    0m0.130s  // OK\r\nreal    0m0.148s  // OK\r\nreal    0m0.152s  // OK\r\nreal    0m0.119s  // OK\r\nreal    0m0.113s  // FAILED - nothing is read\r\nreal    0m2.012s  // FAILED - nothing is read\r\nreal    0m2.012s  // FAILED - nothing is read\r\nreal    0m2.006s  // FAILED - nothing is read\r\nreal    0m2.012s  // FAILED - nothing is read\r\nreal    0m2.014s  // OK\r\nreal    0m0.115s  // OK\r\nreal    0m0.130s  // OK\r\nreal    0m0.128s  // OK\r\nreal    0m0.130s  // OK\r\nreal    0m0.157s  // OK\r\nreal    0m0.096s  // OK\r\nreal    0m0.098s  // OK\r\nreal    0m0.223s  // OK\r\nreal    0m0.136s  // OK\r\nreal    0m0.099s  // OK\r\nreal    0m0.138s  // OK\r\nreal    0m0.120s  // OK\r\nreal    0m0.091s  // OK\r\nreal    0m0.149s  // OK\r\nreal    0m2.011s  // FAILED - nothing is read\r\nreal    0m2.011s  // FAILED - nothing is read\r\nreal    0m1.872s  // OK\r\nreal    0m0.108s  // OK\r\nreal    0m0.113s  // OK\r\nreal    0m0.135s  // OK\r\nreal    0m0.106s  // OK\r\nreal    0m0.153s  // OK\r\nreal    0m0.096s  // OK\r\nreal    0m0.108s  // OK\r\nreal    0m0.130s  // OK\r\nreal    0m0.148s  // OK\r\nreal    0m0.113s  // OK\r\nreal    0m0.139s  // OK\r\nreal    0m0.129s  // OK\r\nreal    0m0.139s  // OK\r\nreal    0m2.009s  // FAILED - nothing is read\r\nreal    0m2.011s  // FAILED - nothing is read\r\nreal    0m0.014s  // FAILED - connect: Device or resource busy (16)\r\nreal    0m0.014s  // FAILED - connect: Device or resource busy (16)\r\nreal    0m0.013s  // FAILED - connect: Device or resource busy (16)\r\n```\r\nNever recovers from this. 'bluetoothctl' (info 20:CD:39:B1:8E:E3) reports device as 'connected = yes'. Which explains the error, but not why it occured in the first place.\r\nI waited 20 min to see if it would time out or something, but it did not. 'Resource busy' persisted. Only resolution is to either turn the device off and restart or disconnect via 'bluetoothctr'.\r\n\r\n```\r\nRUN #2\r\n\r\n$ time ./gatttool -b 20:CD:39:B1:8E:E3 --char-read -a 0x0040\r\nCharacteristic value/descriptor: 57\r\n\r\nreal    0m1.645s\r\nuser    0m0.000s\r\nsys     0m0.010s\r\n\r\nSubsequent\r\n\r\nreal    0m3.691s  // OK\r\nreal    0m0.099s  // OK\r\nreal    0m0.140s  // OK\r\nreal    0m0.102s  // OK\r\nreal    0m4.049s  // OK - Failed to read\r\nreal    0m0.013s  // FAILED - connect: Device or resource busy (16)\r\nreal    0m0.013s  // FAILED - connect: Device or resource busy (16)\r\nreal    0m0.014s  // FAILED - connect: Device or resource busy (16)\r\nreal    0m0.013s  // FAILED - connect: Device or resource busy (16)\r\n\r\n```"}}
{"actor":{"display_login":"stepen"}}