怎么用android获取android.bluetooth.le的信号强度

评论-3568&
trackbacks-6
&前言   本章内容是
android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译。蓝牙设备类,代表了蓝牙通讯国足中的远端设备。版本为 Android
r1,翻译来自中山大学的"Android Club SYSU",欢迎访问他们的WIKI:,再次感谢"Android Club SYSU"!期待你一起参与Android中文API的翻译,联系我 。
& 声明   欢迎转载,但请保留文章原始出处:)     Android Club SYSU:
    博客园:     Android中文翻译组:
& 正文   一、结构
public static class BluetoothDevice extends Object implements Parcelable
java.lang.Object
android.bluetooth.BluetoothDevice
&  二、概述
  代表一个远程蓝牙设备。让你创建一个带有各自设备的BluetoothDevice或者查询其皆如名称、地址、类和连接状态等信息。
  对于蓝牙硬件地址而言,这个类仅仅是一个瘦包装器。这个类的对象是不可改变的。这个类上的操作会使用这个用来创建BluetoothDevice类的BluetoothAdapter类执行在远程蓝牙硬件上。
  为了获得BluetoothDevice,类,使用BluetoothAdapter.getRemoteDevice(String)方法去创建一个表示 已知MAC地址的设备(用户可以通过带有BluetoothAdapter类来完成对设备的查找)或者从一个通过 BluetoothAdapter.getBondedDevices()得到返回值的有联系的设备集合来得到该设备。
  注意:需要权限
  参见             &  三、常量
ACTION_ACL_CONNECTED &
广播活动:指明一个与远程设备建立的低级别(ACL)连接。
总是包含附加域
ACL连接通过Android蓝牙栈自动进行管理
需要权限接收
&&&& 常量值: "android.bluetooth.device.action.ACL_CONNECTED"
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
ACTION_ACL_DISCONNECTED &&&&
广播活动:指明一个来自于远程设备的低级别(ACL)连接的断开
总是包含附加域
ACL连接通过Android蓝牙栈自动进行管理
需要权限接收
常量值: "android.bluetooth.device.action.ACL_DISCONNECTED"
ACTION_ACL_DISCONNECT_REQUESTED &&&
广播活动:指明一个为远程设备提出的低级别(ACL)的断开连接请求,并即将断开连接。
对于友好的断开连接,该常量是有作用的。应用程序可以用它作为暗示去马上中断对远程设备的高级别的连接(RFCOMM,L2CAP,或者其它连接)。
总是包含附加域
需要权限接收
常量值: "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"
ACTION_BOND_STATE_CHANGED &&&&&&&
广播活动:指明一个远程设备的连接状态的改变。比如,当一个设备已经被匹配。
总是包含EXTRA_DEVICE, EXTRA_BOND_STATE和 EXTRA_PREVIOUS_BOND_STATE.这些附加域。
需要权限接收
常量值: "android.bluetooth.device.action.BOND_STATE_CHANGED"
ACTION_CLASS_CHANGED &&
广播活动:一个已经改变的远程设备的蓝牙类。
总是包含EXTRA_DEVICE和EXTRA_BOND_STATE这些附加域。
需要权限接收
&常量值: "android.bluetooth.device.action.CLASS_CHANGED"
ACTION_FOUND &&&
广播活动:发现远程设备
当一个远程设备在查找过程中被发现时,发送该常量值。
总是包含和这些附加域。如果可用的话,也可包含和/或这些附加域。
需要权限接收
常量值: "android.bluetooth.device.action.FOUND"
ACTION_NAME_CHANGED &
广播活动:指明一个远程设备的昵称第一次找到,或者自从最后一次找到该昵称开始已经改变。
总是包含EXTRA_DEVICE和EXTRA_NAME这些附加域
需要权限接收
常量值: "android.bluetooth.device.action.NAME_CHANGED"
int &&&&&& BOND_BONDED &&&
指明远程设备已经匹配。
一个共享的连接键为了远程设备而存在于本地,因而设备间的通讯可以被认证和加密。
和远程设备的匹配并不意味着设备间已经成功连接。它只意味着匹配过程已经在稍早之前完成,并且连接键已经存储在本地,准备在下次连接的时候使用。
常量值: 12 (0x0000000c)
int &&&&&& BOND_BONDING &
指明和远程设备的匹配正在进行中
常量值: 11 (0x0000000b)
int &&&&&& BOND_NONE &&&&&&&&&
指明远程设备未被匹配。
不存在为了远程设备而已经共享的连接键,因而设备间的通讯(如果完全被允许)不可被认证和加密。
&&&& 常量值: 10 (0x0000000a)
Creator&BluetoothDevice& &&&&&&& CREATOR &&&&&&&
int &&&&&& ERROR &&
该 类的错误标志值. 标记该类的错误值。确保和该类中的任意其它整数常量不相等。它为需要一个标记错误值的函数提供了便利。例如: Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
常量值: - (0x)
EXTRA_BOND_STATE &&&&
作为一个的整型附加域。包含了远程设备的匹配状态。
可能值有: BOND_NONE, BOND_BONDING, BOND_BONDED.
常量值: "android.bluetooth.device.extra.BOND_STATE"
EXTRA_CLASS &&&&&&&&
作为一个和的Parcelabe 附加域。
常量值: "android.bluetooth.device.extra.CLASS"
EXTRA_DEVICE &&&&&
每次通过该类进行广播时,作为Parcelable BluetoothDevice的附加域。它包含了该常量适用的BluetoothDevice类。
常量值: "android.bluetooth.device.extra.DEVICE"
EXTRA_NAME &&&&&&&
作为ACTION_NAME_CHANGED和ACTION_FOUND的字符串附加域。它包含了这个蓝牙昵称。
常量值: "android.bluetooth.device.extra.NAME"
EXTRA_PREVIOUS_BOND_STATE &
作为ACTION_BOND_STATE_CHANGED的整型附加域。包含了远程设备以前的匹配状态。
可能值有: BOND_NONE, BOND_BONDING, BOND_BONDED.
常量值: "android.bluetooth.device.extra.PREVIOUS_BOND_STATE"
EXTRA_RSSI &&
作为ACTION_FOUND的可选短整型附加域。包含了被蓝牙硬件通知的远程设备的RSSI(Receive Signal Strength Indication,接收信号强度指示)值。
常量值: "android.bluetooth.device.extra.RSSI"&&  四、公共方法  &&&&   public
BluetoothSocketcreateRfcommSocketToServiceRecord (UUID uuid)&
&&&& 该方法是为了使用带有listenUsingRfcommWithServiceRecord(String, UUID)方法来进行对等的蓝牙应用而设计的。
&&&& 使用connect()初始化这个外界连接。它也将执行一个已给与UUID的SDP查找,从而确定连接到哪个通道上。
&&&& 远程设备将被认证,在这个端口上的通讯会被加密。
&&&& 提示:如果你正试图连接蓝牙串口,那么使用众所周知的SPP UUID 0-805F9B34FB。但是你如果正试图连接Android设备那么请你生成你自己的专有UUID。
&&&& 需要权限。
&&&&&&&&&&&&& 参数
&&&&&&&&&&&&&&&&&&&&&& uuid 查询RFCOMM通道的服务记录UUID
&&&&&&&&&&&&& 返回值
&&&&&&&&&&&&&&&&&&&&&& 一个准备好外界连接的RFCOMM蓝牙服务端口
&&&&&&&&&&&&& 异常
&&&&&&&&&&&&&&&&&&&&&& IOException&&&& 出现错误,比如蓝牙模块不可用,或者许可无效。
&&&& public int describeContents ()
描述了包含在Parcelable's marshalled representation中的特殊对象的种类。
&&&& 返回值
一个指示被Parcelabel所排列的特殊对象类型集合的位屏蔽。
&&&& public boolean equals (Object o)
比较带有特定目标的常量。如果他们相等则标示出来。 为了保证其相等,o必须代表相同的对象,该对象作为这个使用类依赖比较的常量。通常约定,该比较既需要可复制、相等和可传递。另外,没有对象引用的时候null等于null。
默认实现是返回ture,仅当this == o。如果你想实现你自己的equals方法,参见。
当且仅当o是一个作为接收器(使用==操作符来做比较)的精确相同的对象是,这个对象的实现才返回true值。子类通常实现equals(Object)方法,这样它才会重视这两个对象的类型和状态。
通常约定,对于equals(Object)和hashCode() 方法,如果equals对于任意两个对象返回真值,那么hashCode()必须对这些对象返回相同的纸。这意味着对象的子类通常都覆盖或者都不覆盖这两个方法。
o 需要对比常量的对象
如果指定的对象和该对象相等则返回true,否则返回false。
public String getAddress ()
返回该蓝牙设备的硬件地址
例如: "00:11:22:AA:BB:CC".
字符串类型的蓝牙硬件地址
&&&& public BluetoothClass getBluetoothClass ()
获取远程设备的蓝牙类
需要权限。
蓝牙类对象出错时返回空值
public int getBondState ()
获取远程设备的连接状态。
连接状态的可能值有: BOND_NONE, BOND_BONDING, BOND_BONDED.
需要权限。
&&&& 连接状态。
&&&& public String getName ()
获取远程设备的蓝牙昵称。
当执行设备扫描的时候,本地适配器将自动寻找远程名称。该方法只返回来自存储器中该设备的名称。
需要权限。
蓝牙昵称,如果出现问题则返回null。
&&&& public int hashCode ()
返回该对象的一个整型哈希值. 通常约定,如果equals对于任意两个对象返回真值,那么hashCode()必须对这些对象返回相同的值。这意味着对象的子类通常都覆盖或者都不覆盖这两个方法。
注意:除非同等对比信息发生改变,否则哈希码不随时间改变而改变。
如果你想要实现你自己的哈希码方法,参见。
该对象的哈希值
&&&& public String toString ()
返回该蓝牙设备的字符串表达式。
这是一个蓝牙硬件地址,例如"00:11:22:AA:BB:CC".然而,如果用户明确需要蓝牙硬件地址以防以后表达式会改变的话,用户总是需要使用方法。
该蓝牙设备的字符串表达式。
&&&& public void writeToParcel (Parcel out, int flags)
将类的数据写入外部提供的Parcel中
out& 对象需要被写入的Parcel
      && flags& 和对象需要如何被写入有关的附加标志。可能是0,或者可能是&  五、补充
&&&&&&&& &&&&&&&& 文章精选
&&&&&&&&&&&&&&&&&& &&&&&&&&
&&&&&&&&&&&&&&&&&& &&&&&&&&
阅读(...) 评论()当前访客身份:游客 [
当前位置:
发布于 日 14时,
iOS&BLE&4.0&实现搜索周边蓝牙设备并显示其信号强度(RSSI)源码:https://git.oschina.net/laughingzhong/MyBluetoothDemo.git
代码片段(1)
1.&[代码][Objective-C]代码&&&&
#import "ViewController.h"
#import "TableViewCell.h"
#import "PeripheralViewController.h"
#define ScanTimeInterval 1.0
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *devicesA
@property (nonatomic,strong) CBCentralManager *centralM
@property (nonatomic,strong) CBPeripheral *selectedP
@property (nonatomic,strong) NSTimer *scanT
@implementation ViewController
- (void)dealloc
_devicesArray =
_centralManager =
_selectedPeripheral =
_scanTimer =
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_devicesArray = [[NSMutableArray alloc] initWithCapacity:1];
[self initWithLeftBarButton];
[self initWithRightBarButton];
[self initWithTableView];
[self initWithCBCentralManager];
#pragma mark - UI
- (void)initWithLeftBarButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
[button setBackgroundColor:[UIColor clearColor]];
[button setTitle:@"搜索" forState:UIControlStateNormal];
[button addTarget:self action:@selector(startScanPeripherals) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:item];
- (void)initWithRightBarButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
[button setBackgroundColor:[UIColor clearColor]];
[button setTitle:@"停止" forState:UIControlStateNormal];
[button addTarget:self action:@selector(stopScan) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:item];
- (void)initWithTableView
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
[_tableView setBackgroundColor:[UIColor clearColor]];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[_tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
if (_tableView && _tableView.superview != self.view) {
[self.view addSubview:_tableView];
NSArray *h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)];
NSArray *v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)];
[self.view addConstraints:h];
[self.view addConstraints:v];
#pragma mark - ScanTimer
- (void)startScanPeripherals
if (!_scanTimer) {
_scanTimer = [NSTimer timerWithTimeInterval:ScanTimeInterval target:self selector:@selector(scanForPeripherals) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_scanTimer forMode:NSDefaultRunLoopMode];
if (_scanTimer && !_scanTimer.valid) {
[_scanTimer fire];
- (void)stopScan
if (_scanTimer && _scanTimer.valid) {
[_scanTimer invalidate];
_scanTimer =
[_centralManager stopScan];
#pragma mark - CBCentralManager
- (void)initWithCBCentralManager
if (!_centralManager) {
dispatch_queue_t queue = dispatch_get_main_queue();
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:@{CBCentralManagerOptionShowPowerAlertKey:@YES}];
[_centralManager setDelegate:self];
- (void)scanForPeripherals
if (_centralManager.state == CBCentralManagerStateUnsupported) {//设备不支持蓝牙
}else {//设备支持蓝牙连接
if (_centralManager.state == CBCentralManagerStatePoweredOn) {//蓝牙开启状态
//[_centralManager stopScan];
[_centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]}];
- (void)connectPeripheral
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark - UITableView Datasource && Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return _devicesArray.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
if (section == 0) {
return @"Peripherals Nearby";
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 50.0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellID = @"cellID";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
NearbyPeripheralInfo *info = [_devicesArray objectAtIndex:indexPath.row];
[cell setPeripheral:info];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
if (_centralManager.state == CBCentralManagerStateUnsupported) {//设备不支持蓝牙
}else {//设备支持蓝牙连接
if (_centralManager.state == CBCentralManagerStatePoweredOn) {//蓝牙开启状态
//连接设备
NearbyPeripheralInfo *info = [_devicesArray objectAtIndex:indexPath.row];
[_centralManager connectPeripheral:info.peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES,CBConnectPeripheralOptionNotifyOnNotificationKey:@YES,CBConnectPeripheralOptionNotifyOnDisconnectionKey:@YES}];
#pragma mark - CBCentralManager Delegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
switch (central.state) {
case CBCentralManagerStatePoweredOff:
NSLog(@"CBCentralManagerStatePoweredOff");
case CBCentralManagerStatePoweredOn:
NSLog(@"CBCentralManagerStatePoweredOn");
case CBCentralManagerStateResetting:
NSLog(@"CBCentralManagerStateResetting");
case CBCentralManagerStateUnauthorized:
NSLog(@"CBCentralManagerStateUnauthorized");
case CBCentralManagerStateUnknown:
NSLog(@"CBCentralManagerStateUnknown");
case CBCentralManagerStateUnsupported:
NSLog(@"CBCentralManagerStateUnsupported");
//发现蓝牙设备
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
NSLog(@"\nperipheral is :\n%@\nadvertisementData is :\n%@\nRSSI is :%d",peripheral,advertisementData,[RSSI intValue]);
BOOL isExist = NO;
NearbyPeripheralInfo *info = [[NearbyPeripheralInfo alloc] init];
info.peripheral =
info.advertisementData = advertisementD
info.RSSI = RSSI;
if (_devicesArray.count == 0) {
[_devicesArray addObject:info];
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
[_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
for (int i = 0;i & _devicesArray.i++) {
NearbyPeripheralInfo *originInfo = [_devicesArray objectAtIndex:i];
CBPeripheral *per = originInfo.
if ([peripheral.identifier.UUIDString isEqualToString:per.identifier.UUIDString]) {
isExist = YES;
[_devicesArray replaceObjectAtIndex:i withObject:info];
[_tableView reloadData];
if (!isExist) {
[_devicesArray addObject:info];
NSIndexPath *path = [NSIndexPath indexPathForRow:(_devicesArray.count - 1) inSection:0];
[_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
//连接蓝牙设备成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
NSLog(@"%s",__FUNCTION__);
[self stopScan];
_selectedPeripheral =
PeripheralViewController *viewController = [[PeripheralViewController alloc] initWithNibName:nil bundle:nil];
viewController.currentPeripheral = _selectedP
[self.navigationController pushViewController:viewController animated:YES];
//连接蓝牙设备失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
NSLog(@"%s",__FUNCTION__);
//断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
NSLog(@"%s",__FUNCTION__);
开源中国-程序员在线工具:
相关的代码(9)
[Objective-C]
[Objective-C]
[Objective-C]
[Objective-C]
0回/1119阅
[Objective-C]
[Objective-C]
[Objective-C]
[Objective-C]
[Objective-C]
开源从代码分享开始
LaughingZhong的其他代码 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
Android中蓝牙的基本使用----BluetoothAdapter类简介
下载积分:30
内容提示:Android中蓝牙的基本使用----BluetoothAdapter类简介
文档格式:PDF|
浏览次数:43|
上传日期: 20:34:47|
文档星级:
该用户还上传了这些文档
Android中蓝牙的基本使用----BluetoothAdapter类简介
官方公共微信| 漏洞检测 |
| 隐藏捆绑 |
android蓝牙4.0(BLE)开发之ibeacon初步
此文使用的ibeacon模块是april beacon,至于什么是ibeacon。本文不做解释,具体请自查。 一个april beacon里携带的信息如下 0201061AFF4CBDB88C11416BAC3FACCE020A
此文使用的模块是april beacon,至于什么是。本文不做解释,具体请自查。
一个april beacon里携带的信息如下
0201061AFF4CBDB88C11416BAC3FACCE020A
具体是什么意思呢
Number of bytes that follow in first AD structure
Flags AD type
Flags value 0x1A =
bit 0 (OFF) LE Limited Discoverable Mode
bit 1 (ON) LE General Discoverable Mode
bit 2 (OFF) BR/EDR Not Supported
bit 3 (ON) Simultaneous LE and BR/EDR to Same Device Capable (controller)
bit 4 (ON) Simultaneous LE and BR/EDR to Same Device Capable (Host)
1a Number of bytes that follow in second (and last) AD structure
前面是常规智能硬件广播包部分
ff (FF代表后面是Manufacture Data)
(组织标识,0x4c00苹果公司标识,https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers)
02 (0x02 ibeacon标识位)
15 (0x15,22个字节标识长度,uuid,major,minor总和的长度)
90 69 bd b8-8c 11-41 6b-ac 3f-33 46 8c 27 88 a3 (Proximity UUID)
04 4b(1099,major)
03 78(888,minor)
(切记这里是补码,转化为原码就是-58,iBeacon的信号发出强度值,用来作为和RSSI一起测距的基准 ,txPower)
-(32+16+8+2)
e(AprilBeacon字符串对应的十六进制)
020a(未知)
Proximity UUID :这是将你所有的beacon与其他人的beacon设备区别开的id!例如,目前在商店里某个区域分布着多个beacon形成一条“链带”,用于为顾客提供特定的服务,那么归属于同一条“链带”的beacon将分配到相同的proximity UUID。为这条“链带”设计的专用应用程序将会在后台使用这个UUID扫描到这条“链带”中的beacon设备。
major 编号:用于将相关的beacon标识为一组。例如,一个商店中的所有beacon将会分配到相同的major编号。通过这种方式,应用程序就能够知道顾客位于哪一家商店。
minor 标号:用于标识特定的beacon设备。例如一个商店中的每一个beacon设备都拥有唯一的minor编号,这样你才能够知道顾客位于商店中的哪个位置。
Measuring distance(测量距离)
最后一个值, TX power ,用于确定你和beacon之间距离有多近。根据这个值不但可以获得粗略的信息(比如靠近/远离/不在范围内等),也可以获取精确到米的距离(当然你也可以转换为以步为单位的距离)。那么如何实现?
TX power (上面例子中为0xC6=198,根据2的补码测得256-198=-58dBm)是距离设备1米测得的信号强度值(RSSI- Received Signal Strength Indication,接收到的信号强弱指标)。假如接收到的信号强度减弱了,那么我们可能在远离。只要知道1米距离的RSSI,以及当前的RSSI(我们可以从接收到的信号中一块获取到这些信息),那么计算出当前的距离是可能的。IOS已经实现了个这个功能,对于其它平台需要自己手动编码计算 。
一个简单的测距函数
protected static double calculateAccuracy(int txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
double ratio = rssi * 1.0 / txP
if (ratio < 1.0) {
return Math.pow(ratio, 10);
double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
在使用时需要加权限
关键代码如下
package cn.edu.zafu.
import .app.A
import .bluetooth.BluetoothA
import android.bluetooth.BluetoothD
import android.bluetooth.BluetoothM
import android.content.C
import android.content.I
import android.os.B
import android.util.L
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
mBluetoothAdapter.startLeScan(mLeScanCallback);
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi,
final byte[] scanRecord) {
int startByte = 2;
boolean patternFound =
// 寻找ibeacon
while (startByte >> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
return new String(hexChars);
protected static double calculateAccuracy(int txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
double ratio = rssi * 1.0 / txP
if (ratio < 1.0) {
return Math.pow(ratio, 10);
double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
至此,本文也就结束,所谓,就是获取ibeacon模块的基本信息。
(责任编辑:幽灵学院)
------分隔线----------------------------
一.概述 单例模式是设计模式中最简单的一种,但是它没有设计模式中的那种各种对象之间...
今天看到百度手机助手首页上的滑动效果非常nice,主要功能归结为: 1、当手指上划时,...
Android中的消息处理实例与分析摘要 本文介绍了Android中的消息处理机制,给出了Andro...
通常我们说一个系统不如另一个系统流畅,说的就是前者动画显示不如后者流畅,因此动画...
一、 JNI基础介绍JNI is the Java Native Interface. It defines a way for managed c...
今天我们自定义一个TextView,它的名称叫做RiseNumberTextView,我们在平时使用支付宝...
admin@1744.cc
工作日:9:00-21:00
周 六:9:00-18:00
&&扫一扫关注幽灵学院
广告服务:QQ:

我要回帖

更多关于 android4.4 bluetooth 的文章

 

随机推荐