扫描使用通用信标库(Scanning for beacons using universal bea

2019-10-30 12:50发布

我想实现移动应用(iPhone上),仅仅扫描信标,并显示为每一个通知。 我与信标/蓝牙一个小白。

我实现它使用通用标库( https://github.com/andijakl/universal-beacon ),并随信附上我的IOS蓝牙实现。

我的问题是,我收到约12灯塔添加的事件,即使我只有两个(我假设它是捡了我所有的其他蓝牙设备)。 我也只在advertisement_received事件接收本地名称。

我的问题是:

  1. 我怎么区分正在添加它的灯塔?
  2. 我如何得到的唯一的ID从信的网址是什么? (它们是KONTAKT信标)

谢谢你的帮助。

我的航标工作:

    public BeaconService()
    {
        // get the platform-specific provider
        var provider = RootWorkItem.Services.Get<IBluetoothPacketProvider>();

        if (null != provider)
        {
            // create a beacon manager, giving it an invoker to marshal collection changes to the UI thread
            _manager = new BeaconManager(provider, Device.BeginInvokeOnMainThread);
            _manager.Start();

            _manager.BeaconAdded += _manager_BeaconAdded;
            provider.AdvertisementPacketReceived += Provider_AdvertisementPacketReceived;
        }
    }

我的IOS蓝牙实现:

public class iOSBluetoothPacketProvider : CocoaBluetoothPacketProvider { }

public class CocoaBluetoothPacketProvider : NSObject, IBluetoothPacketProvider
{
    public event EventHandler<BLEAdvertisementPacketArgs> AdvertisementPacketReceived;
    public event EventHandler<BTError> WatcherStopped;

    private readonly CocoaBluetoothCentralDelegate centralDelegate;
    private readonly CBCentralManager central;

    public CocoaBluetoothPacketProvider()
    {
        Debug.WriteLine("BluetoothPacketProvider()");

        centralDelegate = new CocoaBluetoothCentralDelegate();
        central = new CBCentralManager(centralDelegate, null);
    }

    private void ScanCallback_OnAdvertisementPacketReceived(object sender, BLEAdvertisementPacketArgs e)
    {
        AdvertisementPacketReceived?.Invoke(this, e);
    }

    public void Start()
    {
        Debug.WriteLine("BluetoothPacketProvider:Start()");
        centralDelegate.OnAdvertisementPacketReceived += ScanCallback_OnAdvertisementPacketReceived;

        // Wait for the PoweredOn state

        //if(CBCentralManagerState.PoweredOn == central.State) {
        //    central.ScanForPeripherals(peripheralUuids: new CBUUID[] { },
        //                                               options: new PeripheralScanningOptions { AllowDuplicatesKey = false });
        //}
    }

    public void Stop()
    {
        Debug.WriteLine("BluetoothPacketProvider:Stop()");
        centralDelegate.OnAdvertisementPacketReceived -= ScanCallback_OnAdvertisementPacketReceived;

        central.StopScan();
        WatcherStopped?.Invoke(sender: this, e: new BTError(BTError.BluetoothError.Success));
    }
}
internal class CocoaBluetoothCentralDelegate : CBCentralManagerDelegate
{
    public event EventHandler<BLEAdvertisementPacketArgs> OnAdvertisementPacketReceived;

    #region CBCentralManagerDelegate

    public override void ConnectedPeripheral(CBCentralManager central, CBPeripheral peripheral)
    {
        Debug.WriteLine($"ConnectedPeripheral(CBCentralManager central, CBPeripheral {peripheral})");
    }

    public override void DisconnectedPeripheral(CBCentralManager central, CBPeripheral peripheral, NSError error)
    {
        Debug.WriteLine($"DisconnectedPeripheral(CBCentralManager central, CBPeripheral {peripheral}, NSError {error})");
    }

    public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
    {
        Debug.WriteLine($"Cocoa peripheral {peripheral}");
        Debug.WriteLine($"Cocoa advertisementData {advertisementData}");
        Debug.WriteLine($"Cocoa RSSI {RSSI}");

        var bLEAdvertisementPacket = new BLEAdvertisementPacket()
        {
            Advertisement = new BLEAdvertisement()
            {
                LocalName = peripheral.Name,
                ServiceUuids = new List<Guid>(),
                DataSections = new List<BLEAdvertisementDataSection>(),
                ManufacturerData = new List<BLEManufacturerData>()
            },
            AdvertisementType = BLEAdvertisementType.ScanResponse,
            BluetoothAddress = (ulong)peripheral.Identifier.GetHashCode(),
            RawSignalStrengthInDBm = RSSI.Int16Value,
            Timestamp = DateTimeOffset.Now
        };

        //https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataserviceuuidskey
        //if (advertisementData.ContainsKey(CBAdvertisement.DataServiceUUIDsKey))
        //{
        //    bLEAdvertisementPacket.Advertisement.ServiceUuids.Add(
        //        item: new BLEManufacturerData(packetType: BLEPacketType.UUID16List,
        //                                      data: (advertisementData[CBAdvertisement.DataServiceUUIDsKey])));
        //}

        //https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataservicedatakey
        //if (advertisementData.ContainsKey(CBAdvertisement.DataServiceDataKey))
        //{
        //    bLEAdvertisementPacket.Advertisement.DataSections.Add(
        //        item: new BLEManufacturerData(packetType: BLEPacketType.ServiceData,
        //                                      data: advertisementData[CBAdvertisement.DataServiceDataKey]));
        //}

        //https://developer.apple.com/documentation/corebluetooth/cbadvertisementdatamanufacturerdatakey
        if (advertisementData.ContainsKey(CBAdvertisement.DataManufacturerDataKey))
        {
            bLEAdvertisementPacket.Advertisement.ManufacturerData.Add(
                item: new BLEManufacturerData(packetType: BLEPacketType.ManufacturerData,
                                              data: (advertisementData[CBAdvertisement.DataManufacturerDataKey]
                                                     as NSData).ToArray()));
        }

        // Missing CBAdvertisement.DataTxPowerLevelKey

        var bLEAdvertisementPacketArgs = new BLEAdvertisementPacketArgs(data: bLEAdvertisementPacket);
        OnAdvertisementPacketReceived?.Invoke(this, bLEAdvertisementPacketArgs);
    }

    public override void FailedToConnectPeripheral(CBCentralManager central, CBPeripheral peripheral, NSError error)
    {
        Debug.WriteLine($"FailedToConnectPeripheral(CBCentralManager central, CBPeripheral {peripheral}, NSError {error})");
    }

    public override void UpdatedState(CBCentralManager central)
    {
        switch (central.State)
        {
            case CBCentralManagerState.Unknown:
                Debug.WriteLine("CBCentralManagerState.Unknown");
                break;
            case CBCentralManagerState.Resetting:
                Debug.WriteLine("CBCentralManagerState.Resetting");
                break;
            case CBCentralManagerState.Unsupported:
                Debug.WriteLine("CBCentralManagerState.Unsupported");
                break;
            case CBCentralManagerState.Unauthorized:
                Debug.WriteLine("CBCentralManagerState.Unauthorized");
                break;
            case CBCentralManagerState.PoweredOff:
                Debug.WriteLine("CBCentralManagerState.PoweredOff");
                break;
            case CBCentralManagerState.PoweredOn:
                Debug.WriteLine("CBCentralManagerState.PoweredOn");
                central.ScanForPeripherals(peripheralUuids: new CBUUID[] { },
                                                           options: new PeripheralScanningOptions { AllowDuplicatesKey = true });
                break;
            default:
                throw new NotImplementedException();
        }
    }

    public override void WillRestoreState(CBCentralManager central, NSDictionary dict)
    {
        Debug.WriteLine($"WillRestoreState(CBCentralManager central, NSDictionary {dict})");
    }

    #endregion CBCentralManagerDelegate
}

Answer 1:

所以,如果有人正在寻找这一点。 通用灯塔库不具有转换的IOS分组到通用分组的IOS实现。 这需要落实。

  1. 我怎么区分正在添加它的灯塔?

我找了埃迪斯通包,如果发现我添加到观察的名单。

  1. 我如何得到的唯一的ID从信的网址是什么? (它们是KONTAKT信标)

你需要通过advertisementData循环与广告发送,并创建一个BLEAdvertisementDataSection。 复制帧数据作为NSData的。



文章来源: Scanning for beacons using universal beacon library