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项目
后台管理其中有一个添加歌手信息和歌曲信息的窗体要点击按钮并上传文件,因为对那些文件流什么的不懂,所以用了老师教的最简单的判断方法,但此方法只是按后缀名判断文件的样式,如果后缀名乱改就不行了! 此时需要 ...
随机推荐
- Python错误和异常概念(总)
转载请标明出处: http://www.cnblogs.com/why168888/p/6435956.html 本文出自:[Edwin博客园] Python错误和异常概念(总) 1. 错误和异常的处 ...
- Web App, Native APP,Hybird App 介绍
一.Web App 这个主要是采用统一的标准的 HTML,JavaScript.CSS 等 web 技术开发. 用户无需下载,通过不同平台 的浏览器访问来实现跨平台, 同时可以通过浏览器支持充分使用 ...
- POJ-3662 Telephone Lines---二分+最短路+最小化第k+1大
题目链接: https://cn.vjudge.net/problem/POJ-3662 题目大意: 求一条路径从1到n使第k+1大的边最小. 解题思路: 二分答案mid,当原边权小于等于mid新边权 ...
- map详讲<二>
查找元素: Map可以根据健来查找元素,提供方法find(key),如果是这个健对应的元素存在,则返回的是这个健的迭代器iterator,否则返回的是std::end(): 使用find()函数有点笨 ...
- .net core RabbitMQ 消息队列
上篇我们说到erlang的安装,现在有了基础前提,就可以继续安装RabbitMQ了! 这里我选用的RabbitMQ版本是: PS:这个RabbitMQ版本是要对应前面erlang版本,所以前面我们安装 ...
- Linux Notes | Linux常用命令行笔记
[ show all running processes ] (1) ps -aux | less 'ps' means: Process Status The -a option tells ps ...
- 对极几何(Epipolar Geometry)
基本概念 对极几何(Epipolar Geometry)是Structure from Motion问题中,在两个相机位置产生的两幅图像的之间存在的一种特殊几何关系,是sfm问题中2D-2D求解两帧间 ...
- objc单例的两种安全实现方案
所有转出博客园,请您注明出处:http://www.cnblogs.com/xiaobajiu/p/4122034.html objc的单例的两种安全实现方案 首先应该知道单例的实现有两大类,一个是懒 ...
- SQLServer禁用、启用外键约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ---启用or禁用指定表所有外键约束 alter table PUB_STRU NOCHECK constrai ...
- js中的冒泡排序
<!-- 冒泡排序:把一组数列按照一定的顺序进行排列,从大到小,或者从小到大 --> // 控制循环的轮数 arr = [3,14,55,2,1,4,5 ...