下面是代码。它最初是为 1.8.7 编写的,自 2006 年以来就没有被触及过。我已经成功地获得了一个姊妹函数 bt_rfcomm_socket_connect() 工作,但不使用 GetOpenFile(),但如果可能的话,我想用 Ruby Way(tm) 来做。
263 static VALUE
264 bt_l2cap_socket_connect(VALUE self, VALUE host, VALUE port)
265 {
266 fprintf(stderr, "connecting!\n");
267 rb_io_t *fptr;
268 int fd;
269
270 VALUE tmp = rb_check_convert_type(self, T_FILE, "IO", "to_io");
271
272 fprintf(stderr,"Getting open file\n");
273 GetOpenFile(tmp, fptr);
274
275 fprintf(stderr, "got open file\n");
276 fd = fptr->fd;
277
278 fprintf(stderr, "structing\n");
279 struct sockaddr_l2 addr = { 0 };
280 char *dest = STR2CSTR(host);
281
282 fprintf(stderr, "setting addr info\n");
283 // set the connection parameters (who to connect to)
284 addr.l2_family = AF_BLUETOOTH;
285 addr.l2_psm = (uint8_t) FIX2UINT(port);
286 str2ba( dest, &addr.l2_bdaddr );
287
288 // connect to server
289 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
290 fprintf(stderr, "connected!\n");
291 rb_sys_fail("connect(2)");
292 }
293
294 return INT2FIX(0);
295 }
以下是发生的情况,即使从 root 运行:
bperry@bperry:~/work/rubytooth$ rvmsudo ruby sample_program.rb
6
Doing something with a port: 4097
connecting!
Getting open file
sample_program.rb:20:in `connect': closed stream (IOError)
from sample_program.rb:20:in `<main>'
bperry@bperry:~/work/rubytooth$