Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322945
  • 博文数量: 41
  • 博客积分: 2540
  • 博客等级: 少校
  • 技术积分: 570
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-18 11:29
文章分类

全部博文(41)

文章存档

2011年(4)

2010年(32)

2009年(3)

2008年(2)

我的朋友

分类: LINUX

2010-02-01 13:15:23

刚刚学习了Android的编译系统。现在就来请自写个程序测试一下吧。因为正才为Android调试蓝牙。就写了个BlueZ的测试程序。程序很简单就是打印本地的所有蓝牙设备信息。

测试代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

#define HCI_MAX_DEV 16
static struct hci_dev_info di;

static void print_dev_hdr(struct hci_dev_info *di)
{
    static int hdr = -1;
    char addr[18];

    if (hdr == di->dev_id)
        return;
    hdr = di->dev_id;

    ba2str(&di->bdaddr, addr);

    printf("%s:\tType: %s\n", di->name, hci_dtypetostr(di->type));
    printf("\tBD Address: %s ACL MTU: %d:%d SCO MTU: %d:%d\n",
                        addr, di->acl_mtu, di->acl_pkts,
                                di->sco_mtu, di->sco_pkts);
}

static void print_dev_info(int ctl, struct hci_dev_info *di)
{
    struct hci_dev_stats *st = &di->stat;

    print_dev_hdr(di);

    printf("\t%s\n", hci_dflagstostr(di->flags) );

    printf("\tRX bytes:%d acl:%d sco:%d events:%d errors:%d\n",
                                    st->byte_rx, st->acl_rx, st->sco_rx, st->evt_rx, st->err_rx);

    printf("\tTX bytes:%d acl:%d sco:%d commands:%d errors:%d\n",
                                    st->byte_tx, st->acl_tx, st->sco_tx, st->cmd_tx, st->err_tx);

    printf("\n");
}

static void print_dev_list(int ctl, int flags)
{
    struct hci_dev_list_req *dl;
    struct hci_dev_req *dr;
    int i;

    if (!(dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)))) {
        perror("Can't allocate memory");
        exit(1);
    }
    dl->dev_num = HCI_MAX_DEV;
    dr = dl->dev_req;

    if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) {
        perror("Can't get device list");
        exit(1);
    }


    for (i = 0; i< dl->dev_num; i++) {
        di.dev_id = (dr+i)->dev_id;
        if (ioctl(ctl, HCIGETDEVINFO, (void *) &di) < 0)
            continue;
        if (hci_test_bit(HCI_RAW, &di.flags) &&
                    !bacmp(&di.bdaddr, BDADDR_ANY)) {
            int dd = hci_open_dev(di.dev_id);
            hci_read_bd_addr(dd, &di.bdaddr, 1000);
            hci_close_dev(dd);
        }
        print_dev_info(ctl, &di);
    }
}

int main()
{
    int ctl;
    if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
        perror("Can't open HCI socket.");
        exit(1);
    }

    print_dev_list(ctl, 0);
    return 0;
}


最重要的环节是为这个程序编写一个Android.mk。

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS) 

LOCAL_SRC_FILES := test.c 

LOCAL_MODULE := test 

LOCAL_C_INCLUDES += \                //指定头文件位置 

             $(LOCAL_PATH)/external/textbluez

LOCAL_MODULE_TAGS := eng 

LOCAL_SHARED_LIBRARIES += libbluetooth       //需要加载的so文件 

include $(BUILD_EXECUTABLE)


现在就可以编译了。

# . (YOUR_ANDROID)/build/envsetup.sh 

# lunch 2 

# mm



阅读(2443) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~