Read advertisement packet in Android

前端 未结 3 918
梦毁少年i
梦毁少年i 2021-01-31 00:30

I\'m working on a BLE sensor that is advertising manufacturer specific data. Is there any sample code that demonstrates how to receive an advertisement packet in Android and par

3条回答
  •  悲哀的现实
    2021-01-31 01:14

    ADPayloadParser in nv-bluetooth parses the payload of an advertising packet and returns a list of AD structures. The AD structure format is described in "11 ADVERTISING AND SCAN RESPONSE DATA FORMAT" of "Bluetooth Core Specification 4.2".

    The following code snippet is an implementation example of onLeScan method.

    public void onLeScan(
        BluetoothDevice device, int rssi, byte[] scanRecord)
    {
        // Parse the payload of the advertising packet.
        List structures =
            ADPayloadParser.getInstance().parse(scanRecord);
    
        // For each AD structure contained in the advertising packet.
        for (ADStructure structure : structures)
        {
            if (structure instanceof IBeacon)
            {
                // iBeacon packet was found.
                handleIBeacon((IBeacon)structure);
            }
            ......
        }
    }
    

    You can register a parser of your own for your manufacturer-specific format into ADPayloadParser. Refer to the following links for more information.

    Blog: http://darutk-oboegaki.blogspot.jp/2015/03/ibeacon-as-kind-of-ad-structures.html

    GitHub: https://github.com/TakahikoKawasaki/nv-bluetooth

    JavaDoc: http://takahikokawasaki.github.io/nv-bluetooth/

    Maven: http://search.maven.org/#search|ga|1|a%3A%22nv-bluetooth%22

提交回复
热议问题