linux下集成蓝牙dongle,csr
root@freescale /$ cat /etc/rc.d/init.d/bluetooth
#!/bin/sh
#
# Start/stop the Bluetooth daemons
#
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=bluetooth
DESC="Bluetooth subsystem"
HCID_NAME=hcid
SDPD_NAME=sdpd
HIDD_NAME=hidd
HID2HCI_NAME=hid2hci
RFCOMM_NAME=rfcomm
PAND_NAME=pand
DUND_NAME=dund
HCID_EXEC="`which $HCID_NAME || true`"
SDPD_EXEC="`which $SDPD_NAME || true`"
HIDD_EXEC="`which $HIDD_NAME || true`"
HID2HCI_EXEC="`which $HID2HCI_NAME || true`"
RFCOMM_EXEC="`which $RFCOMM_NAME || true`"
PAND_EXEC="`which $PAND_NAME || true`"
DUND_EXEC="`which $DUND_NAME || true`"
HCID_ENABLE=true
SDPD_ENABLE=true
HIDD_ENABLE=false
HID2HCI_ENABLE=false
RFCOMM_ENABLE=true
DUND_ENABLE=false
PAND_ENABLE=false
HCID_CONFIG="/etc/bluetooth/hcid.conf"
RFCOMM_CONFIG="/etc/bluetooth/rfcomm.conf"
HIDD_OPTIONS=""
DUND_OPTIONS=""
PAND_OPTIONS=""
[ -e /etc/default/bluetooth ] && . /etc/default/bluetooth
case "$1" in
start)
echo -n "Starting $DESC:"
if $HCID_ENABLE && [ -x "$HCID_EXEC" -a -f "$HCID_CONFIG" ] ; then
$HCID_EXEC -f $HCID_CONFIG
echo -n " $HCID_NAME"
fi
if $SDPD_ENABLE && [ -x "$SDPD_EXEC" ] ; then
$SDPD_EXEC
echo -n " $SDPD_NAME"
fi
if $HIDD_ENABLE && [ -x "$HIDD_EXEC" ] ; then
$HIDD_EXEC $HIDD_OPTIONS --server || true
echo -n " $HIDD_NAME"
fi
if $HID2HCI_ENABLE && [ -x "$HID2HCI_EXEC" ] ; then
$HID2HCI_EXEC --tohci > /dev/null 2>&1 || true
echo -n " $HID2HCI_NAME"
fi
if $RFCOMM_ENABLE && [ -x "$RFCOMM_EXEC" -a -f "$RFCOMM_CONFIG" ] ; then
$RFCOMM_EXEC -f $RFCOMM_CONFIG bind all || true
echo -n " $RFCOMM_NAME"
fi
if $DUND_ENABLE && [ -x "$DUND_EXEC" -a -n "$DUND_OPTIONS" ] ; then
$DUND_EXEC $DUND_OPTIONS
echo -n " $DUND_NAME"
fi
if $PAND_ENABLE && [ -x "$PAND_EXEC" -a -n "$PAND_OPTIONS" ] ; then
$PAND_EXEC $PAND_OPTIONS
echo -n " $PAND_NAME"
fi
echo "."
;;
stop)
echo -n "Stopping $DESC:"
killall $PAND_NAME > /dev/null 2>&1 || true
echo -n " $PAND_NAME"
killall $DUND_NAME > /dev/null 2>&1 || true
echo -n " $DUND_NAME"
if [ -x "$RFCOMM_EXEC" ] ; then
$RFCOMM_EXEC release all > /dev/null 2>&1 || true
echo -n " $RFCOMM_NAME"
fi
killall $HIDD_NAME > /dev/null 2>&1 || true
echo -n " $HIDD_NAME"
killall $SDPD_NAME > /dev/null 2>&1 || true
echo -n " $SDPD_NAME"
killall $HCID_NAME > /dev/null 2>&1 || true
echo -n " $HCID_NAME"
echo "."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
exit 0
root@freescale /$
root@freescale /$ cat /etc/bluetooth/hcid.conf
#
# HCI daemon configuration file.
#
# HCId options
options {
# Automatically initialize new devices
autoinit yes;
# Security Manager mode
# none - Security manager disabled
# auto - Use local PIN for incoming connections
# user - Always ask user for a PIN
#
security user;
# Pairing mode
# none - Pairing disabled
# multi - Allow pairing with already paired devices
# once - Pair once and deny successive attempts
pairing multi;
# PIN helper
pin_helper /usr/bin/bluepin;
# D-Bus PIN helper
#dbus_pin_helper;
}
# Default settings for HCI devices
device {
# Local device name
# %d - device id
# %h - host name
name "blue02100025";
# Local device class
class 0x3e0100;
# Default packet type
#pkt_type DH1,DM1,HV1;
# Inquiry and Page scan
iscan enable; pscan enable;
# Default link mode
# none - no specific policy
# accept - always accept incoming connections
# master - become master on incoming connections,
# deny role switch on outgoing connections
lm accept;
# Default link policy
# none - no specific policy
# rswitch - allow role switch
# hold - allow hold mode
# sniff - allow sniff mode
# park - allow park mode
lp rswitch,hold,sniff,park;
# Authentication and Encryption (Security Mode 3)
#auth enable;
#encrypt enable;
}
root@freescale /$ cat /etc/bluetooth/rfcomm.conf
#
# RFCOMM configuration file.
#
rfcomm0 {
# Automatically bind the device at startup
bind no;
# Bluetooth address of the device
device 11:22:33:44:55:66;
# RFCOMM channel for the connection
channel 1;
# Description of the connection
comment "Example Bluetooth device";
}
root@freescale /$
root@freescale /$
root@freescale /$ cat /usr/bin/bluepin
#!/bin/sh
echo "PIN:0000"
启动蓝牙:
/etc/rc.d/init.d/bluetooth start
http://people.csail.mit.edu/albert/bluez-intro/x502.html
Example 4-2. rfcomm-server.c
#include #include #include #include #include int main(int argc, char **argv) { struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; char buf[1024] = { 0 }; int s, client, bytes_read; socklen_t opt = sizeof(rem_addr); // allocate socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); // bind socket to port 1 of the first available // local bluetooth adapter loc_addr.rc_family = AF_BLUETOOTH; loc_addr.rc_bdaddr = *BDADDR_ANY; loc_addr.rc_channel = (uint8_t) 1; bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); // put socket into listening mode listen(s, 1); // accept one connection client = accept(s, (struct sockaddr *)&rem_addr, &opt); ba2str( &rem_addr.rc_bdaddr, buf ); fprintf(stderr, "accepted connection from %s\n", buf); memset(buf, 0, sizeof(buf)); // read data from the client bytes_read = read(client, buf, sizeof(buf)); if( bytes_read > 0 ) { printf("received [%s]\n", buf); } // close connection close(client); close(s); return 0; }
Example 4-3. rfcomm-client.c
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status;
char dest[18] = "01:23:45:67:89:AB";
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
// send a message
if( status == 0 ) {
status = write(s, "hello!", 6);
}
if( status < 0 ) perror("uh oh");
close(s);
return 0;
}