This is the error that is causing disconnection (Event 05 disconnect reason code = 3E)
2.59 CONNECTION FAILED TO BE ESTABLISHED / SYNCHRONIZATION TIMEOUT (0x3E)
The Connection Failed to be Established / Synchronization Timeout error code
indicates that the Link Layer initiated a connection or initiated synchronization to
periodic advertising but the connection has failed to be established or the Link
Layer failed to synchronize with the periodic advertising within 6 periodic
advertising events of the first attempt.
The Link Layer is buried in the Bluetooth adapter hardware and outside user programming control, so it's not a coding issue.
Since this seems to have something to do with the advertising interval, I tried changing it on the HM10 over its whole range (AT+ADVI0 to AT+ADVIF) but could not reproduce the error. You might try that. My HM10 connects reliably every time, and I have never seen this error with any device.
A google search for this error suggested that interference could be the cause.
Try deleting the Pi's data file /etc/btferret.dat. It might be storing invalid pairing data, but the disconnection happens before pairing even starts.
If you do get it working, this is code for an HM10 client. It scans and prompts for a node number, then connects, sends "Hello" and waits for a reply from the HM10 (an ASCII string sent from the serial terminal connected to the HM10). So this shows how serial data is sent through the FFE1 characteristic.
2.59 CONNECTION FAILED TO BE ESTABLISHED / SYNCHRONIZATION TIMEOUT (0x3E)
The Connection Failed to be Established / Synchronization Timeout error code
indicates that the Link Layer initiated a connection or initiated synchronization to
periodic advertising but the connection has failed to be established or the Link
Layer failed to synchronize with the periodic advertising within 6 periodic
advertising events of the first attempt.
The Link Layer is buried in the Bluetooth adapter hardware and outside user programming control, so it's not a coding issue.
Since this seems to have something to do with the advertising interval, I tried changing it on the HM10 over its whole range (AT+ADVI0 to AT+ADVIF) but could not reproduce the error. You might try that. My HM10 connects reliably every time, and I have never seen this error with any device.
A google search for this error suggested that interference could be the cause.
Try deleting the Pi's data file /etc/btferret.dat. It might be storing invalid pairing data, but the disconnection happens before pairing even starts.
If you do get it working, this is code for an HM10 client. It scans and prompts for a node number, then connects, sends "Hello" and waits for a reply from the HM10 (an ASCII string sent from the serial terminal connected to the HM10). So this shows how serial data is sent through the FFE1 characteristic.
Code:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "btlib.h" int notify_callback(int node,int cticn,unsigned char *dat,int datlen);void getstring(char *prompt,char *s,int len);int inputint(char *ps);int main() { int node,cticn; unsigned char uuid[2]; char *buf; if(init_blue("devices.txt") == 0) return(0); printf("\nScanning for HM10\n"); le_scan(); printf("\nEnter [ ] to scroll device list\n"); node = inputint("Enter node of HM10 (should report UUID=FFE0)"); if(node < 0) return(0); if(connect_node(node,CHANNEL_LE,0) == 0) { printf("Connect failed\n"); return(0); } // read services find_ctics(node); // find data characteristic UUID=FFE1 uuid[0] = 0xFF; uuid[1] = 0xE1; cticn = find_ctic_index(node,UUID_2,uuid); if(cticn > 0) { printf("Found data characteristic index = %d\n",cticn); printf("Enabling notifications\n"); notify_ctic(node,cticn,NOTIFY_ENABLE,notify_callback); printf("Sending Hello\n"); buf = "Hello\n"; write_ctic(node,cticn,buf,strlen(buf)); printf("Waiting 30s for a reply from HM10\n"); printf("Send an ASCII string reply from the HM10 with a line feed termination\n"); read_notify(30000); printf("\n30s wait for reply timed out\n"); } else printf("Data characteristic FFE1 not found\n"); printf("Disconnecting HM10\n"); disconnect_node(node); close_all(); return(0); } int notify_callback(int node,int cticn,unsigned char *dat,int datlen) { int flag,n; for(n = 0 ; n < datlen ; ++n) printf(" %02X",dat[n]); printf("\n"); flag = 0; for(n = 0 ; n < datlen ; ++n) { if(dat[n] < 32) { dat[n] = 0; flag = 1; } } if(flag == 0) printf("No line feed 0A terminate\n"); else printf("Reply = %s\n",dat); } void getstring(char *prompt,char *s,int len) { int n; do { printf("%s",prompt); fgets(s,len,stdin); } while(s[0] == 10); n = 0; while(s[n] != 10 && s[n] != 13 && s[n] != 0) ++n; s[n] = 0; } /***** INPUT INTEGER ********/ int inputint(char *ps) { int n,flag; char s[128]; do { printf("%s (x=cancel)\n",ps); getstring("? ",s,128); flag = 0; for(n = 0 ; s[n] != 0 ; ++n) { if(s[n] < '0' || s[n] > '9') flag = 1; } if(flag == 0) n = atoi(s); else if(s[0] == 'x') { n = -1; flag = 0; } else if(s[0] == '[') scroll_back(); else if(s[0] == ']') scroll_forward(); else printf("Not a number\n"); } while(flag != 0); return(n); }
Statistics: Posted by petzval — Tue Apr 08, 2025 4:30 pm