如何在Linux x86上使用C对蓝牙LE设备进行编程?
发布时间:2021-01-09 20:06:37 所属栏目:Linux 来源:网络整理
导读:我有一个蓝牙设备,我可以在 linux上使用gatttool控制.我想开发自己的c程序,可以向它发送命令. 我以前做了蓝牙编程,它相对简单,类似于网络编程,但这一次,它是一个蓝牙低功耗设备,并遵循原则here导致主机关闭消息,当我可以清楚地连接/断开它使用gatttool. 我该
|
我有一个蓝牙设备,我可以在 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 hostn");
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 (编辑:清远站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

