Ejemplo n.º 1
0
 private async void MClient_onUpdatePenController(BluetoothPenClient sender, PenUpdateInformation args)
 {
     await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
     {
         var item = lvDevices.Items.Where(p => (p as PenInformation)?.Id == args.Id);
         if (item != null)
         {
             PenInformation penInformation = item as PenInformation;
             if (penInformation != null)
             {
                 penInformation.Update(args);
             }
         }
     });
 }
Ejemplo n.º 2
0
        private async void BluetoothLEAdvertisementWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            try
            {
                if (args.AdvertisementType == BluetoothLEAdvertisementType.ConnectableUndirected)
                {
                    int protocol = Protocols.NONE;
                    if (args.Advertisement.ServiceUuids.Contains(BluetoothLePenClient.OldServiceUuidV2))
                    {
                        protocol = Protocols.V2;
                    }
                    else if (args.Advertisement.ServiceUuids.Contains(BluetoothLePenClient.ServiceUuidV2))
                    {
                        protocol = Protocols.V2;
                    }
                    else if (args.Advertisement.ServiceUuids.Contains(BluetoothPenClient.ServiceUuidV1))
                    {
                        protocol = Protocols.V1;
                    }

                    // mac address를 안쓰고 virtual mac address를 쓴이유는 advertisement는 굉장히 많이 들어오는 값이기 떄문에 좀더 빠르게 검색하기 위해
                    //if (penList.Exists(x => x.virtualMacAddress == args.BluetoothAddress))
                    //	return;

                    var penInformation = penList.Where(x => x.VirtualMacAddress == args.BluetoothAddress).FirstOrDefault();
                    if (penInformation == null)
                    {
                        BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                        string mac = string.Empty;
                        foreach (var data in args.Advertisement.DataSections)
                        {
                            if (data.DataType == 0xFF)
                            {
                                mac = BufferToMacAddress(data.Data);
                                break;
                            }
                        }

                        if (ValidateMacAddress(mac))
                        {
                            lock (deviceLock)
                            {
                                if (penList.Where(x => x.VirtualMacAddress == args.BluetoothAddress).Count() <= 0)
                                {
                                    penInformation = PenInformation.Create(bleDevice.DeviceInformation, bleDevice.Name, args.BluetoothAddress, mac, args.RawSignalStrengthInDBm, protocol, true);
                                    penList.Add(penInformation);
                                    onAddPenController(this, penInformation);
                                }
                            }
                        }
                    }
                    else
                    {
                        BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                        var update = PenUpdateInformation.Create(bleDevice?.DeviceId, true);
                        update.Rssi = args.RawSignalStrengthInDBm;

                        onUpdatePenController?.Invoke(this, update);
                    }
                }
                else if (args.AdvertisementType == BluetoothLEAdvertisementType.ScanResponse)
                {
                    // ScanResponse에 name이 추가정보로 오는데 DeviceInformaion을 가져오면 Name을 알 수 있기 때문에 추가로 받아 처리할 필요는 아직까진 없다.
                    var penInformation = penList.Where(x => x.VirtualMacAddress == args.BluetoothAddress).FirstOrDefault();
                    if (penInformation == null)
                    {
                        return;
                    }

                    BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                    var update = PenUpdateInformation.Create(bleDevice?.DeviceId, true);
                    update.Rssi = args.RawSignalStrengthInDBm;
                    if (!string.IsNullOrEmpty(bleDevice.Name))
                    {
                        update.ModelName = bleDevice.Name;
                    }
                    else
                    {
                        foreach (var data in args.Advertisement.DataSections)
                        {
                            if (data.DataType == 0x09)
                            {
                                var b = new byte[data.Data.Length];
                                using (var reader = DataReader.FromBuffer(data.Data))
                                {
                                    reader.ReadBytes(b);
                                }
                                update.ModelName = Encoding.UTF8.GetString(b);
                                break;
                            }
                        }
                    }

                    onUpdatePenController?.Invoke(this, update);
                }
            }
            catch { }
        }