配对BlueTooth LE并获得Gatt /自定义设备的特征和描述符下一步是什么?

时间:2017-01-04 05:27:00

标签: c# .net winforms bluetooth gatt

这是我原来问题中的一个新问题: How to Bind manually to a BlueTooth Low Energy Device in a WinForm using C#?

使用:Windows 10,C#.Net 2015社区,WinForms应用程序。

该应用程序当前处理以下功能没有问题:

1)接收BLE的BLE广告,这些广告既可以绑定也可以不绑定到窗口。

2)然后,我可以通过广告回调连接广告BLE并通过代码绑定到它或继续下一步。

3)检索所有GATT服务以及相应的特征和描述符。

让我解释一下自定义设备:它是TI SmartRF06 Eval Board Ti CC2650BLE chip。 该服务的工作方式与串行相似,但已超过BLE。 Ti器件需要单字节代码。基于该代码,它将在TI器件上运行某些功能,然后返回结果。结果将是一个Byte [],我们有特殊的类可以接收并解析数据。

如果我们使用BLE2COMM类型的板并通过串口连接,它就像一个快照,因为我只是打开一个COMM端口并使用简单的读/写命令。

它应该通过GATT服务工作。这就是我被困住的地方。

更多信息:

对于诸如“设备名称”或“设备固件版本”之类的简单属性,它们具有简单的特征,其中我只是执行一个Characteristics.ReadValueAsync()。请看下面的关贸总协定服务。任何类型为“READ”或“READ / WRITE”的东西都可以轻松读/写,当有一个值时我会得到它。

所以我理解那部分。

根据开发人员的说法,我连接的自定义BLE设备似乎有一个自定义GATT,它是GUID:9804c436-45ed-50e2-b577-c43ccb17079d。当然,如果我使用GetGattService查找GatService,我确实看到它并且它有两个特征(Notify / Indicate)。

我在线阅读我需要写入特性并为返回的数据设置回调。

我已经连接了回调,如:

1:await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); //1
2:characteristic.ValueChanged += stData_ValueChanged; //2
3:DataWriter writer = new DataWriter(); //3
4:writer.WriteBytes(new byte[] { 0x01 }); //4
5:var buf = writer.DetachBuffer(); //5
6:WriteRslt = await characteristic.WriteValueAsync(buf,GattWriteOption.WriteWithResponse); //6

所以,在上述引发之后,我希望我的“stData_ValueChanged”函数能够触发。它没有。而且,第1行和第5行失败。在我的代码中,我将它们包装在try / catch中。

错误:

  

WriteClientChar错误:属性值长度无效   操作。 (HRESULT异常:0x8065000D)

     

写入字节:结果(错误):错误:属性值长度为   对操作无效。 (HRESULT异常:0x8065000D)

我确实尝试了不同长度的Byte []长度。

以下是实际代码:

  private async void BtAddRx2(BluetoothLEAdvertisementWatcher bw, BluetoothLEAdvertisementReceivedEventArgs args)
        {


            // BluetoothDevice btDevice;
            if (bw.Status != BluetoothLEAdvertisementWatcherStatus.Started) return; //Can't run this is already stopped.

            bw.Stop();

            device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

            if (device.DeviceInformation.Pairing.IsPaired == false)
            {

                var handlerPairingRequested = new TypedEventHandler<DeviceInformationCustomPairing, DevicePairingRequestedEventArgs>(handlerPairingReq);
                device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;
                var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);
                device.DeviceInformation.Pairing.Custom.PairingRequested -= handlerPairingRequested; //Don't need it anymore once paired.
                System.Threading.Thread.Sleep(5000); //try 3 second delay.
                device.Dispose();
                device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
            }
            //Now Paired.  Lets get all Chars       


            var GatService = device.GetGattService(Guid.Parse("9804c436-45ed-50e2-b577-c43ccb17079d"));
            var GattChars = GatService.GetAllCharacteristics();
            log("GattChars Count: " + GattChars.Count);
            c1 = GattChars[0];
            var d1 = c1.GetAllDescriptors()[0];
            c2 = GattChars[1];
            var d2 = c2.GetAllDescriptors()[0];
            byte[] datarslt1, datarslt2;

            log("d1 total Descs[0] :" + c1.GetAllDescriptors().Count);

            log("d2 total Descs[0] :" + c2.GetAllDescriptors().Count);

            //online examples state I should be able to write to the "Notify" Characterisitic which is C2.
            //I happen to know that C2 is the one I want so below I will be refering to it only.

            if (c2.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
            {
                //we have isolated the Notify Char.
                log("Notify - Uuid: " + c2.Uuid);

                IAsyncOperation<GattReadClientCharacteristicConfigurationDescriptorResult> cs2Char;
                GattCommunicationStatus  cs2;
                try
                {

                    cs2Char = c2.ReadClientCharacteristicConfigurationDescriptorAsync();
                    log("Read Client Chars: " + cs2Char.GetResults().ClientCharacteristicConfigurationDescriptor.ToString());

                    cs2 = await c2.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                }
                catch (Exception eee)
                { log("WriteClientChar Error:" + eee.Message); }

                c2.ValueChanged += stData_ValueChanged; //will alert us when data changes.
                                                        //set the notify enable flag per examples


                GattCommunicationStatus WriteRslt = 0;
                DataWriter writer = new DataWriter();
                writer.WriteBytes(new byte[] { 0x01 });
                var buf = writer.DetachBuffer();
                try
                {
                    log("Writting Bytes: ");                    
                    WriteRslt = await d2.WriteValueAsync(buf);
                    log("Result(good): " + WriteRslt.ToString());
                }
                catch (Exception ee)
                { log("Result(bad): " + WriteRslt.ToString() + "   Error:" + ee.Message); }
            }
        }

