This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SDP with Andriod phone from SPP Demo

Hi All,

My application requires me to perform service discovery on remote Android devices. I need to search for remote channel number. I referred to following post and wrote code according to that-

http://e2e.ti.com/support/wireless_connectivity/f/660/p/169706/652369.aspx#652369

But looks like this code is for older version of Bluetopia BTPS kernel, because I could not modify _malloc function as told in the thread, I got errors when I replaced the _malloc function with the function definition given in the thread.

Following is the code I am using for starting service discovery to Android phone.

static void BTPSAPI SDP_EventCallback(
unsigned int BluetoothStackID,
unsigned int SDPRequestID,
SDP_Response_Data_t *SDP_Response_Data,
unsigned long CallbackParameter)
{
int Index;
int ServerPort;
int RFCOMMPortList[30];

/* First, check to see if the required parameters appear to be */
/* semi-valid. */
if((SDP_Response_Data != NULL) && (BluetoothStackID))
{
/* The parameters appear to be semi-valid, now check to see what */
/* type the incoming Event is. */
switch(SDP_Response_Data->SDP_Response_Data_Type)
{
case rdTimeout:
/* A SDP Timeout was received, display a message indicating */
/* this. */
Display(("\r\n"));
Display(("SDP Timeout Received (Size = 0x%04X).\r\n", sizeof(SDP_Response_Data_t)));
break;
case rdConnectionError:
/* A SDP Connection Error was received, display a message */
/* indicating this. */
Display(("\r\n"));
Display(("SDP Connection Error Received (Size = 0x%04X).\r\n", sizeof(SDP_Response_Data_t)));
break;
case rdErrorResponse:
/* A SDP error response was received, display all relevant */
/* information regarding this event. */
Display(("\r\n"));
Display(("SDP Error Response Received (Size = 0x%04X) - Error Code: %d.\r\n", sizeof(SDP_Response_Data_t), SDP_Response_Data->SDP_Response_Data.SDP_Error_Response_Data.Error_Code));
break;
case rdServiceSearchResponse:
/* A SDP Service Search Response was received, display all */
/* relevant information regarding this event */
Display(("\r\n"));
Display(("SDP Service Search Response Received (Size = 0x%04X) - Record Count: %d\r\n", sizeof(SDP_Response_Data_t), SDP_Response_Data->SDP_Response_Data.SDP_Service_Search_Response_Data.Total_Service_Record_Count));

/* First, check to see if any SDP Service Records were */
/* found. */
if(SDP_Response_Data->SDP_Response_Data.SDP_Service_Search_Response_Data.Total_Service_Record_Count)
{
Display(("Record Handles:\r\n"));

for(Index = 0; (Word_t)Index < SDP_Response_Data->SDP_Response_Data.SDP_Service_Search_Response_Data.Total_Service_Record_Count; Index++)
{
Display(("Record %u: 0x%08X\r\n", (Index + 1), (unsigned int)SDP_Response_Data->SDP_Response_Data.SDP_Service_Search_Response_Data.Service_Record_List[Index]));
}
}
else
Display(("No SDP Service Records Found.\r\n"));
break;
case rdServiceAttributeResponse:
/* A SDP Service Attribute Response was received, display */
/* all relevant information regarding this event */
Display(("\r\n"));
Display(("SDP Service Attribute Response Received (Size = 0x%04X)\r\n", sizeof(SDP_Response_Data_t)));

break;
case rdServiceSearchAttributeResponse:
/* A SDP Service Search Attribute Response was received, */
/* display all relevant information regarding this event */
Display(("\r\n"));
Display(("SDP Service Search Attribute Response Received (Size = 0x%04X)\r\n", sizeof(SDP_Response_Data_t)));


BTPS_MemInitialize(RFCOMMPortList, 0, sizeof(RFCOMMPortList));

/* Call only when you queried for Attribute ID PROTOCOL DESCRIPTOR LIST (0x0004) */
if(ExtractRFCOMMPortNumber(&SDP_Response_Data->SDP_Response_Data.SDP_Service_Search_Attribute_Response_Data, RFCOMMPortList) == 0)
{
Index = 0;

while(RFCOMMPortList[Index])
{
Display(("RFCOMM Server Port Found at: %d", RFCOMMPortList[Index]));
Index++;
}
}
else
{
Display(("No RFCOMM Server found"));
}


break;
default:
/* An unknown/unexpected SDP event was received. */
Display(("\r\n"));
Display(("Unknown SDP Event.\r\n"));
break;
}
}
else
{
/* There was an error with one or more of the input parameters. */
Display(("\r\n"));
Display(("SDP callback data: Response_Data = NULL.\r\n"));
}
}

