public Device(Plugin.BLE.Abstractions.Contracts.IDevice device, string macAddress = null) { NativeDevice = device; Id = NativeDevice.Id.ToString(); Name = NativeDevice.Name; Rssi = NativeDevice.Rssi; MacAddress = macAddress; }
public async void ConnectAsync() { var ble = CrossBluetoothLE.Current; var status = ble.State; if (status == Plugin.BLE.Abstractions.Contracts.BluetoothState.Off) { return; } var adapter = CrossBluetoothLE.Current.Adapter; ble.StateChanged += (s, e) => { Debug.WriteLine($"The bluetooth state changed to {e.NewState}"); }; adapter.DeviceDiscovered += (s, a) => { device = a.Device; Debug.WriteLine($"Bluetooth found {device.Name}"); adapter.StopScanningForDevicesAsync(); label.Text = device.Name; }; Debug.WriteLine("STARTING SCANNING"); await adapter.StartScanningForDevicesAsync(); try { Debug.WriteLine("CONNECTING TO DEVICE"); await adapter.ConnectToDeviceAsync(device); } catch (DeviceConnectionException e) { Debug.WriteLine($"Error connecting: {e.Message}"); } var service = await device.GetServiceAsync(Guid.Parse(SERVICE_UUID)); Debug.WriteLine($"Services: {service}"); var write = await service.GetCharacteristicAsync(Guid.Parse(CHAR_W_UUID)); await write.WriteAsync(Encoding.ASCII.GetBytes("start")); var notify = await service.GetCharacteristicAsync(Guid.Parse(CHAR_N_UUID)); notify.ValueUpdated += (o, args) => { string text = Encoding.ASCII.GetString(args.Characteristic.Value); //Debug.WriteLine($"VALUE: {text}"); Device.BeginInvokeOnMainThread(() => { label.Text = text; }); }; await notify.StartUpdatesAsync(); }
public Device(Plugin.BLE.Abstractions.Contracts.IDevice device) : base(device, ((BluetoothDevice)device.NativeDevice).Address.Replace(":", string.Empty).ToUpper()) { }