예제 #1
0
파일: loveToy.cs 프로젝트: XAYRGA/loveTalk
        public async Task <bool> connect()
        {
            Debug.WriteLine("Connecting to GATT");
            await btDevice.Gatt.ConnectAsync();

            for (int i = 0; i < rxCharacteristics.Length; i++)
            {
                var service = serviceUID[i];
                var rxChar  = rxCharacteristics[i];
                var txChar  = txCharacteristics[i];
                try
                {
                    Debug.WriteLine("Connecting to service");
                    btSevice = await btDevice.Gatt.GetPrimaryServiceAsync(BluetoothUuid.FromGuid(new Guid(service)));

                    if (btSevice == null)
                    {
                        continue; // skip iteration, it was bad.
                    }
                } catch { Debug.WriteLine("Failed to connect to toy");  continue; }
                try
                {
                    Debug.WriteLine("Getting TXRX");
                    btRxChr = (await btSevice.GetCharacteristicAsync(new Guid(rxChar)));
                    btTxChr = (await btSevice.GetCharacteristicAsync(new Guid(txChar)));
                    if (btRxChr == null || btTxChr == null)
                    {
                        continue; // skip iteration, not good.
                    }
                    Debug.WriteLine("Success, got TXRX");
                    break;
                }
                catch { Debug.WriteLine("Failed to connect to toy (characteristic)");  continue; }
            }
            if (btSevice == null || btRxChr == null || btTxChr == null)
            {
                Debug.WriteLine("Service or characteristic was null!");
                return(false);
            }
            var devInfo = sendCommand("DeviceType;").Result;

            Model = lookupToyName(devInfo[0]);
            Debug.WriteLine($"Toy type connected . . . {Model}");
            return(true);
        }
예제 #2
0
        async Task Send(byte[] byteArray)
        {
#if disableBluetoothLights
#else
            await GetDevice();

            if (device == null)
            {
                return;
            }

            const string  STR_LightControlServiceId = "69400001-b5a3-f393-e0a9-e50e24dcca99";
            BluetoothUuid bluetoothUuid             = BluetoothUuid.FromGuid(new Guid(STR_LightControlServiceId));
            GattService   controlService            = await device.Gatt.GetPrimaryServiceAsync(BluetoothUuid.FromGuid(new Guid(STR_LightControlServiceId)));

            if (controlService != null)
            {
                const string       STR_CharacteristicID = "69400002-b5a3-f393-e0a9-e50e24dcca99";
                GattCharacteristic gattCharacteristic   = await controlService.GetCharacteristicAsync(BluetoothUuid.FromGuid(new Guid(STR_CharacteristicID)));

                await gattCharacteristic.WriteValueWithoutResponseAsync(byteArray);
            }
#endif
        }