示例#1
0
        async Task <bool> ConnectGearVR(DeviceInformation info)
        {
            try
            {
                Console.WriteLine(string.Format("Trying to connect {0} {1}", info.Name, info.Id));
                service = await AsTask(GattDeviceService.FromIdAsync(info.Id));

                if (service == null)
                {
                    Console.WriteLine("Error: Another program is using the device.");
                    return(false);
                }

                //
                // Get characteristics
                //

                var list = service.GetCharacteristics(Constants.GEARVR_DATA_CHARACTERISTICS_UUID);
                if (list.Count == 0)
                {
                    Console.WriteLine("Error: No characteristics. uuid=GEARVR_DATA_CHARACTERISTICS_UUID.");
                    return(false);
                }
                gearvrDataCharacteristic = list[0];

                list = service.GetCharacteristics(Constants.GEARVR_COMMAND_CHARACTERISTICS_UUID);
                if (list.Count == 0)
                {
                    Console.WriteLine("Error: No characteristics. uuid=GEARVR_COMMAND_CHARACTERISTICS_UUID.");
                    return(false);
                }
                gearvrCommandCharacteristic = list[0];

                //
                // Set callbak
                //

                gearvrDataCharacteristic.ValueChanged += CallbackGearVR;

                var status = await AsTask(gearvrDataCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify));

                if (status == GattCommunicationStatus.Unreachable)
                {
                    Console.WriteLine("Error: Failed to enable notify.");
                    return(false);
                }

                //
                // Start to get sensor data
                //

                status = await gearvrCommandCharacteristic.WriteValueAsync(getBuffer(Constants.GEARVR_COMMAND_VR_MODE));

                if (status == GattCommunicationStatus.Unreachable)
                {
                    Console.WriteLine("Error: Failed to enable notify.");
                    return(false);
                }

                status = await gearvrCommandCharacteristic.WriteValueAsync(getBuffer(Constants.GEARVR_COMMAND_SENSOR));

                if (status == GattCommunicationStatus.Success)
                {
                    Console.WriteLine("Connected.");
                    AHRS       = new AHRS.MadgwickAHRS(1f / Constants.GEARVR_HZ, 0.01f);
                    type       = Constants.TYPE_GEARVR;
                    bdaddr     = BDAddrToString(service.Device.BluetoothAddress);
                    deviceId   = service.Device.DeviceId;
                    deviceName = info.Name;
                    return(true);
                }
                else
                {
                    Console.WriteLine("Error: Cannot connect. (Timeout)");
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Caught exception. " + e.Message);
            }

            return(false);
        }