Olá, neste post irei demonstrar como sua aplicação Xamarin.Forms, pode encontrar dispositivos e conectar-se via bluetooth.
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Plugin.BLE” e selecione o plugin como demonstrado na imagem a seguir.
Selecione todos os projetos e clique no botão “Install”.
Xaml
Crie um botão para acionar o Scanner e uma lista onde será exibido todos os dispositivos encontrados. Adicione 2 labels, uma para aparecer o Nome do dispositivo e outra para o Address, como demonstrado a seguir:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
x:Class="DemoBluetooth.MainPage"> | |
<ContentPage.Content> | |
<StackLayout> | |
<Button Text="Search" Clicked="searchDevice"/> | |
<ListView x:Name="DevicesList" | |
IsPullToRefreshEnabled="true" | |
CachingStrategy="RecycleElement" | |
ItemSelected="DevicesList_OnItemSelected"> | |
<ListView.ItemTemplate> | |
<DataTemplate> | |
<ViewCell> | |
<StackLayout> | |
<Label Text="{Binding NativeDevice.Name}"></Label> | |
<Label Text="{Binding NativeDevice.Address}" ></Label> | |
</StackLayout> | |
</ViewCell> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
</ListView> | |
</StackLayout> | |
</ContentPage.Content> | |
</ContentPage> |
C#
Crie as variáveis adapter, bluetoothBLE, list e device e inicialize no método construtor como demonstrado a seguir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Em seguida crie o método “searchDevice”, que irá verificar se o Bluetooth do dispositivo está ativado, se sim irá scannear os dispositivos, a cada dispositivo encontrado irá adicionar na variável list criada anteriormente.
Vale ressaltar que o ScanMode está configurado para “Balanced”, isso significa que este modo possui um desgaste da bateria balanceado.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… | |
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(); | |
} | |
} | |
… |
E por último, crie o método “DevicesList_OnItemSelected” que irá se conectar com o dispositivo que foi selecionado na lista.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… | |
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"); | |
} | |
} | |
… |
MainPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} | |
} |
ANDROID
Edite o manifesto para adicionar algumas permissões, para isso clique com o botão direito no projeto .android e selecione Properties.
No Android Manifest adicione as seguintes permissões.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.DemoBluetooth" android:installLocation="auto"> | |
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28" /> | |
<uses-permission android:name="android.permission.BLUETOOTH" /> | |
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> | |
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" /> | |
<uses-permission android:name="android.permission.LOCATION_HARDWARE" /> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" /> | |
<application android:label="DemoBluetooth.Android"></application> | |
</manifest> |
MainActivity
No arquivo MainActivity.cs, solicite a permissão em tempo de execução.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android; | |
using Android.App; | |
using Android.Content.PM; | |
using Android.OS; | |
namespace DemoBluetooth.Droid | |
{ | |
[Activity(Label = "DemoBluetooth", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] | |
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity | |
{ | |
protected override void OnCreate(Bundle bundle) | |
{ | |
TabLayoutResource = Resource.Layout.Tabbar; | |
ToolbarResource = Resource.Layout.Toolbar; | |
base.OnCreate(bundle); | |
global::Xamarin.Forms.Forms.Init(this, bundle); | |
LoadApplication(new App()); | |
this.RequestPermissions(new[] | |
{ | |
Manifest.Permission.AccessCoarseLocation, | |
Manifest.Permission.BluetoothPrivileged | |
}, 0); | |
} | |
} | |
} | |
IOS
No projeto .iOS edite o arquivo Info.plist e adicione a seguinte permissão dentro de “dict” como demonstrado a seguir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… | |
<key>NSBluetoothPeripheralUsageDescription</key> | |
<string>Esse aplicativo precisa acessar o Bluetooth.</string> | |
</dict> | |
</plist> |
Resultado
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.
Amigo estou com problemas de incompatibilidade no meu projeto xamarin e o plugin BLE.
Criei um projeto Xamarin que nesta versão do visual studio não temos a opção PCL e somente .Net Framework onde quase tudo não tem compatibilidade, gostaria de saber como faço para que esta biblioteca funcione em meu projeto, você tem ideia de como fazer isto ?
Outra dúvida é como enviar para um dispositivo informações, no meu caso quero enviar somente um char ‘L’ ou ‘E’ gostaria de obter também esta informação.
Obrigado por compartilhar 😉
CurtirCurtir
Olá,
No momento existe uma issue no repositório do projeto plugin BLE, se quiser acompanhar essa issue o link é: https://github.com/xabre/xamarin-bluetooth-le/issues/267
Caso você necessite de uma solução imediata, recomendo baixar o projeto no github e utilizar esse método para realizar a conversão.
https://blogs.msdn.microsoft.com/premier_developer/2017/10/27/converting-pcl-portable-class-libraries-libraries-to-net-standard-class-libraries/
Da uma olhada no readme do projeto, lá tem exemplos de como vc pode trocar informações com o dispositivo: https://github.com/xabre/xamarin-bluetooth-le/blob/master/README.md
Espero ter ajudado. 🙂
CurtirCurtir
Amigo, gostaria de saber se há como fazer uma conexão similiar, mas passando apenas um MacAdress de uma forma simples. Sem utilizar o BluetoothLE
CurtirCurtir
Olá Daniel,
Utilizando a conexão via Bluetooth, desconheço outra forma.
Abraço.
CurtirCurtir
Ola Juliano. Gotstaria de parabenizar pelos seus artigos, bastante intuitivo. Possuo uma impressora termica via Bluetooth, mas nao tenho ideia de como devo comecar para imprimir, ja consigo reconhecer e conectar pelo seu codigo. O problema maior e que nao tem encontrei nada sobre impressao na net no xamarin forms. O SDK do fornecedor da impressora e em Android Studio. Haveria possibilidade de converter. Obrigado. Posso enviar para vc para analizar e futuros artigos sobre impressao Bluetooth e Wifi.
CurtirCurtir
Olá,
Obrigado pelo feedback 🙂
Você já verificou se existe algum Nuget Package disponibilizado pelo fornecedor ou pela comunidade para que possa te auxiliar com essa impressora ? Pois, desconheço se existe alguma forma de converter.
CurtirCurtir
Hi, and thank you Juliano for sharing your code
The code compiles and the program loads to the search page without any problems. I have added the Nuget package and made the appropriate edits to the iOS and Android files. Unfortunately I am not finding any of my Bluetooth devices when I click search to perform the scan. I am running Visual Studio Community 2017 on Mac, and using an Iphone emulator. I have also reloaded the code into Visual Studio Community on Windows this time using an android emulator. I have also tried to use LivePlayer with an iphone, but this just crashes as soon as I press search. I have added a “write line” command just after device discovered and this does not write to the console, so I can only assume that I’m not finding the devices.
My Bluetooth devices are all discoverable by my windows computer and my phone using their in built Bluetooth, so I am not sure what to try next. Needless to say I’m very new to this so I’d appreciate any suggestions.
Please can you help?
Thanks
Sarah
CurtirCurtir
Hi Sarah,
I believe that don’t work in Simulators and in Live Player.
Try play in the android real device .
I hope help you.
CurtirCurtir
in my case the name of the device was not display only the address of the device. how can I fix it?
CurtirCurtir
Hi… Verify if the device have a name.
CurtirCurtir
DevicesList donde fue creado?
CurtirCurtir
Foi criada no arquivo Xaml.
CurtirCurtir
nunca entra a
adapter.DeviceDiscovered += (obj, a) =>
{
System.Diagnostics.Debug.WriteLine(“XXXXXXXXXX/ENTRO AL IF 2 /XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”);
if (!list.Contains(a.Device))
{
System.Diagnostics.Debug.WriteLine(“XXXXXXXXXX/ENTRO AL IF 3/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”);
list.Add(a.Device);
};
};
CurtirCurtir