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 IDvar 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);}

转载于:https://www.cnblogs.com/HCCZX/p/5240959.html

windows phone 8.1 让项目开启蓝牙genericAttributeProfile相关推荐

  1. Windows Mobile 与 PC之间的通过蓝牙(Bluetooth) 传输文件的开发

    背景 之前也写过一些Windows Mobile和Wince下Bluetooth开发的文章如下. .NET Compact Framework下的Bluetooth开发 之 Windows Embed ...

  2. (三)jenkins+bonobo git server+windows系统自动化部署springboot项目(远程windows自动化部署)

    前提 jenkins安装,参考:(一)jenkins+bonobo git server+windows系统自动化部署springboot项目(jenkins安装) 自动化部署,参考:(二)jenki ...

  3. 乐鑫esp8266学习rtos3.0笔记:如何在 windows 用 CMake 编译 ESP8266 项目。

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,不做开发板.仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 基于C SDK的ESP8266开发技术全系列笔记 一.N ...

  4. Vue项目 开启gzip

    Vue项目 开启gzip Nginx 动态压缩与静态压缩,显著提高前后端分离项目响应速度 Gzip Precompression模块(Gzip Precompression) nginx缓存配置及开启 ...

  5. Windows Phone 7 优秀开源项目概览 来源:http://www.cnblogs.com/porscheyin/archive/2010/12/15/1906476.html...

    Windows Phone 7 优秀开源项目概览 在介绍正题之前,先向大家推荐一个非常好的网站,它的维护者是微软前Silverlight程序经理.此网站提供了关于Windows Phone 7的大量开 ...

  6. UE4项目开启光线追踪

    UE4项目开启光线追踪 硬件要求 创建项目之后开启光线追踪 项目开启之前可以选择是否开启光线追踪 硬件要求 显卡:最低要求GTX1060 系统:Win10 1809及以上(可以再 设置=>系统= ...

  7. 【techQA】如何在Mac OS 11 Big Sur or M1芯片Mac下开启蓝牙Apt-X

    [techQ&A]如何在Mac OS 11 Big Sur or M1芯片Mac下开启蓝牙Apt-X Question Answer Question Mac下连接蓝牙耳机,音质不行啊 Ans ...

  8. win10打开蓝牙_Win10开启蓝牙移动热点共享上网教程

    MS酋长之前已经分享过Win10创建移动热点的教程,其中提到可以通过蓝牙共享上网,有小伙伴就很困惑,能创建WiFi热点谁会用蓝牙连接呢?你还别说,真有广大校园里的难兄难弟难姐难妹们需要这个.因为众所周 ...

  9. win10打开蓝牙_Win10隐藏技巧:如何一键开启蓝牙功能!

    作者:蝈蝈   来源:每天学点电脑知识(id:diannao_88) 随着win10操作系统不断的更新,现在越来越多的用户使用上了win10系统.那么,小伙伴你们都知道怎么在win10系统中开启蓝牙功 ...

  10. MFC Windows 程序设计[五十六]之开启子对话框(附源码)

    MFC Windows 程序设计[五十六]之开启子对话框 程序之美 前言 主体 运行效果 核心代码 逻辑分析 结束语 程序之美 前言 MFC是微软公司提供的一个类库(class libraries), ...

最新文章

  1. 某程序员求助:30万年包留在家乡华为,50万年包去新加坡shopee,选哪个?
  2. C# HashTable的用法总结
  3. python3 随机数模块 得到一个10位随机数
  4. java 右键菜单_界面操作--添加右键菜单
  5. 配置MySQL8.0 环境变量
  6. Java Map集合面试题汇总
  7. android 页面转换
  8. Unity OnTriggerEnter不调用
  9. python内置函数面向对象_Pyhton——面向对象进阶二:类的内置函数补充、描述符...
  10. java异常处理封装_Java统一异常处理(配置文件集中化定义)
  11. Too_many_open_files_问题的解决
  12. java 短连接+MD5加密短链接
  13. Entity Framework Fluent API
  14. 小米 samba linux,U-BOOT移植准备之(一)Samba服务器
  15. ds6708 symbol 驱动_symbol DS6708设置及使用手册中文版.pdf
  16. 解决力扣等国外网站打不开问题
  17. python接口自动化13-流量回放
  18. Scum服务器显示红色,进服务器 提示这个有大神解答一下吗
  19. 【附加作业】沈航软件工程期末附加作业
  20. oracle 11g ocp 笔记(6)-- oracle安全

热门文章

  1. 漏洞扫描与云服务器的安全防护
  2. linux下expect环境安装以及简单脚本测试
  3. java泛型好处及案例
  4. Material Design学习之 Dialog(顺便把前两天AppBarLayout没讲的部分提一提)
  5. mysql主从同步开启后的iptables的设定问题
  6. size_t_百度百科
  7. C++新标准——C++1x
  8. [征求意见]关于收录精华区文章的标准
  9. windows 下 MyEclipse 运行hadoop 出错
  10. J2EE 领域的一些技术框架结构图