Skip to content

Instantly share code, notes, and snippets.

@juucustodio
Last active December 5, 2019 18:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juucustodio/8e675fc2ae2aeca30ba645cc45efc2ad to your computer and use it in GitHub Desktop.
Save juucustodio/8e675fc2ae2aeca30ba645cc45efc2ad to your computer and use it in GitHub Desktop.
Example of Bluetooth in Xamarin.Forms applications - http://julianocustodio.com/bluetooth
using System;
using System.Collections.ObjectModel;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
using Xamarin.Forms;
namespace DemoBluetooth
{
public partial class MainPage
{
IAdapter adapter;
IBluetoothLE bluetoothBLE;
ObservableCollection<IDevice> list;
IDevice device;
public MainPage()
{
InitializeComponent();
bluetoothBLE = CrossBluetoothLE.Current;
adapter = CrossBluetoothLE.Current.Adapter;
list = new ObservableCollection<IDevice>();
DevicesList.ItemsSource = list;
}
private async void searchDevice(object sender, EventArgs e)
{
if (bluetoothBLE.State == BluetoothState.Off)
{
await DisplayAlert("Atenção", "Bluetooth desabilitado.", "OK");
}
else
{
list.Clear();
adapter.ScanTimeout = 10000;
adapter.ScanMode = ScanMode.Balanced;
adapter.DeviceDiscovered += (obj, a) =>
{
if (!list.Contains(a.Device))
list.Add(a.Device);
};
await adapter.StartScanningForDevicesAsync();
}
}
private async void DevicesList_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
device = DevicesList.SelectedItem as IDevice;
var result = await DisplayAlert("AVISO", "Deseja se conectar a esse dispositivo?", "Conectar", "Cancelar");
if (!result)
return;
//Stop Scanner
await adapter.StopScanningForDevicesAsync();
try
{
await adapter.ConnectToDeviceAsync(device);
await DisplayAlert("Conectado", "Status:" + device.State , "OK");
}
catch (DeviceConnectionException ex)
{
await DisplayAlert("Erro", ex.Message, "OK");
}
}
}
}
@renanct
Copy link

renanct commented Aug 6, 2018

Bom dia, você conseguiu enviar alguma string, em bytes[], para algum dispositivo?

O método IDevice.GetServiceAsync(device.id) retorna null pra min, porém se eu chamo o IDevice.GetServicesAsync();, retornam vários serviços do mesmo dispositivo, e quando tento escrever me retorna que o dispositivo não suporta escrever.

Services = await device.GetServicesAsync(); > Isso me retorna os serviços
Service = await device.GetServiceAsync(devices.Id); > Isso me retorna null

@stonestecnologia
Copy link

Bom dia, você conseguiu enviar alguma string, em bytes[], para algum dispositivo?

O método IDevice.GetServiceAsync(device.id) retorna null pra min, porém se eu chamo o IDevice.GetServicesAsync();, retornam vários serviços do mesmo dispositivo, e quando tento escrever me retorna que o dispositivo não suporta escrever.

Services = await device.GetServicesAsync(); > Isso me retorna os serviços
Service = await device.GetServiceAsync(devices.Id); > Isso me retorna null

Renan, bom dia.
Voce consegui ?

@felipesans
Copy link

Bom dia, você conseguiu enviar alguma string, em bytes[], para algum dispositivo?
O método IDevice.GetServiceAsync(device.id) retorna null pra min, porém se eu chamo o IDevice.GetServicesAsync();, retornam vários serviços do mesmo dispositivo, e quando tento escrever me retorna que o dispositivo não suporta escrever.
Services = await device.GetServicesAsync(); > Isso me retorna os serviços
Service = await device.GetServiceAsync(devices.Id); > Isso me retorna null

Renan, bom dia.
Voce consegui ?

Boa tarde!

Alguem conseguiu fazer o envio para algum dispositivo.

Até o momento consegui me comunicar com o dispositivo apenas para receber os dados.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment