1.打開項目裏面的 Package.appxmanifest 文件app
找到<Capabilities>節點,添加代碼以下,其中async
serviceId:6006 能夠本身修改值
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile"> <m2:Device Id="any"> <m2:Function Type="serviceId:6006" /> </m2:Device> </m2:DeviceCapability> </Capabilities>
2.藍牙核心代碼ui
GattDeviceService gattDeviceService; GattCharacteristic characteristic; GattCharacteristic writeCharateristic; #region 藍牙管理 public async Task<bool> Connect(string deviceName = "mydevice") { if (IsConnected) return true; bool result = false; if (gattDeviceService != null) gattDeviceService = null; try { var devices = await GetDevices(); string s = ""; foreach (var item in devices) { s += item.Name + " ,"; } P(s); foreach (var item in devices) { if (item.Name.ToLower().Contains(deviceName.ToLower())) { //已經配對 若是找不到提示用戶先去藍牙設置配對 gattDeviceService = await GattDeviceService.FromIdAsync(item.Id); if (gattDeviceService != null) { var status = await Config(); if (status == GattCommunicationStatus.Success) return true; else { characteristic = null; return false; } } else { //提示用戶 沒法鏈接 } } } } catch { } return result; } public bool IsBluetoothOpened { get { return isBlueBootoothOpend(); } } internal bool IsConnected { get { return characteristic != null; } } /// <summary> /// 判斷藍牙是否打開 /// </summary> /// <returns></returns> private bool isBlueBootoothOpend() { try { try { PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; var peers = PeerFinder.FindAllPeersAsync().AsTask().Result; return true; } catch (Exception ex) { if ((uint)ex.HResult == 0x8007048F || ex.HResult == -2146233088) { return false; } return true; } } catch { return false; } } internal async Task<DeviceInformationCollection> GetDevices() { var serverID = @"00008009-0000-1000-7000-00805f9b8875"; //Serive ID var zeCircleGUID = GattDeviceService.GetDeviceSelectorFromUuid(Guid.Parse(serverID)); var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(zeCircleGUID); return devices; } private async Task<GattCommunicationStatus> Config() { GattCommunicationStatus status = GattCommunicationStatus.Unreachable; characteristic = null; characteristic = gattDeviceService.GetCharacteristics(Guid.Parse("00008002-0000-1000-8000-00805f9b34fb"))[0]; writeCharateristic = gattDeviceService.GetCharacteristics(Guid.Parse("00008001-0000-1000-8000-00805f9b34fb"))[0]; if (characteristic != null) { var currentDescriptorValue = await characteristic.ReadClientCharacteristicConfigurationDescriptorAsync(); if (currentDescriptorValue.Status == GattCommunicationStatus.Success) { status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status == GattCommunicationStatus.Success) characteristic.ValueChanged += characteristic_ValueChanged; } } return status; } #endregion
3.藍牙數據接收部分spa
public event EventHandler OnConnectionFailed; static UInt16 ReSendCount = 0; List<byte> receivedData = new List<byte>(); private bool IsEnableReconnect = true; //數據處理 public void characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { byte[] data = new byte[args.CharacteristicValue.Length]; DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(data); //讀取數據 receivedData.AddRange(data); }