Advertisement
phr3ncj

bt_server.pl

Feb 19th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.15 KB | None | 0 0
  1. use Net::Bluetooth;
  2. use strict;
  3.  
  4. #### create a RFCOMM server
  5. my $obj = Net::Bluetooth->newsocket("RFCOMM");
  6. #### bind to port 1
  7. if($obj->bind(9) != 0) {
  8.       die "bind error: $!\n";
  9. }
  10.  
  11. #### listen with a backlog of 2
  12. if($obj->listen(2) != 0) {
  13.       die "listen error: $!\n";
  14. }
  15.  
  16. #### register a service
  17. #### $obj must be a open and bound socket
  18. my $service_obj = Net::Bluetooth->newservice($obj, "00001101-0000-1000-8000-00805F9B34FB", "Test", "Test");
  19. unless(defined($service_obj)) {
  20.       #### couldn't register service
  21. }
  22.  
  23. print "waiting for client connections...\n";
  24.  
  25. while (1) {
  26.   #### accept a client connection
  27.   #### blocks here until a client connects
  28.   my $client_obj = $obj->accept();
  29.   unless(defined($client_obj)) {
  30.         die "client accept failed: $!\n";
  31.   }
  32.  
  33.   #### create a Perl filehandle for reading and writing
  34.   my $client = $client_obj->perlfh();
  35.  
  36.   my $data;
  37.   my $bytesRead;
  38.  
  39.   $bytesRead = sysread $client, $data, 80;
  40.   print "bytesRead: $bytesRead, data: $data\n";
  41.  
  42.   close ($client);
  43. }
  44.  
  45. #### stop advertising service
  46. $service_obj->stopservice();
  47. #### close server connection
  48. $obj->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement