IBEACON 解析代码(C语言和LUA语言)
--[[
case 0xFF: // Manufacturer Specific Data BEACON类型必定为ADV_NONCON_IND 不可连接
//厂商代码 :https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers/
// PrintMsg(String().sprintf("厂家自定义数据:%02X %02X", buf[index+1],buf[index]));
if( buf[index]==0x4c && buf[index+1]==0x00)
{
//PrintMsg("Apple:");
/*
4C 00 # 公司的标志 (0x004C == Apple)
02 # Byte 0 of iBeacon advertisement indicator
15 # Byte 1 of iBeacon advertisement indicator
B9 40 7F 30 F5 F8 46 6E AF F9 25 55 6B 57 FE 6D # iBeacon proximity uuid
00 01# major 10001
00 01 # minor 19641
c5 # calibrated Tx Power -59
*/
if( buf[index+2]==0x02 && buf[index+3]==0x15)//0x0215,表示这个设备是苹果的iBeacon设备)
{
String uuid=String().sprintf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x",
buf[index+4], buf[index+5],buf[index+6],buf[index+7],
buf[index+8],buf[index+9],
buf[index+10],buf[index+11],
buf[index+12], buf[index+13],buf[index+14],buf[index+15], buf[index+16], buf[index+17],buf[index+18],buf[index+19]
); //xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16)
int major=buf[index+20];
major <<=8;
major |=buf[index+21];
int minor=buf[index+22];
minor <<=8;
minor |=buf[index+23];
BYTE txpwr=buf[index+24];
txpwr =~(txpwr-1);
int rssi=(char) txpwr ; //(切记这里是补码,负数的补码是数值位取反再加1,转化为原码就是-58,iBeacon的信号发出强度值,用来作为和RSSI一起测距的基准 ,txPower)
String ibstr= (String().sprintf("[IBEACON] UUID:%s MAJOR:%d MINOR:%d TX POWER:-%d ",uuid,major,minor,txpwr));
// UpdateBLENote(mac,ibstr);
blenote = ibstr;
}
}
//
if( buf[index]==0xe0 && buf[index+1]==0x00)
{
if(chkshowpkg->Checked)
{
PrintMsg(" Google Eddystone:");
}
}
]]
--将字符串按格式转为16进制串
function str2hex(str)
--判断输入类型
if (type(str) ~= 'string') then
return nil, 'str2hex invalid input type'
end
--滤掉分隔符
str = str:gsub('[%s%p]', ''):upper()
--检查内容是否合法
if (str:find('[^0-9A-Fa-f]') ~= nil) then
return nil, 'str2hex invalid input content'
end
--检查字符串长度
if (str:len() % 2 ~= 0) then
return nil, 'str2hex invalid input lenth'
end
--拼接字符串
local index = 1
local ret = ''
for index = 1, str:len(), 2 do
ret = ret .. string.char(tonumber(str:sub(index, index + 1), 16))
end
return ret
end
--[[
位运算
--与 同为1,则为1
--或 有一个为1,则为1
--非 true为 false,其余为true
--异或 相同为0,不同为1
]]
local MathBit = {}
function MathBit.__andBit(left,right) --与
return (left == 1 and right == 1) and 1 or 0
end
function MathBit.__orBit(left, right) --或
return (left == 1 or right == 1) and 1 or 0
end
function MathBit.__xorBit(left, right) --异或
return (left + right) == 1 and 1 or 0
end
function MathBit.__base(left, right, op) --对每一位进行op运算,然后将值返回
if left < right then
left, right = right, left
end
local res = 0
local shift = 1
while left ~= 0 do
local ra = left % 2 --取得每一位(最右边)
local rb = right % 2
res = shift * op(ra,rb) + res
shift = shift * 2
left = math.modf( left / 2) --右移
right = math.modf( right / 2)
end
return res
end
function MathBit:andOp(left, right)
return MathBit.__base(left, right, MathBit.__andBit)
end
function MathBit:xorOp(left, right)
return MathBit.__base(left, right, MathBit.__xorBit)
end
function MathBit:orOp(left, right)
return MathBit.__base(left, right, MathBit.__orBit)
end
function MathBit:notOp(left)
return left > 0 and -(left + 1) or -left - 1
end
function MathBit:lShiftOp(left, num) --left左移num位
return left * (2 ^ num)
end
function MathBit:rShiftOp(left,num) --right右移num位
return math.floor(left / (2 ^ num))
end
function print_r(t)
local print_r_cache = {}
local function sub_print_r(t, indent)
if (print_r_cache[tostring(t)]) then
print(indent .. '*' .. tostring(t))
else
print_r_cache[tostring(t)] = true
if (type(t) == 'table') then
for pos, val in pairs(t) do
if (type(val) == 'table') then
print(indent .. '[' .. pos .. '] => ' .. tostring(t) .. ' {')
sub_print_r(val, indent .. string.rep(' ', string.len(pos) + 8))
print(indent .. string.rep(' ', string.len(pos) + 6) .. '}')
elseif (type(val) == 'string') then
print(indent .. '[' .. pos .. '] => "' .. val .. '"')
else
print(indent .. '[' .. pos .. '] => ' .. tostring(val))
end
end
else
print(indent .. tostring(t))
end
end
end
if (type(t) == 'table') then
print(tostring(t) .. ' {')
sub_print_r(t, ' ')
print('}')
else
sub_print_r(t, ' ')
end
print()
end
str = '4C000215FDA50693A4E24FB1AFCFC6EB0764782527114CB9C5'
function ibeaconInfo(data)
local info = {}
str = str2hex(str)
local buf = {}
for i = 1, #str do
table.insert(buf, string.byte(str, i))
end
local index = 1
if (buf[index] ~= 0x4c or buf[index + 1] ~= 0x00) then
return nil
end
if (buf[index + 2] ~= 0x02 or buf[index + 3] ~= 0x15) then
return nil
end
info.uuid =
string.format(
'%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%02x%02x',
buf[index + 4],
buf[index + 5],
buf[index + 6],
buf[index + 7],
buf[index + 8],
buf[index + 9],
buf[index + 10],
buf[index + 11],
buf[index + 12],
buf[index + 13],
buf[index + 14],
buf[index + 15],
buf[index + 16],
buf[index + 17],
buf[index + 18],
buf[index + 19]
) --xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16)
info.major = buf[index + 20] * 256 + buf[index + 21]
info.minor = buf[index + 22] * 256 + buf[index + 23]
info.txpwr = buf[index + 24]
info.txpwr=MathBit:notOp(info.txpwr-1);
-- info.txpwr=MathBit:andOp(info.txpwr,0xff) ; --(切记这里是补码,负数的补码是数值位取反再加1,转化为原码就是-58,iBeacon的信号发出强度值,用来作为和RSSI一起测距的基准 ,txPower)
return info
end
res = ibeaconInfo(str)
print_r(res)
IBEACON 解析代码(C语言和LUA语言)相关推荐
- CRC校验码生成逻辑的实现原理详解——结合C语言和Verilog语言代码分析
文章目录 前言 一.CRC校验码的计算 1.CRC模型 2.CRC计算 步骤1:输入数据与初始值模2加并左移 步骤2:被除数与多项式模2除 二.CRC校验码生成逻辑的C语言实现 1.实现代码 2.代码 ...
- java语言和C语言的区别
java语言和C语言的区别 简单的说就是两种不同的语言. 区别如下: 1在运行方式上: C:原始的C是面向过程的.就是按行执行. JAVA:绝对的面向对象.就像按模块执行. (补充:当然,在C++,及 ...
- php和java语言_JAVA语言和PHP语言的比较
JAVA语言和PHP语言的比较 JAVA语言在服务器端开发的流行以及安卓客户端开发的流行,让他占据了大部分市场份额.在服务器端还有一个语言也较为流行,就是PHP.PHP语言由于简单易学,响应速度快,在 ...
- C语言和C++语言关系
C语言和C++语言关系 C语言是在实践的过程中逐步完善起来的 C语言的目标是高效 C语言和C++并不是对立的竞争关系 C语言是在实践的过程中逐步完善起来的 没有深思熟虑的设计过程 使用时存在很多&qu ...
- C 语言和C++语言的 struct对比
C 语言和C++语言的 struct对比 author: stonehat 1. C++语言将struct当成类来处理的,所以C++的struct可以包含C++类的所有东西,例如构造函数,析构函数,友 ...
- dart语言和java语言_Java不是文明语言吗?
dart语言和java语言 几周前,我有机会学习iOS编程. 我的老板认为我更像是"计算机科学家",而不是开发人员,这意味着我可以将自己的知识应用于开发一两个iPad应用程序–我要 ...
- 一步步编写操作系统 69 汇编语言和c语言共同协作 70
由于有了上一节的铺垫,本节的内容相对较少,这里给大家准备了两个小文件来实例演示汇编语言和c语言相互调用. 会两种不同语言的人,只是掌握了同一件事物的两种表达方式.人在学习一种新语言时,潜意识里是建立了 ...
- 软件工程:汇编语言和C语言在软件工程的应用,计算机学生必看!
随着科学技术不断发展,软件工程在社会发展中取得了进一步发展,软件工程专业,也在各大学生根发芽,许多大学的专业教学上也取得了良好的进展.汇编语言与C语言作为两种计算机基础语言,在推动软件工程发展等方面具 ...
- php c 交互,C语言和go语言之间的交互操作方法
一.go代码中使用C代码 go代码中使用C代码,在go语言的函数块中,以注释的方式写入C代码,然后紧跟import "C" 即可在go代码中使用C函数 代码示例: go代码:tes ...
- D语言和Go语言有前途吗?
发信人: vimer (老来多忘事,惟不忘相思), 信区: Programming 标 题: [合集] D语言和Go语言有前途吗? (转载) 发信站: 水木社区 (Wed Dec 14 21:11: ...
最新文章
- 【对讲机的那点事】关于对讲机锂电池你了解多少?
- 【转】C# using的三种使用方法
- SATA硬盘如何使用GHOSTSATA硬盘如何使用GHOST
- css background-position
- ASP.NET报错集合一----.net4.0创建项目后,在iis上部署项目,无法浏览,提示404
- C++ set insert的返回值
- linux服务器进程不存在,查看 Linux 服务器的进程
- bash 将二进制转换为十进制_6、计算机进制之二进制、十进制、十六进制之间的转换...
- 结对-贪吃蛇游戏-测试过程
- python中如何将矩阵合并并多一个维度
- memcached介绍与作用和它的工作原理
- ‘pom.xml‘ has syntax errors
- 固体激光器的转换效率
- Predictive State Temporal Difference Learning 原文翻译预测状态时间差分学习
- python实现12306余票
- 被称为中国版“Robinhood”,老虎与富途谁的低佣生意更值钱?
- 程序员需要学习英语吗?
- Android结束进程的几种方法
- CLUENER 细粒度命名实体识别baseline:BiLSTM-CRF
- 计算机丢失mfc110d.dll,msvcp110d.dll