BLE Win API无法获取通知

时间:2017-09-18 03:22:54

标签: c# winapi bluetooth

我正在使用Win API开发一个用于BLE通信的Windows 10 destop应用程序。我使用BluetoothLEAdvertisementWatcher adWatcher扫描附近的BLE设备。在一些UI工作之后,我使用下面的代码连接到某个设备并执行对某些特性的读/写。写作部分工作正常,但是从来没有触发过ValueChanged事件,知道BLE设备应该发送通知。

我在Android平台上用Java尝试过类似的代码。写作和nofication都有效。我阅读了所有与蓝牙相关的Win API文档,但仍然无法找出代码的错误。

如果有人对此有所了解,那么请帮助我。

async public static void Connect(UInt64 bleAddress)
{
    adWatcher.Stop();
    currentBle = await BluetoothLEDevice.FromBluetoothAddressAsync(bleAddress);
    GattDeviceServicesResult result = await currentBle.GetGattServicesForUuidAsync(new Guid("0000ffe0-0000-1000-8000-00805f9b34fb"));
    if (result.Status == GattCommunicationStatus.Success)
    {
        GattCharacteristicsResult chResult = await result.Services[0].GetCharacteristicsForUuidAsync(new Guid("0000ffe1-0000-1000-8000-00805f9b34fb"));
        if (chResult.Status == GattCommunicationStatus.Success)
        {
            cha = chResult.Characteristics[0];
            Debug.WriteLine(cha.Uuid.ToString());

            cha.ValueChanged += Characteristic_ValueChanged;
            GattCommunicationStatus status = await cha.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
            DataWriter writer = new DataWriter();
            writer.WriteString("hello");
            GattCommunicationStatus wResult = await cha.WriteValueAsync(writer.DetachBuffer());

            if (status == GattCommunicationStatus.Success)
            {
                Debug.WriteLine("OK");
            }
           else
                Debug.WriteLine("error");
        }
    }
    else
        Debug.WriteLine(result.Status.ToString());
}


static void Characteristic_ValueChanged(GattCharacteristic sender,  GattValueChangedEventArgs args)
{            
    Debug.Write("Triggered!");
}

1 个答案:

答案 0 :(得分:0)

https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk阅读 这是一个已知问题。我在10分钟前遇到了同样的问题,经过三天的挣扎,然后我尝试使用Matthew Beaver的建议:

"O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)"

CoInitializeSecurity(
    absoluteSddl, // Converted from the above string.
    -1,
    nullptr,
    nullptr,
    RPC_C_AUTHN_LEVEL_DEFAULT,
    RPC_C_IMP_LEVEL_IDENTIFY,
    NULL,
    EOAC_NONE,
    nullptr);`

这就是我使用它的方式:

        [DllImport("ole32.dll")]
        public static extern int CoInitializeSecurity(IntPtr pVoid, int
            cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RPCAUTHNLEVEL level,
            RPCIMPLEVEL impers, IntPtr pAuthList, EOAUTHNCAP dwCapabilities, IntPtr
            pReserved3);

        public enum RPCAUTHNLEVEL
        {
            Default = 0,
            None = 1,
            Connect = 2,
            Call = 3,
            Pkt = 4,
            PktIntegrity = 5,
            PktPrivacy = 6
        }

        public enum RPCIMPLEVEL
        {
            Default = 0,
            Anonymous = 1,
            Identify = 2,
            Impersonate = 3,
            Delegate = 4
        }

        public enum EOAUTHNCAP
        {
            None = 0x00,
            MutualAuth = 0x01,
            StaticCloaking = 0x20,
            DynamicCloaking = 0x40,
            AnyAuthority = 0x80,
            MakeFullSIC = 0x100,
            Default = 0x800,
            SecureRefs = 0x02,
            AccessControl = 0x04,
            AppID = 0x08,
            Dynamic = 0x10,
            RequireFullSIC = 0x200,
            AutoImpersonate = 0x400,
            NoCustomMarshal = 0x2000,
            DisableAAA = 0x1000
        }

然后,在您的应用程序中开始其他任何事情之前:

 CoInitializeSecurity(
                IntPtr.Zero,
                -1,
                IntPtr.Zero,
                IntPtr.Zero,
                RPCAUTHNLEVEL.Default,
                RPCIMPLEVEL.Identify,
                IntPtr.Zero,
                EOAUTHNCAP.None,
                IntPtr.Zero);

显然现在正在运行,我仍然需要调查这将如何影响我现有的应用程序(在Windows Creator更新之前有效)。