下面我附上代码:
#include
#include
#include
int sdp_ch(char *bdaddr)
{
uuid_t svc_uuid;
int err;
bdaddr_t target;
sdp_list_t *response_list = NULL, *search_list, *attrid_list;
sdp_session_t *session = 0;
uint32_t class = 0;
int num;
int turn_d;
/* hard code your remote bdaddr here */
char serv_class_ID[]="0x1106"; /* obex push service identifier */
//char bdaddr[]="00:15:83:B9:78:A6";/* remote bdaddress(Pls change for yours)*/
str2ba( bdaddr, &target );//-------------------------------------------------------------------------------------(1)
if (!strncasecmp(serv_class_ID, "0x", 2)) {
int num;
/* This is a UUID16, just convert to int */
sscanf(serv_class_ID + 2, "%X", &num);
class = num;
//printf("Service Class 0x%X\n", class);
}
if (class) {
if (class & 0xffff0000){
sdp_uuid32_create(&svc_uuid, class);
// printf("Creating a 32 bit uuid\n");
}
else {
uint16_t class16 = class & 0xffff;
sdp_uuid16_create(&svc_uuid, class16);
// printf("Creating a 16 bit uuid\n");
}
}
else
//printf("You have no class :0)\n");
// connect to the SDP server running on the remote machine
session = sdp_connect( BDADDR_ANY, &target, SDP_RETRY_IF_BUSY );//-----------------------(2)
// specify the UUID of the application we're searching for
search_list = sdp_list_append( NULL, &svc_uuid );//---------------------------------------------(3)
// specify that we want a list of all the matching applications' attributes
uint32_t range = 0x0000ffff;
attrid_list = sdp_list_append( NULL, &range );//------------------------------------(4)
// get a list of service records that have UUID 0x1106
err = sdp_service_search_attr_req( session, search_list, \
SDP_ATTR_REQ_RANGE, attrid_list, &response_list);//-------------------------------(5)
sdp_list_t *r = response_list;//--------------response_list has the record have UUID 0x1106
// go through each of the service records
for (; r; r = r->next ) {
sdp_record_t *rec = (sdp_record_t*) r->data;
sdp_record_print(rec);
//printf("Service RecHandle: 0x%x\n", rec->handle);
sdp_list_t *proto_list;
// get a list of the protocol sequences
if( sdp_get_access_protos( rec, &proto_list ) == 0 ) {
sdp_list_t *p = proto_list;
// go through each protocol sequence
for( ; p ; p = p->next ) {
sdp_list_t *pds = (sdp_list_t*)p->data;
// go through each protocol list of the protocol sequence
for( ; pds ; pds = pds->next ) {
// check the protocol attributes
sdp_data_t *d = (sdp_data_t*)pds->data;
int proto = 0;
for( ; d; d = d->next ) {
switch( d->dtd ) {
case SDP_UUID16:
case SDP_UUID32:
case SDP_UUID128:
proto = sdp_uuid_to_proto( &d->val.uuid );
turn_d = d->val.int8;
break;
case SDP_UINT8:
if( proto == RFCOMM_UUID ) {
//printf("rfcomm channel: %d\n",d->val.int8);
}
turn_d = d->val.int8;
printf("%d---1",turn_d); //----------------------输出为3
break;
}
}
}
sdp_list_free( (sdp_list_t*)p->data, 0 );
}
sdp_list_free( proto_list, 0 );
}
//printf("found service record 0x%x\n", rec->handle);
sdp_record_free( rec );
}
sdp_close(session);
printf("%d----2\n",turn_d); //----------------------输出为25
return turn_d;
}
int main()
{
int i;
char bdaddr[]="00:15:83:B9:78:A6";
i=sdp_channel(bdaddr);
printf("--%d--\n",i); //-------------------------输出为25
}
运行结果:
3------1
25-----2
--25--
请高手指点是哪出错了啊,急啊!!!