这是我被困的地方。下面是使用c#.net 4.5在Windows 10上使用简单的WinFormApp从设备中取出的服务/ gatts。

> Total Gatt services found: 4 Local Name: NASACPAD-1sd  HEX: 4E 41 53
> 41 43 50 41 44 2D 31 73 64 
>      Gatt Device Address: 00001800-0000-1000-8000-00805f9b34fb
>      Total Characteristics: 3
>           Char:   00002a00-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a00-0000-1000-8000-00805f9b34fb
>                Data returned : 4E 41 53 41 43 50 41 44 2D 31 73 64 
>                ASCII Value   : NASACPAD-1sd
>           Char:   00002a01-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a01-0000-1000-8000-00805f9b34fb
>                Data returned : 00 00 
>                ASCII Value   :   
>           Char:   00002a04-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a04-0000-1000-8000-00805f9b34fb
>                Data returned : 50 00 A0 00 00 00 E8 03 
>                ASCII Value   : P       
>      Gatt Device Address: 00001801-0000-1000-8000-00805f9b34fb
>      Total Characteristics: 0 found it
>      Gatt Device Address: 9804c436-45ed-50e2-b577-c43ccb17079d
>      Total Characteristics: 2
>           Char:   00002a1c-0000-1000-8000-00805f9b34fb      CharProps: Indicate
>           UUID: 00002a1c-0000-1000-8000-00805f9b34fb
>           Total Descriptors: 1
>                12 Plain
>           Char:   00002a1e-0000-1000-8000-00805f9b34fb      CharProps: Notify
>           UUID: 00002a1e-0000-1000-8000-00805f9b34fb
>           Total Descriptors: 1
>                15 Plain
>      Gatt Device Address: 0000180a-0000-1000-8000-00805f9b34fb
>      Total Characteristics: 9
>           Char:   00002a23-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a23-0000-1000-8000-00805f9b34fb
>                Data returned : 00 00 00 00 00 00 00 00 
>                ASCII Value   :         
>           Char:   00002a24-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a24-0000-1000-8000-00805f9b34fb
>                Data returned : 4D 6F 64 65 6C 20 4E 75 6D 62 65 72 
>                ASCII Value   : Model Number
>           Char:   00002a25-0000-1000-8000-00805f9b34fb      CharProps: Read, Write
>           UUID: 00002a25-0000-1000-8000-00805f9b34fb
>                Write Bytes: 30 30 30 30 30 30 30 31 
>                Data returned : 30 30 30 30 30 30 30 31 
>                ASCII Value   : 00000001
>           Char:   00002a26-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a26-0000-1000-8000-00805f9b34fb
>                Data returned : 46 69 72 6D 77 61 72 65 20 52 65 76 69 73 69 6F 6E 
>                ASCII Value   : Firmware Revision
>           Char:   00002a27-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a27-0000-1000-8000-00805f9b34fb
>                Data returned : 48 61 72 64 77 61 72 65 20 52 65 76 69 73 69 6F 6E 
>                ASCII Value   : Hardware Revision
>           Char:   00002a28-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a28-0000-1000-8000-00805f9b34fb
>                Data returned : 30 2E 30 2E 33 
>                ASCII Value   : 0.0.3
>           Char:   00002a29-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a29-0000-1000-8000-00805f9b34fb
>                Data returned : 4D 69 72 69 6F 6E 20 54 65 63 68 6E 6F 6C 6F 67 69 65 73 
>                ASCII Value   : Mirion Technologies
>           Char:   00002a2a-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a2a-0000-1000-8000-00805f9b34fb
>                Data returned : FE 00 65 78 70 65 72 69 6D 65 6E 74 61 6C 
>                ASCII Value   :   experimental
>           Char:   00002a50-0000-1000-8000-00805f9b34fb      CharProps: Read
>           UUID: 00002a50-0000-1000-8000-00805f9b34fb
>                Data returned : 01 0D 00 00 00 10 01 
>                ASCII Value   :

如果您看到Guid:9804c436-45ed-50e2-b577-c43ccb17079d,那就是我应该使用的那个。

有谁能让我知道下一步是什么?也许我根本不应该使用关贸总协定?虽然我不知道有任何其他方式连接到BLE设备。我觉得BLE和windows10的信息非常稀缺。其中大部分只是重复并指回相同的样本。我找不到自定义设备充当服务器的示例,只是根据写入特征的值发送数据。

0 个答案:

没有答案