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. https nginx 设置

    https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubunt ...

  2. chrome最新版49跨域问题

    chrome最新版49跨域问题   一.最新版49要用新的参数 加--user-data-dirwindows:"C:\Program Files\Google\Chrome\Applica ...

  3. Mac OS Yosemite(10.10.3)系统下环境配置

    /etc/bash_profile #Java export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_65.jdk/Contents/ ...

  4. iOS 技能分类:

    1.语言与系统: 2.架构与机制: 3.性能:cpu.gpu.io.缓存.内存:性能监测工具: 4.知名开源库:

  5. MAC 下 安装redis 并配置 php redis 扩展

    下载 redis redis-3.1.2.tgz sudo tar -xzf redis-3.1.2.tgz cd redis-3.1.2 sudo phpize grep: /usr/include ...

  6. Gradle初步

    一.介绍 Gradle是一个基于 JVM 的富有突破性构建工具. 它为您提供了: 一个像 ant 一样,通用的灵活的构建工具 一种可切换的,像 maven 一样的基于约定优于配置的构建框架 强大的多工 ...

  7. 框架 Hibernate 2

    持久化类百度百科 http://baike.baidu.com/link?url=gNus-8jhK0Mi401aIR-16YUAnWKzOJfeMagUV8_t5iG8235JyjMrmZPd7rI ...

  8. Gradle Goodness: Run a Build Script With a Different Name

    Normally Gradle looks for a build script file with the name build.gradle in the current directory to ...

  9. oracle同义词语句备份

    --创建同义词create synonym T_SYSTEM_USERDEPARTMENT for xtzl.T_SYSTEM_USERDEPARTMENT;--查询同义词SELECT * FROM ...

  10. Oracle 存储结构三

    Oracle数据库服务器自动管理空间的方法 段空间的分配 空间以区间的形式分配给段,区间是一组连续的Oracle块.每个数据文件都有一个位图,来描述文件中块的状态,块可能是空闲的,也可能是区间中已分配 ...