免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2061 | 回复: 0
打印 上一主题 下一主题

编译 Android Native C程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-01 13:15 |只看该作者 |倒序浏览

                刚刚学习了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
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/88891/showart_2166412.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP