public void Insert(BluetoothServiceItem item)
 {
     if (item != null)
     {
         allDevices.Add(item);
         item.SendingMessage  += (o, e) => AppendLog($"本机=>{((BluetoothServiceItem)o).Device.FriendlyName}的{((BluetoothServiceItem)o).FriendlyName}", e);
         item.ReceivedMessage += (o, e) =>
         {
             AppendLog($"{((BluetoothServiceItem)o).Device.FriendlyName},{DateTime.Now.TimeOfDay.TotalMilliseconds},{e}");
             //AppendLog(((BluetoothServiceItem)o).Device.FriendlyName, e);
         };
     }
 }
        public static async Task <BluetoothServiceItem> GetBluetoothServiceItemAsync(BluetoothAddress address, Guid service)
        {
            BluetoothServiceItem item = new BluetoothServiceItem();

            item.FriendlyName = BluetoothService.GetName(service);
            item.endPoint     = new BluetoothEndPoint(address, service);
            try
            {
                BluetoothClient client = new BluetoothClient();
                await Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, item.endPoint, null);

                item.peerStream       = client.GetStream();
                item.reader           = new StreamReader(item.peerStream, Encoding.ASCII);
                item.writer           = new StreamWriter(item.peerStream, Encoding.ASCII);
                item.writer.AutoFlush = true;
                return(item);
            }
            catch (Exception) { return(null); }
        }
        public static async Task <BluetoothDeviceItem> GetBluetoothDeviceItemAsync(BluetoothDeviceInfo info)
        {
            BluetoothDeviceItem device = new BluetoothDeviceItem()
            {
                FriendlyName = info.DeviceName, DeviceInfo = info
            };
            List <Guid> services = device.DeviceInfo.InstalledServices.ToList();

            if (!services.Contains(BluetoothService.SerialPort))
            {
                services.Add(BluetoothService.SerialPort);
            }

            foreach (Guid service in services)
            {
                BluetoothServiceItem bluetoothService = await BluetoothServiceItem.GetBluetoothServiceItemAsync(device.DeviceInfo.DeviceAddress, service);

                if (bluetoothService != null)
                {
                    bluetoothService.AppendLog("系统", $"{device.FriendlyName}的{bluetoothService.FriendlyName}服务已加入");
                    bluetoothService.Device = device;
                    await bluetoothService.BeginReceiveMessageAsync();

                    device.Services.Add(bluetoothService);
                }
            }

            if (device.Services.Count == 0)
            {
                return(null);
            }
            else
            {
                return(device);
            }
        }