我有一个蓝牙设备,我可以在
linux上使用gatttool控制.我想开发自己的c程序,可以向它发送命令.
我以前做了蓝牙编程,它相对简单,类似于网络编程,但这一次,它是一个蓝牙低功耗设备,并遵循原则here导致主机关闭消息,当我可以清楚地连接/断开它使用gatttool.
我该如何创建这个程序?我知道我应该使用bluez库,但我不知道从哪里开始使用低能耗设备.
int main(int argc,char **argv) { struct sockaddr_rc addr = { 0 }; int s,status; char dest[18] = "B4:99:4C:5C:EE:49"; char buf[2048]; pthread_t rthread; setbuf(stdout,NULL); // allocate a socket s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM); // set the connection parameters (who to connect to) addr.rc_family = AF_BLUETOOTH; addr.rc_channel = (uint8_t) 1; str2ba( dest,&addr.rc_bdaddr ); // connect to server status = connect(s,(struct sockaddr *)&addr,sizeof(addr)); if( status < 0 ){ perror("Error connecting to host\n"); exit(1); } while(fgets(buf,sizeof(buf),stdin) != NULL){ status = send(s,buf,0); if(status < 0){ printf("Error sending.\n"); exit(1); } } close(s); return;
解决方法
你的程序是经典的蓝牙,为了支持我的声明,我会说在任何经典的蓝牙设备上你的代码工作都可以正常工作
为了得到教团我建议去这个link.sudo ./st会扫描附近的ble divices
https://github.com/carsonmcdonald/bluez-experiments