Search code examples
pythonlinuxbluetootha2dphfp

Programmatically enable/disable Bluetooth profiles


I'm running Rasbian Jessie Lite on Raspberry Pi 3 with a USB/Bluetooth dongle (blueZ) 5.4.

The /etc/bluetooth/main.conf has Class = 0x0c0408. I have a Qt5 application which enables the Bluetooth device and accepts any incoming pairing requests.

I can successfully connect from my smartphone to all enabled Bluetooth profiles: A2DP and HFP.

Now I want to let the users select which profile(s) should be enabled. Thus I'm looking for a way to enable/disable on-the-fly A2DP and HFP. It's ok from C++, bash or python script.

I can't just change the Class value because I cannot restart the bluetooth service - I MUST keep running the GATT server.

Any thought about?


Solution

  • Enabling and disabling any profile/service in Bluez can be done using sdptool command. If you want to enable any profile/service you can use:

    sdptool add A2SRC

    In the same way to disable any service/profile you can use:

    sdptool del A2SRC
    

    More info can be found using help of sdptool

        sdptool - SDP tool v5.37
    Usage:
        sdptool [options] <command> [command parameters]
    Options:
        -h      Display help
        -i      Specify source interface
    Commands:
        search      Search for a service
        browse      Browse all available services
        records     Request all records
        add         Add local service
        del         Delete local service
        get         Get local service
        setattr     Set/Add attribute to a SDP record
        setseq      Set/Add attribute sequence to a SDP record
    
    Services:
        DID SP DUN LAN FAX OPUSH FTP PRINT HS HSAG HF HFAG SAP PBAP MAP 
        NAP GN PANU HCRP HID KEYB WIIMOTE CIP CTP A2SRC A2SNK AVRCT AVRTG 
        UDIUE UDITE SEMCHLA SR1 SYNCML SYNCMLSERV ACTIVESYNC HOTSYNC 
        PALMOS NOKID PCSUITE NFTP NSYNCML NGAGE APPLE IAP ISYNC GATT 
    

    Now, this is how you can enable and disable any profile/services.

    Moving to your second question, how to remotely let the user of smartphone to enable and disable the profile. This you can achieve through the serial port profile(SPP) in Bluetooth. Just to brief you, SPP is serial port emulation over Bluetooth. It is based on RFcomm protocol and can be used in parallel with A2DP and HFP.

    So here the idea is to create SPP connection from smartphone to RSP and then send command to enable and disable profiles. SPP can be used from command line using rfcomm command available with Bluez. More info on how to use the command can be found here:

    https://unix.stackexchange.com/questions/92255/how-do-i-connect-and-send-data-to-a-bluetooth-serial-port-on-linux

    Let me know if any further clarification you need on this.