Friday 25 November 2011

USB bluetooth In android


  I have been working on enabling USB Bluetooth on a set top box(STB).
Requirements:
  1.Kernel subsystem for Bluetooth drivers i.e.
    a)Host Controller Interface(HCI).
    b) Synchronous connection-oriented (SCO) link.
    c) Logical link control and adaptation protocol (L2CAP)
    d) Radio Frequency Communication(RFCOMM)
    e)Bluetooth Netwok Encapsulation protocol(BNEP)
  2. Bluez in android/external/bluetooth/bluez/
  3. D-bus in /external/dbus

Steps taken to Enable USB Bluetooth support in Android Ginger Bread running on a Set top Box
1. Enable USB Bluetooth Support in kernel.
    Do make menuconfig in kernel for your platform.
a        Goto to Networking Support
   i)Bluetooth Subsystem support
  Enable L2CAP ,RFCOMM,SCO,BNEP


           
 ii)Bluetooth Device drivers
      Enable HCI UART  and USB Drivers



     

 iii)In Networking support



              















Enable RF switch Subsystem support for RFKILL
So After above changes final .config file should have following macros :
CONFIG_BT=y
CONFIG_BT_L2CAP=y
CONFIG_BT_SCO=y
CONFIG_BT_RFCOMM=y
CONFIG_BT_RFCOMM_TTY=y
CONFIG_BT_BNEP=y
CONFIG_BT_HCIBTUSB=y
CONFIG_BT_HCIUART=y
CONFIG_BT_HCIUART_H4=y
CONFIG_BT_HCIUART_BCSP=y
CONFIG_BT_HCIUART_LL=y
CONFIG_RFKILL=y

2.       Add BOARD_HAVE_BLUETOOTH :=true in
$GB_SOURCE\ gingerbread-2.3.4\build\target\board\generic\ BoardConfig.mk
This will compile the Bluez stack and create a libbluetoothd.so which will run as a service.
Check in Linux shell of board  \system\bin\
3.       Check whether the device is listed in sysfs when the USB BT is inserted.
Rfkill file in :        sys\class\rfkill\rfkill0
HCI file in :           sys\class\Bluetooth\hci0
The end number can vary in case of hotplug.

Now you ready to test the bluetooth on your system adb shell.Goto to the adb shell and check following command:

       # hciconfig hci0 up

       #hciconfig
         hci0:   Type: BR/EDR  Bus: USB
         BD Address: 00:16:41:97:BA:5E  ACL MTU: 1017:8  SCO MTU: 64:8
         UP RUNNING
         RX bytes:348 acl:0 sco:0 events:11 errors:0
         TX bytes:38 acl:0 sco:0 commands:11 errors:0

         If you are not seeing the above output than you need to check the configuration done above.

         for scanning other devices:

      #hcitool scan
         Scanning ...
         00:1A:1B:82:9B:6D

    #l2ping 00:1A:1B:82:9B:6D
      Ping: 00:1A:1B:82:9B:6D from 00:16:41:97:BA:5E (data size 44) ...
      44 bytes from 00:1A:1B:82:9B:6D id 0 time 23.94ms
      44 bytes from 00:1A:1B:82:9B:6D id 1 time 18.85ms
     44 bytes from 00:1A:1B:82:9B:6D id 2 time 30.88ms
     44 bytes from 00:1A:1B:82:9B:6D id 3 time 18.88ms
     44 bytes from 00:1A:1B:82:9B:6D id 4 time 17.88ms
    44 bytes from 00:1A:1B:82:9B:6D id 5 time 17.88ms
      6 sent, 6 received, 0% loss

iv)      In  $(GB_CODE)\gingerbread\root\init.rc
        i)service bluetoothd /system/bin/bluetoothd -n
          Replace it service with
         service bluetoothd /system/bin/logwrapper /system/bin/bluetoothd -d –n
         for getting logs of Bluetooth daemon.
     ii) change the permissions for  sys\class\rfkill\rfkill0\state for enabling and disabling it from Android             UI.In case of hot plug rfkill number will change so change the permissions in   system\bluetooth\bluedroid\bluetooth.c  in function  init_rfkill() instead of init.rc.

    chown bluetooth bluetooth /sys/class/rfkill/rfkill0/type
    chown bluetooth bluetooth /sys/class/rfkill/rfkill0/state
    chmod 0660 /sys/class/rfkill/rfkill0/state
                 
      
After the above changes for testing Bluetooth:
1.If you don't have SD card on board then mount the Pen drive as SD card.
2.Check the default channels for OPUSH in init.rc which is 12 .
3.In GB code the channel 12 is used by handsfree profile also(HFP) so change that channel number in headset.h to be equal to the one used in BluetoothAudioGateway.java and init.rc .
In  BluetoothAudioGateway.java
 public static final int DEFAULT_HF_AG_CHANNEL = 10;
  public static final int DEFAULT_HS_AG_CHANNEL = 11;
In headset.h
#define DEFAULT_HS_AG_CHANNEL 10;
#define DEFAULT_HF_AG_CHANNEL 11;


In case of issues can look into following files :
1.BluetoothA2dp.java
2.BluetoothA2dpService.java
3.BluetoothAudioGateway.java
4.Bluetooth.c(In Bluedroid)
5.BluetoothServerSocket.java
6.BluetoothService.java
7.android_server_BluetoothService.cpp
8.BluetoothOppRfcommListener.java
9.BluetoothOppReciever.java

Useful Links:
Bluetooth Wiki