如何配置Linux作为蓝牙RFCOMM SPP服务器?


7

我正在为Android编写一个手机应用程序,它连接到我车内的蓝牙RFCOMM设备。我的手机应用程序与它谈论AT命令。对于开发工作,我经常需要与设备通信以尝试不同的命令和事物。

我的邻居们开始觉得我很奇怪,因为我坐在车里好几个小时,我的笔记本电脑屏幕照在我的脸上,像脚本小伙子一样打字。

我宁愿配置我的许多Linux服务器中的一个作为蓝牙RFCOMM设备,并允许我连接到它(室内,当我坐在沙发上)。

我想我必须从类似的东西开始 sdptool add SP

但那又怎样?

我非常高兴编写一个perl应用程序来处理I / O,但我只是不知道如何使bluez堆栈接受连接并随后将该流传输到perl应用程序。


2
为什么不实际只是利用你的邻居认为你是脚本小子呢? ,P
n611x007

Answers:


5

使用Perl Net::Bluetooth 看起来很有希望...我正在使用以下代码,大多数是从示例中复制和粘贴,并从各种来源拼凑而成。

cat rfcomm-fake-server.pl

#! /usr/bin/perl -w

# Information Sources: 
# http://search.cpan.org/~iguthrie/Net-Bluetooth-0.40/Bluetooth.pm
# http://people.csail.mit.edu/albert/bluez-intro/x290.html#py-rfcomm-server-sdp
# http://people.csail.mit.edu/albert/bluez-intro/x232.html#rfcomm-server.py
# http://linuxdevcenter.com/pub/a/linux/2006/09/21/rediscovering-bluetooth.html?page=last


  use Net::Bluetooth;

  #### create a RFCOMM server

print "create rfcomm server\n";

  $obj = Net::Bluetooth->newsocket("RFCOMM");
  #### bind to port 1

print "binding to port 1\n";
  if($obj->bind(1) != 0) {
        die "bind error: $!\n";
  }

print "listening with backlog 2\n";
  #### listen with a backlog of 2
  if($obj->listen(2) != 0) {
        die "listen error: $!\n";
  }

print "register UUID\n";
  #### register a service
  #### $obj must be a open and bound socket
  # UUID Format: 00000000-0000-0000-0000-000000000000
  # RFCOMM:      00001101-0000-1000-8000-00805F9B34FB
  my $service_obj = Net::Bluetooth->newservice($obj, "00001101-0000-1000-8000-00805F9B34FB", "FAKEOBD", "Fake OBD Adapter");
print "Now what?\n";
  unless(defined($service_obj)) {
    print "There was a problem registering the UUID...\n";
    die ("Couldn't register UUID/service");
        #### couldn't register service
  }

  #### accept a client connection
print "Blocking until we receive an incoming connection";
  $client_obj = $obj->accept();
  unless(defined($client_obj)) {
        die "client accept failed: $!\n";
  }

  #### get client information
  my ($caddr, $port) = $client_obj->getpeername();

  print "Connected to $caddr on port $port\n";

  #### create a Perl filehandle for reading and writing
  *CLIENT = $client_obj->perlfh();
  print CLIENT "Hello there?";

while (<CLIENT>) {
    print "Data: "
}

哦,你需要在某些消息中添加“\ n”,否则在进程退出之前屏幕不会刷新。
HRJ

4

我通过custtre尝试了Perl脚本,但无法使其工作。问题是Net :: Bluetooth模块没有正确地使用SDP注册类。

最后,我发现这个Java示例完美运行:

http://www.jsr82.com/jsr-82-sample-spp-server-and-client/

请注意,它要求您在Linux上安装BlueCove jar。如果你的linux有“bluez”堆栈,那么你需要BlueCove调用的两个jar

  • bluecove-version.jar
  • bluecove-GPL-version.jar

1
好贴!你对perl / SDP完全正确。我忘记了我的帖子,并且没有更新它,因为我最终不得不修补perl net :: bluetooth以便正确注册。我把补丁发给了开发者,他说他会发布它。请将您的电子邮件发送给我,如果您愿意,我会直接发送给您。
regulatre

@regularte谢谢,但我对Java感觉更舒服。你的Perl脚本是一个很好的起点。虽然我输了一天,但我也学到了很多东西!哦,我的用例与你的用例非常相似。用于望远镜传感器的Android-app,但我没有它们。因此必须在Linux上模拟:)
HRJ

3

虽然这是一个相当古老的问题,但“如何使bluez堆栈接受连接并随后将该流传输到perl应用程序”的答案是 rfcomm。您必须将SDP记录缩小到您将要使用的特定频道,例如:

sdptool add --channel 23 SP

然后跑 rfcomm 像这样( hci0 作为您选择的蓝牙设备):

rfcomm watch hci0 23 yourperlscript.pl {}

哪里 {} 将被连接的插座设备替换,例如 /dev/rfcomm0。通过将其附加到调用 yourperlscript.pl,该套接字名称将作为脚本的第一个命令行参数传递。我不太喜欢perl,但你应该能够创建一个绑定到给定设备的对象。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.