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

reg*_*tre 7 linux bluetooth

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

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

我更愿意将我的众多 Linux 服务器之一配置为蓝牙 RFCOMM 设备并允许我连接到它(在室内,当我坐在沙发上时)。

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

但是然后呢?

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

reg*_*tre 5

使用 PerlNet::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: "
}
Run Code Online (Sandbox Code Playgroud)