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); }
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 }
private async void MainPage_Appearing(object sender, EventArgs e) { foreach (var prop in typeof(GattServiceUuids).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)) { BluetoothUuid val = (BluetoothUuid)prop.GetValue(null); ushort shortVal = (ushort)val; BluetoothUuid convertedVal = shortVal; } Bluetooth b = new Bluetooth(); b.AdvertisementReceived += B_AdvertisementReceived; //b.RequestLEScan(new BluetoothLEScan() { AcceptAllAdvertisements = true }); var device = await b.RequestDevice(new RequestDeviceOptions() { AcceptAllDevices = true }); if (device != null) { var servs = await device.Gatt.GetPrimaryServices(); foreach (var serv in servs) { System.Diagnostics.Debug.WriteLine($"{serv.Uuid} Primary:{serv.IsPrimary}"); if (serv.Uuid == GattServiceUuids.Battery) { var batchar = await serv.GetCharacteristicAsync(GattCharacteristicUuids.BatteryLevel); var batval = await batchar.ReadValueAsync(); //battery percent stored as a single byte System.Diagnostics.Debug.WriteLine($"Battery: {batval[0]}"); } System.Diagnostics.Debug.Indent(); foreach (var chars in await serv.GetCharacteristicsAsync()) { System.Diagnostics.Debug.WriteLine($"{chars.Uuid} UserDescription:{chars.UserDescription} Properties:{chars.Properties}"); var val = await chars.ReadValueAsync(); System.Diagnostics.Debug.WriteLine(ByteArrayToString(val)); if (chars.Uuid == GattCharacteristicUuids.DeviceName) { Debug.WriteLine($"DeviceName:{System.Text.Encoding.UTF8.GetString(val)}"); } System.Diagnostics.Debug.Indent(); foreach (var descriptors in await chars.GetDescriptorsAsync()) { System.Diagnostics.Debug.WriteLine($"Descriptor:{descriptors.Uuid}"); var val2 = await descriptors.ReadValueAsync(); if (descriptors.Uuid == GattDescriptorUuids.ClientCharacteristicConfiguration) { System.Diagnostics.Debug.WriteLine($"Notifying:{val2[0] > 0}"); } else if (descriptors.Uuid == GattDescriptorUuids.CharacteristicUserDescription) { System.Diagnostics.Debug.WriteLine($"UserDescription:{ByteArrayToString(val2)}"); } else { System.Diagnostics.Debug.WriteLine(ByteArrayToString(val2)); } } System.Diagnostics.Debug.Unindent(); } System.Diagnostics.Debug.Unindent(); } } }