windows phone 8.1 让项目开启蓝牙genericAttributeProfile
1.打开项目里面的 Package.appxmanifest 文件
找到<Capabilities>节点,添加代码如下,其中
serviceId:6006 可以自己修改值
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile"> <m2:Device Id="any"> <m2:Function Type="serviceId:6006" /> </m2:Device> </m2:DeviceCapability> </Capabilities>
2.蓝牙核心代码
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.蓝牙数据接收部分
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); }