【已解决】Bluno如何与Windows 8.1建立蓝牙连接
2014-06-262.3万
AI 快速预览详细
本文介绍了如何解决Bluno与Windows 8.1建立蓝牙连接的问题。文章详细描述了Windows 8.1支持的蓝牙协议(GATT和RFComm)、配对要求(所有编程访问的蓝牙设备必须已经配对过),以及Bluno无法被扫描到的原因。作者提供了一个简单的温度湿度以及亮度收集程序,Bluno从扩展版上的传感器收集数据并通过COM口发送给PC,PC则负责把数据记录到CSV文件中。为了解决蓝牙连接问题,建议尝试使用HID设备模式进行连接。
|
我购买了Bluno和配套扩展版,用COM口通信没有问题。写了一个简单的温度湿度以及亮度收集程序,Bluno从扩展版上的传感器收集数据并通过COM口发送给PC,PC则负责把数据记录到CSV文件中,目前这一切运行良好。 但是尝试用蓝牙连接的时候发现Windows8.1连接Bluno似乎有些问题。我对于硬件以及Bluetooth LE还是新手,所以只能把我所知道的情况写在这里,请这里的老手予以指点。 1.Windows 8.1支持GATT(Client only)和RFComm协议,不支持GAT。 2.Windows 8.1要求所有编程访问的蓝牙设备必须已经配对过。 3.Bluno无法被扫描到,所以无法完成配对。 4. HID设备模式尚未尝试,会继续进行研究。 我不想再去买一个Bluno来做主从机,非常希望能够一个电脑就直接连接我的Bluno。 |




Windows程序有些耽搁了。我会在过一阵子继续做这个程序。
目前源代码已经可以下载,如果你只是想连接Bluno进行蓝牙串口收发的话,目前的代码已经可以用来参考了。
目前代码尚处在功能测试阶段,支持扫描Bluno设备和连接,读取温度和开关蜂鸣器。实现了PlanProtocol的.NET版本。
我会在全部完成后将程序发布到windows store,同时也欢迎大家随时下载源代码。希望能够帮助到更多的程序员进入这个领域。
开发环境:
Windows 8.1,计算机上搭载支持蓝牙4的芯片(如果你的计算机没有蓝牙4的芯片,可以购买USB的蓝牙4棒子。很便宜,淘宝上白菜价)。
VS 2013
Bluno和配套扩展版(使用官方提供的Bluno扩展版演示程序)
一些注意事项:
1.必须先在windows中进行配对,配对密码000000(这点很重要)。如果出现驱动程序错误,请检查是否把工作模式切换到HID了(应该是FSM_TRANS_USB_COM_BLE模式)。如果工作模式没有问题,请到设备管理器里面删除该设备然后扫描硬件变化让windows自己重装一次驱动就好了。
2.蓝牙连接上的时候是无法Upload sketch的,请先切断连接。
3.Windows里面没有丢失链接的说法,也无法在程序里切断连接的。 我查了官方的资料,设计来就是这样,目的是由操作系统来接管这些事情,程序只需要考虑发送失败和响应超时问题即可。所以如果自己写程序,需要考虑这两点。
测试类
--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
namespace BLETest
{
class Test1
{
GattCharacteristic commandService, serialService;
public event EventHandler
private bool isConnected = false;
public async Task Connect()
{
if (isConnected)
{
return;
}
//Find the devices that expose the service
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromShortId(0xDFB0));
if (devices.Count == 0)
return;
//Connect to the service
System.Diagnostics.Debug.WriteLine(devices[0].Id);
var service = await GattDeviceService.FromIdAsync(devices[0].Id);
if (service == null)
return;
//Obtain the characteristic we want to interact with
commandService = service.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0xDFB2))[0]; //Model Number
serialService = service.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0xDFB1))[0]; //Model Number
serialService.ValueChanged += serialService_ValueChanged;
isConnected = true;
}
public void test()
{
sendCommand(commandService, "AT+PASSWORD=DFRobot\r\n");
sendCommand(commandService, "AT+CURRUART=115200\r\n");
sendCommand(serialService, "
}
private async void sendCommand(GattCharacteristic c,string content)
{
Windows.Storage.Streams.InMemoryRandomAccessStream im = new Windows.Storage.Streams.InMemoryRandomAccessStream();
Windows.Storage.Streams.DataWriter writer = new Windows.Storage.Streams.DataWriter(im);
writer.WriteString(content);
var x = await c.WriteValueAsync(writer.DetachBuffer());
}
void serialService_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
Windows.Storage.Streams.DataReader reader = Windows.Storage.Streams.DataReader.FromBuffer(args.CharacteristicValue);
string result=reader.ReadString(args.CharacteristicValue.Length);
System.Diagnostics.Debug.WriteLine(result );
if (DataArrived!=null)
{
DataArrived(this, result);
}
}
}
}
--------------------Package.appxmanifest---------
在Capabilities中下面红字部分(其实不需要那么多,不过懒得删了):
[color=red]
STATUS_DEVICE_POWER_FAILURE".
如果使用COM透传,则还是原来的样子。看来除非Bluno支持GATT模式(RFComm模式为高速连接,LE设备不支持该模式),或者Windows支持GAT,否则看来是没希望了。
@齐天大妖孽 : 很羡慕Linux这方面的跟进速度,微软在BLE方面后知后觉让人十分蛋疼。
我尝试过把芯片切换到HID模式,重新配对后服务一点也没变化,并没有增加HID设备,所以没法继续下去了。而且似乎设备断电后也不会自动重新连接,必须手动连接,不知道是硬件问题、设备驱动问题还是Windows自己的问题。
【固件已解决,已经升级到1.8】
我尝试过升级固件,目前我用的是1.6,结果系统把设备识别为一个新的Uno, 可是并没有多出一个U盘。可能是我的Windows 8.1不兼容的关系,希望官方能够尽快更新驱动解决这个问题。
最后附加我的代码,这段代码是用来扫描HID设备,得到的结果是0. 不过如果修改为获取 Generic Access (0x1800)以及那个奇怪的DFB0服务的话都可以正确获取对象。
public async void test()
{
//Find the devices that expose the service
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromShortId(0x1812));
if (devices.Count == 0)
return;
//Connect to the service
System.Diagnostics.Debug.WriteLine(devices[0].Id);
var service = await GattDeviceService.FromIdAsync(devices[0].Id);
if (service == null)
return;
//Obtain the characteristic we want to interact with
//var characteristic = service.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0x2A4D))[0];
//Windows.Storage.Streams.InMemoryRandomAccessStream im=new Windows.Storage.Streams.InMemoryRandomAccessStream();
//Windows.Storage.Streams.DataWriter writer=new Windows.Storage.Streams.DataWriter(im);
//writer.WriteString("ABC\n");
//await writer.StoreAsync();
//await writer.FlushAsync();
//await characteristic.WriteValueAsync(writer.DetachBuffer());
}
更新:
原来我的固件是1.7的,可以用官方软件更新
蓝牙升级到1.8后,把Bluno通电后 Windows可以自动连接设备了。不过切换到HID模式后设备开始报错,太累了,今天睡了。
我发现使用我的ThinkPad S230u可以发现Bluno,也可以完成配对步骤(密码为000000)。接下来我再研究研究,如果有进展我会贴到这里来。
如果你只是想进行数据透传的话,可以买个BLE-Link 当接收器