Can I read an iPhone beacon with Windows.Devices.B

2019-07-13 19:48发布

According to the Bluetooth Advertisement sample, I need to set the CompanyID (UInt16) and the Data (IBuffer, a UInt16 in the sample) to start watching for advertisers.

In the iPhone, I can set the beacon UUID to 4B503F1B-C09C-4AEE-972F-750E9D346784. And reading on the internet, I found Apple's company id is 0x004C, so I tried 0x004C and 0x4C00.

So, this is the code I have so far, but of course, it is not working.

var manufacturerData = new BluetoothLEManufacturerData();

// Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE
manufacturerData.CompanyId = 0x4C00;

// Finally set the data payload within the manufacturer-specific section
// Here, use a 16-bit UUID: 0x1234 -> {0x34, 0x12} (little-endian)
var writer = new DataWriter();
writer.WriteGuid(Guid.Parse("4B503F1B-C09C-4AEE-972F-750E9D346784"));

// Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception.
manufacturerData.Data = writer.DetachBuffer();

I also tried inverting the bytes in the UUID:

writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467"));

Not success so far. Am I mixing two completely different technologies?

1条回答
Anthone
2楼-- · 2019-07-13 20:28

The most important thing you need to do to detect Beacons on Windows 10 is to use the new BluetoothLeAdvertisementWatcher class.

The code in the question seems focussed on setting up a filter to look for only specific Bluetooth LE advertisements matching a company code and perhaps a UUID contained in the advertisement. While this is one approach, it isn't strictly necessary -- you can simply look for all Bluetooth LE advertisements, then decode them to see if they are beacon advertisements.

I've pasted some code below that shows what I think you want to do. Major caveat: I have not tested this code myself, as I don't have a Windows 10 development environment. If you try it yourself and make corrections, please let me know and I will update my answer.

private BluetoothLEAdvertisementWatcher bluetoothLEAdvertisementWatcher;

public LookForBeacons() {
    bluetoothLEAdvertisementWatcher = new BluetoothLEAdvertisementWatcher();
    bluetoothLEAdvertisementWatcher.Received += OnAdvertisementReceived;
    bluetoothLEAdvertisementWatcher.Start();
}


private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) {

    var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
    if (manufacturerSections.Count > 0) {
        var manufacturerData = manufacturerSections[0];
        var data = new byte[manufacturerData.Data.Length];
        using (var reader = DataReader.FromBuffer(manufacturerData.Data)) {
            reader.ReadBytes(data);

            // If we arrive here we have detected a Bluetooth LE advertisement
            // Add code here to decode the the bytes in data and read the beacon identifiers

        }
    }
}

The next obvious question is how do you decode the bytes of the advertisement? It's pretty easy to search the web and find out the byte sequence of various beacon types, even proprietary ones. For the sake of keeping this answer brief and out of the intellectual property thicket, I'll simply describe how to decode the bytes of an open-source AltBeacon advertisement:

18 01 be ac 2f 23 44 54 cf 6d 4a 0f ad f2 f4 91 1b a9 ff a6 00 01 00 02 c5 00

This is decoded as:

  • The first two bytes are the company code (0x0118 = Radius Networks)
  • The next two bytes are the beacon type code (0xacbe = AltBeacon)
  • The next 16 bytes are the first identifier 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6
  • The next 2 bytes are the second identifier 0x0001
  • The next 2 bytes are the third identifier 0x0002
  • The following byte is the power calibration value 0xC5 -> -59 dBm
查看更多
登录 后发表回答