/* The following function is responsible for issuing a Service Search*/
/* Attribute Request to a Remote SDP Server. This function returns */
/* zero if successful and a negative value if an error occurred. */
int
SPPServiceDiscovery(BD_ADDR_t RemoteBDAddr)
{
int Result;
int ret_val;
int Index;
BD_ADDR_t NullADDR;
SDP_UUID_Entry_t SDPUUIDEntry;
SDP_Attribute_ID_List_Entry_t AttributeID;

ASSIGN_BD_ADDR(NullADDR, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

/* First, check that valid Bluetooth Stack ID exists. */
if(g_uiBluetoothStackID)
{
/* First let's build the UUID 32 value(s). */
SDPUUIDEntry.SDP_Data_Element_Type = deUUID_16;
ASSIGN_UUID_16(SDPUUIDEntry.UUID_Value.UUID_16, 0x11, 0x01);

AttributeID.Attribute_Range = (Boolean_t)FALSE;
AttributeID.Start_Attribute_ID = PROTOCOL_DESCRIPTOR_LIST_ATTR;
AttributeID.End_Attribute_ID = 0;

/* Finally submit the SDP Request. */
Result = SDP_Service_Search_Attribute_Request(g_uiBluetoothStackID,
RemoteBDAddr,
1,
&SDPUUIDEntry,
1,
&AttributeID,
SDP_EventCallback,
(unsigned long)0);

if(Result > 0)
{
/* The SDP Request was submitted successfully. */
Display(("SDP_Service_Search_Attribute_Request Success.\r\n"));

/* Flag success to the caller. */
ret_val = 0;
}
else
{
/* There was an error submitting the SDP Request. */
Display(("SDP_Service_Search_Attribute_Request Failure: %d.\r\n"));

/* Flag success to the caller. */
ret_val = 0;
}

}
else
{
/* No valid Bluetooth Stack ID exists. */
ret_val = -1;
}

return(ret_val);
}


static int
ExtractRFCOMMPortNumber(SDP_Service_Search_Attribute_Response_Data_t *SDPServiceSearchAttributeResponse, int *RFCOMMPortList)
{
int NumberOfAttributes;
int NumberOfRecords;
int RFCOMMChannel;
int RetVal=-1;
int Index;
int RecordIndex;
int NumberElements;
int PortCount=0;
UUID_128_t UUID_128;
UUID_128_t RFCOMM_UUID;
SDP_Data_Element_t *SDP_Data_Element;
SDP_Data_Element_t *SDP_Data_Element1;

SDP_ASSIGN_RFCOMM_UUID_128(RFCOMM_UUID);

/* This is a response received when we queried Attribute ID 0x04 */
NumberOfRecords = SDPServiceSearchAttributeResponse->Number_Service_Records;

Display(("Number of Records: %d ", NumberOfRecords));

for(RecordIndex = 0; RecordIndex < NumberOfRecords; RecordIndex++)
{
NumberOfAttributes = SDPServiceSearchAttributeResponse->SDP_Service_Attribute_Response_Data[RecordIndex].Number_Attribute_Values;

if(NumberOfAttributes)
{
SDP_Data_Element = SDPServiceSearchAttributeResponse->SDP_Service_Attribute_Response_Data[RecordIndex].SDP_Service_Attribute_Value_Data[0].SDP_Data_Element;

if((SDP_Data_Element->SDP_Data_Element_Type == deSequence) && (SDP_Data_Element->SDP_Data_Element_Length))
{
/* Data Element Sequence Exists, now */
/* let's loop through the Data Element */
/* Sequence. */
for(Index=0; Index < SDP_Data_Element->SDP_Data_Element_Length;Index++)
{
if(((SDP_Data_Element1 = &(SDP_Data_Element->SDP_Data_Element.SDP_Data_Element_Sequence[Index])) != NULL) && (SDP_Data_Element1->SDP_Data_Element_Type == deSequence) && ((NumberElements = SDP_Data_Element1->SDP_Data_Element_Length) > 0) && ((SDP_Data_Element1 = SDP_Data_Element1->SDP_Data_Element.SDP_Data_Element_Sequence) != NULL))
{
if((SDP_Data_Element1->SDP_Data_Element_Type == deUUID_16) || (SDP_Data_Element1->SDP_Data_Element_Type == deUUID_32) || (SDP_Data_Element1->SDP_Data_Element_Type == deUUID_128))
{
/* Initialize the BASE UUID. */
SDP_ASSIGN_BASE_UUID(UUID_128);

switch(SDP_Data_Element1->SDP_Data_Element_Type)
{
case deUUID_16:
/* First normal-lize the*/
/* 16 Bit UUID to 128 */
/* Bits. */
ASSIGN_UUID_16_TO_UUID_128(UUID_128, SDP_Data_Element1->SDP_Data_Element.UUID_16);
break;
case deUUID_32:
/* First normal-lize the*/
/* 32 Bit UUID to 128 */
/* Bits. */
ASSIGN_UUID_32_TO_UUID_128(UUID_128, SDP_Data_Element1->SDP_Data_Element.UUID_32);
break;
case deUUID_128:
/* Simply assign the */
/* 128 Bit UUID to the */
/* UUID value. */
UUID_128 = SDP_Data_Element1->SDP_Data_Element.UUID_128;
break;
}

/* Now let's check to see if */
/* there is a match to RFCOMM */
if((COMPARE_UUID_128(RFCOMM_UUID, UUID_128)) && (NumberElements > 1))
{
if(SDP_Data_Element1[1].SDP_Data_Element_Type == deUnsignedInteger1Byte)
{
RFCOMMChannel = SDP_Data_Element1[1].SDP_Data_Element.UnsignedInteger1Byte;

/* Add server port to return data */
RFCOMMPortList[PortCount] = RFCOMMChannel;
PortCount++;
RetVal = 0;
}
}
}
else
{
/* Some unknown Protocol */
/* Descriptor, so just ignore */
/* it. */
}
}
}
}
}

}

return(RetVal);
}


The code is same as mentioned in the thread following is the log I am getting -
3SDP_Service_Search_Attribute_Request Success.

HCI Role Change Event
HCI Connection complete event:
Remote BD_ADDR:cc:c3:ea:54:ca:be
HCI Read_Remote_Extended_Features_Complete_Event:3
HCI Number of completed packets Event
HCI Number of completed packets Event
HCI Number of completed packets Event
HCI Number of completed packets Event
HCI Number of completed packets Event
HCI Number of completed packets EventNO SEGMENT FOUND.
Alloc Failed: 44
INVALID POINTER

SDP Service Search Attribute Response Received (Size = 0x000A)
Number of Records: 0 No RFCOMM Server found

My diagnosis is that the memory allocation is failing some where, does any one have a fix for this?

I have one more doubt about SDP_Service_Search_Attribute_Request API-
Can we use 128 bit UUID instead of UUID16?

Thanks in advance,
Sharadanand Karanjkar

  • Hi,

    The implementation that you referred to in your post is deprecated. Can you try this to search for the remote port number and see if works

     http://e2e.ti.com/support/wireless_connectivity/f/660/p/321437/1121479.aspx#1121479

    As for your alloc fail, this can be fixed by increasing the BTPS_MEMORY_BUFFER_SIZE. In IAR its found by right clicking on the project -> Options ->C/C++ Compiler -> Preprocessor.

    In CCS it can be fixed by going to Project->Properties->Build -> Advanced Options -> Predefined Symbols.

    Thanks,

    Stonestreet One.

  • Hi StoneStreet One,

    I implemented the code as suggested but I am still not able to find remote server number. Following is the log I am getting-

    HCI Role Change Event
    HCI Connection complete event:
    Remote BD_ADDR:c0:18:85:bd:12:c2
    HCI Read_Remote_Extended_Features_Complete_Event:1
    HCI Number of completed packets Event
    HCI Number of completed packets Event
    HCI Number of completed packets Event
    HCI Number of completed packets Event
    HCI Number of completed packets Event
    SDP Service Search Attribute Response Received (Size = 0x000A)
    Number of Records: 0 No RFCOMM Server found
    HCI Number of completed packets Event
    HCI Disconnection request event: 16

    Do I need to run any application on the server side so as to get the server number on the remote side?

    As I am definitely getting some response for ,my SDP commands. Is it possible that there are no SPP servers running on the remote side?

    Thanks in advance,

    Sharadanand

  • Hi Stonestreet One,


    I run the Blueterm application on my Android device and I could find the port number with the code.

    Thanks,

    Sharadanand Karanjkar