Example #1
0
        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);
        }
Example #2
0
        public static bool checkPlug()
        {
            if (selectedDevice == null)
            {
                return(false);
            }
            Console.WriteLine("Trying PrimaryService at 5a300001-0023-4bd4-bbd5-a6920e4c5653");
            var devsvc = selectedDevice.Gatt.GetPrimaryServiceAsync(BluetoothUuid.FromGuid(new Guid("5a300001-0023-4bd4-bbd5-a6920e4c5653"))).Result;

            Console.WriteLine("Primary service successfully allocated.");
            Console.WriteLine("Getting service characteristics...");

            try
            {
                plugComm = devsvc.GetCharacteristicsAsync().Result[0];
                Console.WriteLine("Got characteristic service " + plugComm.Uuid);
            }
            catch
            {
                return(false);
            }
            return(false);
        }
        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
        }