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. [零基础学JAVA]Java SE面向对象部分.面向对象基础(05)

    1.继承 2.多态 3.final 4.重载与覆写 5. this/super 6.抽象类 7.接口 java: class Person{ private String name;    priva ...

  2. CentOS7.X安装Redis-4.0.8以及Redis集群搭建

    安装redis 安装前的准备 yum install \ vim \ wget \ make \ gcc \ gcc-c++ \ automake \ autoconf \ -y \ 下载解压并安装 ...

  3. robotframwork接口测试(四)—其他库的安装

    怎么知道自己的RF已经有哪些库了,可以看python安装目录下Python27\Lib\site-packages这个文件夹,有的话就可以直接引入了. 没有的话,就安装了. 1. 命令安装:这种最方便 ...

  4. PHP面试系列之Linux(一) ----- Linux基础

    一.系统安全 sudo:以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行. su:用于变更为其他使用者的身份,除 root 外,需要键入该使用者的密码. ...

  5. 修改微软RDP远程桌面端口

    远程桌面服务所使用的通信协议是Microsoft定义RDP(Remote Desktop Protocol)协议,RDP协议的TCP通信端口号是3389. 有时候为了安全起见,或者其他的需要,我们常需 ...

  6. ASP.NET Core Middleware (转载)

    What is Middleware? Put simply, you use middleware components to compose the functionality of your A ...

  7. SSIS中出现数据流数据源假死状态的解决办法

    相信开发过Sql Server SSIS的人都遇到过在数据流中数据源假死的问题,特别是Excel Source特别容易假死,当job执行到数据流中的Excel Source时,既不报错也不执行,也没有 ...

  8. Strategy(策略)模式

    1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查找.排序等,一种常用的方法是硬编码(Hard Cod ...

  9. vue进入/离开 & 列表过渡transition

    一.transition过渡 1.需求1(优化):想要一种效果,想要ios那种页面切换效果,总而言之就是过渡效果. 附上官网介绍地址:https://cn.vuejs.org/v2/guide/tra ...

  10. vim内替换文件内容

    几个常用的方法如下: :%s/foo/bar/g 把全部foo替换为bar,全局替换 :s/foo/bar/g 当前行替换foo为bar :%s/foo/bar/gc 替换每个foo为bar,但需要确 ...