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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值