2

I have read through many stackoverflow posts about the possibility of getting RSSI of a Bluetooth connection. It seems as though Android does not directly expose this through any API and that the only hope is to get straight to the bluez API.

Apparently, once you have a connection with a Bluetooth device, you can use hcitool to get the RSSI or link quality like:

hcitool rssi E8:06:88:2F:D1:4E

However, whenever I try to do this using hcitool, I am getting segmentation faults:

130|shell@android:/system/bin # hcitool rssi E8:06:88:2F:D1:4E
RSSI return value: 0
[1] + Stopped (signal)     hcitool rssi E8:06:88:2F:D1:4E 

Even if I try to create a connection with hcitool, instead of the BluetoothAdapter:

130|shell@android:/system/bin # hcitool cc E8:06:88:2F:D1:4E
Can't create connection: I/O error

I'm using this version of hcitool: http://code.google.com/p/androidobex/downloads/detail?name=hcitool

However, it says its the "Android dev phone version" and I am clearly using something newer than a G1. But, I can't find any other version of hcitool.

4

2 回答 2

3

欢迎来到俱乐部!我对 hcitool 有同样的问题。问题是错误的实现。

为每个命令分配一个结构 (cr),稍后在 ioctl() cmd 中使用该结构。我查看了内核实现并意识到所需的字节数大于提供的字节数。

由于我目前无法对此进行修补,因此我为自己编写了一个简单的 so lib,它修改了每个 malloc() 调用以分配额外的 100 个字节作为安全裕度。然后不会发生崩溃:

#define _GNU_SOURCE

#include <stdio.h>
#include <dlfcn.h>

static void* (*real_malloc)(size_t)=NULL;


void *malloc(size_t size) {
    if (!real_malloc) {
        real_malloc = dlsym(RTLD_NEXT, "malloc");
    }

    return real_malloc(size + 100);
}

使用以下行进行编译(或在必要时插入您的交叉编译器):

gcc memsafe.c --shared -fPIC -O2 -ldl -o libmemsafe.so

然后自己编写一个 bash 脚本来获取各种值:

#!/bin/bash

if [ ! -z "$1" ]; then
    export LD_PRELOAD="libmemsafe.so"
    hcitool cc $1
    hcitool rssi $1
    hcitool lq $1
    hcitool tpl $1 0
    hcitool tpl $1 1
else
    echo "Usage: $0 <btAddr>"
fi

如果您可以自己编译 hcitool,请查找以下所有行:

cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));

...并将它们更改为...

cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info) + 100);

如果您关心估计正确的大小,请随时更正实施。

希望这个答案仍然有帮助或将来会帮助某人

于 2014-08-06T06:55:31.220 回答
1

hcitool 源代码是 bluez 堆栈的一部分。您需要 Android NDK 针对正确版本的 linux 共享库进行编译。我已经下载了 4.0.4 的整个 android 源代码,它是一个巨大的 blob,但它确实有效。

于 2012-12-13T00:24:16.660 回答