树莓派连接蓝牙适配器,用C语言与手机通过蓝牙进行通信

光屁屁打酱油 2015-10-14 11:21:44
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status, len=0;
char dest[18 = "00:12:01:31:01:13";
char buf[256;
// 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){
printf(" failed to connect the device!\n");
return -1;
}
do{
len = read(s, buf, sizeof buf);
if( len>0 ) {
buf[len=0;
printf("%s\n",buf);
write(s, buf, strlen(buf));
}
}while(len>0);
close(s);
return 0;
}
现在树莓派与手机已经连接,但无法读取到手机发送的信息,树莓派上的连接选的是Serial Port
...全文
607 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
glchild514 2015-10-14
  • 打赏
  • 举报
回复
你是不是發錯位置了 , 這裡是WinCE版

27,377

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