Bluetooth(3pm) | User Contributed Perl Documentation | Bluetooth(3pm) |
Net::Bluetooth - Perl Bluetooth Interface
use Net::Bluetooth; #### list all remote devices in the area my $device_ref = get_remote_devices(); foreach $addr (keys %$device_ref) { print "Address: $addr Name: $device_ref->{$addr}\n"; } #### search for a specific service (0x1101) on a remote device my @sdp_array = sdp_search($addr, "1101", ""); #### foreach service record foreach $rec_ref (@sdp_array) { #### Print all available information for service foreach $attr (keys %$rec_ref) { print "Attribute: $attr Value: $rec_ref->{$attr}\n"; } } #### Create a RFCOMM client $obj = Net::Bluetooth->newsocket("RFCOMM"); die "socket error $!\n" unless(defined($obj)); if($obj->connect($addr, $port) != 0) { die "connect error: $!\n"; } #### create a Perl filehandle for reading and writing *SERVER = $obj->perlfh(); $amount = read(SERVER, $buf, 256); close(SERVER); #### create a RFCOMM server $obj = Net::Bluetooth->newsocket("RFCOMM"); #### bind to port 1 if($obj->bind(1) != 0) { die "bind error: $!\n"; } #### listen with a backlog of 2 if($obj->listen(2) != 0) { die "listen error: $!\n"; } #### register a service #### $obj must be a open and bound socket my $service_obj = Net::Bluetooth->newservice($obj, "1101", "GPS", "GPS Receiver"); unless(defined($service_obj)) { #### couldn't register service } #### accept a client connection $client_obj = $obj->accept(); unless(defined($client_obj)) { die "client accept failed: $!\n"; } #### get client information my ($caddr, $port) = $client_obj->getpeername(); #### create a Perl filehandle for reading and writing *CLIENT = $client_obj->perlfh(); print CLIENT "stuff"; #### close client connection close(CLIENT); #### stop advertising service $service_obj->stopservice(); #### close server connection $obj->close();
This module creates a Bluetooth interface for Perl.
Net::Bluetooth works with the BlueZ libs as well as with Microsoft Windows.
If you are going to be using a Unix system, the Bluez libs can be obtained at www.bluez.org. Please make sure these are installed and working properly before you install the module. Depending on your system BlueZ maybe already installed, or you may have to build it yourself and do some configuration. You can verify that BlueZ can detect devices and services with the utilities that are included with it (hciconfig, sdptool, hcitool, etc).
If you are using Windows, please make sure you have Service Pack 2 installed and the Microsoft Platform SDK. Also please make sure the "$win_include" variable at the top of Makfile.PL is set properly. This needs to point to the SDK include directory for SP2. This is where the module will look for all the Bluetooth header files (ws2bth.h, etc).
Please check out the samples included in the samples directory for more general information.
The return value is a list which contains a hash reference for each service record found. The key/values for the hash are as follows:
"SERVICE_NAME": Service Name
"SERVICE_DESC": Service Description
"SERVICE_PROV": Service Provider
"RFCOMM": RFCOMM Port
"L2CAP": L2CAP Port
"UNKNOWN": Unknown Protocol Port
If any of the values are unavailable, the keys will not exist.
If $addr is "localhost" the call will use the local SDP server.
The bluetooth socket object is used to create bluetooth sockets and interface with them. There are two types of sockets supported, RFCOMM and L2CAP. The methods are listed below.
The service object allows you to register a service with your local SDP server. The methods are as follows:
The return value is a new service object. This will be undefined if there was an error.
All uuids used with this module can either be 128 bit values: "00000000-0000-0000-0000-000000000000" or 16 bit values: "0000". All values must be represented as strings (enclosed in quotes), and must be hexadecimal values.
Windows will not immediately return the device name if it is not already cached. Also there is no mechinism to alert the system when it has acquired the device name. Therefore you may have to call get_remote_devices() twice before the name shows up. I'll see if this can be handled better in the future.
Currently on Windows the service name and description returned by sdp_search() are not setting their terminating NULL character properly. This can result in some garbage characters at the end of the string. I am looking at parsing the raw record to fix this problem.
You need BlueZ or Microsoft Service Pack 2 installed and the Microsoft Platform SDK. Windows needs at least Perl 5.8.
Ian Guthrie IGuthrie@aol.com
Copyright (c) 2006 Ian Guthrie. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
2022-11-19 | perl v5.36.0 |