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 == -)
{
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"))[];
writeCharateristic = gattDeviceService.GetCharacteristics(Guid.Parse("00008001-0000-1000-8000-00805f9b34fb"))[];
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 = ;
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);
}

windows phone 8.1 让项目开启蓝牙genericAttributeProfile的更多相关文章

  1. Windows中使用TortoiseGit提交项目到GitLab配置

    下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLab配置过程,下在全部图片希望对各位带来方便面. Gitlab默认的配置推荐使用shell命令行与server端进行交互,作 ...

  2. Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本

    为Jenkins添加Windows Slave远程执行python项目脚本   by:授客 QQ:1033553122 测试环境 JAVA JDK 1.7.0_13 (jdk-7u13-windows ...

  3. 怎么将linux下的项目转换成windows的VS2010下的项目?

    怎么将linux下的项目转换成windows的VS2010下的项目?             不显示删除回复             显示所有回复             显示星级回复        ...

  4. Windows Docker 部署 Spring Boot 项目

    目录 Docker Configuration Config IDEA Plugin Create Spring Boot Project Containerize It Use Dockerfile ...

  5. 如何在windows上把你的项目提交到github(转载)

    (1)如何在windows上把你的项目提交到githubhttp://michaelye1988.iteye.com/blog/1637951 (2)github错误提示:fatal:remote o ...

  6. 【转】Windows中使用TortoiseGit提交项目到GitLab配置

    转  原文地址 https://www.cnblogs.com/xiangwengao/p/4134492.html   下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLa ...

  7. 如何使用域名访问自己的Windows服务器(Java web 项目)

    如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...

  8. Windows 10预览版14316开启Bash命令支持

    00x0 前言 4月7日凌晨,微软推送了最新的Windows 10一周年更新预览版14316,其中重要的是原生支持Linux Bash命令行支持. 00x1 问题 如何开启Linux Bash命令行? ...

  9. C#Windows窗体应用程序MyKTV项目

    后台管理其中有一个添加歌手信息和歌曲信息的窗体要点击按钮并上传文件,因为对那些文件流什么的不懂,所以用了老师教的最简单的判断方法,但此方法只是按后缀名判断文件的样式,如果后缀名乱改就不行了! 此时需要 ...

随机推荐

  1. Google Chrome保存插件方法

    1.拷贝下面地址到记事本 https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D~~~~%26uc ...

  2. archer 安装

    archer 项目地址: https://github.com/jly8866/archer 安装docker版本 Pull Docker docker pull hhyo/archer 启动服务do ...

  3. JavaScript的DOM_动态加载脚本和样式

    一.动态加载脚本 当网站需求变大,脚本的需求也逐步变大.我们就不得不引入太多的 JS 脚本而降低了整站的性能,所以就出现了动态脚本的概念,在适时的时候加载相应的脚本. 1.动态加载js文件 比如:我们 ...

  4. java多线程--实现Runnable接口方式

    因为java类只能继承一个类可以实现多个接口的特性,所以一般情况下不推荐使用继承Thread类实现多线程,下面是实现Runnable接口方式的简单多线程代码 package text; /** * 多 ...

  5. What Shape Layers Are-CAShapeLayer

    矢量图.gpu直接使用.占用内存小 What Shape Layers Are Shape layers are layers capable of defining shapes as vector ...

  6. BZOJ4321:queue2(DP)

    Description n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行.现在想知道,存在多少方案满足沙茶们如此不苛刻 ...

  7. Django视图(一)

    Django视图(一) 一. 概述 作用:视图接受web请求,并相应请求 本质:视图是自定义的一个python中的函数 响应内容:正常视图,重定向视图,错误视图(404,500,400) 响应过程: ...

  8. SpringMVC整合FreeMarker实例

    FreeMarker作为模板引擎,是比较常用的. FreeMarker官方文档地址为:https://freemarker.apache.org/ 现在浏览器或者翻译工具这么多,对于英文方面,我想大多 ...

  9. Freeze partial parameters while training

    1. requires_grad = False Set all parameters in the current model frozen: for p in self.parameters(): ...

  10. 网络测量中基于Sketch方法的简单介绍

    Sketch介绍 为什么要用Sketch 网络流主要根据五元组.主机地址.包的大小来分类.在网络中存在各种各样的包,如果按照上述分类方法,对每一种包都分配一个计数器来储存,虽然测量准确,那么存放计数器 ...