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 == -)
{
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的更多相关文章
- Windows中使用TortoiseGit提交项目到GitLab配置
下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLab配置过程,下在全部图片希望对各位带来方便面. Gitlab默认的配置推荐使用shell命令行与server端进行交互,作 ...
- Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
为Jenkins添加Windows Slave远程执行python项目脚本 by:授客 QQ:1033553122 测试环境 JAVA JDK 1.7.0_13 (jdk-7u13-windows ...
- 怎么将linux下的项目转换成windows的VS2010下的项目?
怎么将linux下的项目转换成windows的VS2010下的项目? 不显示删除回复 显示所有回复 显示星级回复 ...
- Windows Docker 部署 Spring Boot 项目
目录 Docker Configuration Config IDEA Plugin Create Spring Boot Project Containerize It Use Dockerfile ...
- 如何在windows上把你的项目提交到github(转载)
(1)如何在windows上把你的项目提交到githubhttp://michaelye1988.iteye.com/blog/1637951 (2)github错误提示:fatal:remote o ...
- 【转】Windows中使用TortoiseGit提交项目到GitLab配置
转 原文地址 https://www.cnblogs.com/xiangwengao/p/4134492.html 下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLa ...
- 如何使用域名访问自己的Windows服务器(Java web 项目)
如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...
- Windows 10预览版14316开启Bash命令支持
00x0 前言 4月7日凌晨,微软推送了最新的Windows 10一周年更新预览版14316,其中重要的是原生支持Linux Bash命令行支持. 00x1 问题 如何开启Linux Bash命令行? ...
- C#Windows窗体应用程序MyKTV项目
后台管理其中有一个添加歌手信息和歌曲信息的窗体要点击按钮并上传文件,因为对那些文件流什么的不懂,所以用了老师教的最简单的判断方法,但此方法只是按后缀名判断文件的样式,如果后缀名乱改就不行了! 此时需要 ...
随机推荐
- BZOJ 1192 鬼谷子的钱袋 找规律
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1192 题目大意: 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向 ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
- 1067. [SCOI2007]降雨量【线段树】
Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意 Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003, ...
- C/C++读取一行
C语言 1. char buf[80]={0}; gets(buf); //可以读取空格, 回车结束输入 2. char buf[10] = {0}; scanf("%[^\n] ...
- 随手练——S(n)=O(1),判断一个链表是否为“回文”
方法一:T(n)=O(n),S(n)=O(n) 走完一遍链表,每个值入栈,之后再走一遍链表,和每次弹出的栈顶进行比较. 核心: LNode *p = l->next; while (p) { s ...
- 数论——扩展的欧几里德算法 - HDU2669
http://acm.hdu.edu.cn/showproblem.php?pid=2669 #include <iostream> using namespace std; int gc ...
- 如何批量下载bing的背景图片?
工具准备 wget(点击下载) 批处理命令(点击下载) 网友提供的接口:http://area.sinaapp.com/bingImg?daysAgo=1(1代表天数) 实现步骤 1.打开记事本,并将 ...
- PHP异步:在PHP中使用 fsockopen curl 实现类似异步处理的功能
PHP从主流来看,是一门面向过程的语言,它的最大缺点就是无法实现多线程管理,其程序的执行都是从头到尾,按照逻辑一路执行下来,不可能出现分支,这一点是限制php在主流程序语言中往更高级的语言发展的原因之 ...
- gdbt与adboost(或者说boosting)区别
boosting 是一种将弱分类器转化为强分类器的方法统称,而adaboost是其中的一种,或者说AdaBoost是Boosting算法框架中的一种实现 https://www.zhihu.com/q ...
- js apply的用法
问题: 1.apply和call的区别在哪里 2.什么情况下用apply,什么情况下用call 3.apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和ca ...