Skip to content

Commit

Permalink
implement connected
Browse files Browse the repository at this point in the history
  • Loading branch information
juucustodio committed Dec 7, 2017
1 parent 7762ef8 commit c4ae67d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
2 changes: 2 additions & 0 deletions DemoBluetooth/DemoBluetooth.iOS/Info.plist
Expand Up @@ -48,5 +48,7 @@
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Esse aplicativo precisa acessar o Bluetooth.</string>
</dict>
</plist>
9 changes: 2 additions & 7 deletions DemoBluetooth/DemoBluetooth/App.xaml.cs
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;
using Xamarin.Forms;

namespace DemoBluetooth
{
Expand All @@ -13,7 +8,7 @@ public App()
{
InitializeComponent();

MainPage = new DemoBluetooth.MainPage();
MainPage = new MainPage();
}

protected override void OnStart()
Expand Down
8 changes: 3 additions & 5 deletions DemoBluetooth/DemoBluetooth/MainPage.xaml
@@ -1,27 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DemoBluetooth"
x:Class="DemoBluetooth.MainPage">
<ContentPage.Content>
<StackLayout>
<Button Text="Search" Clicked="searchDevice"/>
<ListView x:Name="DevicesList"
IsPullToRefreshEnabled="true"
CachingStrategy="RecycleElement">
CachingStrategy="RecycleElement"
ItemSelected="DevicesList_OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding NativeDevice.Name}" ></Label>
<Label Text="{Binding NativeDevice.Name}"></Label>
<Label Text="{Binding NativeDevice.Address}" ></Label>
<Label Text="{Binding NativeDevice.BondState}" ></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</StackLayout>
</ContentPage.Content>
</ContentPage>
39 changes: 32 additions & 7 deletions DemoBluetooth/DemoBluetooth/MainPage.xaml.cs
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Plugin.BLE;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
using Xamarin.Forms;

namespace DemoBluetooth
Expand All @@ -15,7 +13,8 @@ public partial class MainPage : ContentPage

IAdapter adapter;
IBluetoothLE bluetoothBLE;
public ObservableCollection<IDevice> list = new ObservableCollection<IDevice>();
ObservableCollection<IDevice> list = new ObservableCollection<IDevice>();

private IDevice device;
public MainPage()
{
Expand All @@ -25,7 +24,6 @@ public MainPage()
adapter = CrossBluetoothLE.Current.Adapter;

list = new ObservableCollection<IDevice>();

DevicesList.ItemsSource = list;

}
Expand All @@ -41,9 +39,11 @@ private async void searchDevice(object sender, EventArgs e)
else
{
list.Clear();


adapter.ScanTimeout = 10000;
adapter.ScanMode = ScanMode.Balanced;


adapter.DeviceDiscovered += (obj, a) =>
{
if (!list.Contains(a.Device))
Expand All @@ -57,5 +57,30 @@ private async void searchDevice(object sender, EventArgs e)
}


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");
}

}
}
}
6 changes: 2 additions & 4 deletions DemoBluetooth/DemoBluetooth/Properties/AssemblyInfo.cs
@@ -1,7 +1,5 @@
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Resources;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand Down

0 comments on commit c4ae67d

Please sign in to comment.