Révision 4c7eec22

Voir les différences:

TestXamConnections/TestXamConnections.Android/Helper/ConnectionServiceProvider.cs
19 19
{
20 20
    public class ConnectionServiceProvider : IConnectionServiceProvider
21 21
    {
22
        public IConnectionService GetConnectionInstance(ConnectionType ct)
22
        public IConnectionService GetConnectionServiceInstance(ConnectionType ct)
23 23
        {
24 24
            IConnectionService instance = null;
25 25
            switch (ct)
TestXamConnections/TestXamConnections/Helper/IConnectionServiceProvider.cs
18 18

  
19 19
    public interface IConnectionServiceProvider
20 20
    {
21
        IConnectionService GetConnectionInstance(ConnectionType ct);
21
        IConnectionService GetConnectionServiceInstance(ConnectionType ct);
22 22
    }
23 23
}
TestXamConnections/TestXamConnections/Models/AgridentReader.cs
43 43

  
44 44
        public AgridentReader()
45 45
        {
46
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionInstance(ConnectionType.Intern);
46
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionServiceInstance(ConnectionType.Intern);
47 47
            ConnectionService.DataReceivedEvent += InternDataReceived;
48 48
        }
49 49

  
TestXamConnections/TestXamConnections/Models/BluetoothReader.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Threading.Tasks;
5
using TestXamConnections.Connection;
6
using TestXamConnections.Helper;
7
using Xamarin.Forms;
8

  
9
namespace TestXamConnections.Models
10
{
11
    public class BluetoothReader : Device
12
    {
13
        private IConnectionService ConnectionService { get; set; }
14
        public EventHandler<byte[]> BluetoothDataReceivedEvent { get; set; }
15

  
16
        public BluetoothReader(Device device)
17
        {
18
            Name = device.Name;
19
            MACAddress = device.MACAddress;
20
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionServiceInstance(ConnectionType.Bluetooth);
21
            ConnectionService.DataReceivedEvent += BluetoothDataReceived;
22
        }
23

  
24
        private void BluetoothDataReceived(object sender, byte[] e)
25
        {
26
            BluetoothDataReceivedEvent.Invoke(this, e);
27
        }
28

  
29
        public async Task<bool> ConnectToBTReaderAsync()
30
        {
31
            Dictionary<string, string> param = new Dictionary<string, string>()
32
            {
33
                { ConnectionConstants.MAC_ADDR_KEY, MACAddress }
34
            };
35
            bool ret = await ConnectionService.Connect(param);
36
            return ret;
37
        }
38
    }
39
}
TestXamConnections/TestXamConnections/Models/TeoBalance.cs
48 48
        {
49 49
            Name = device.Name;
50 50
            MACAddress = device.MACAddress;
51
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionInstance(ConnectionType.Bluetooth);
51
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionServiceInstance(ConnectionType.Bluetooth);
52 52
            ConnectionService.DataReceivedEvent += TeoDataReceived;
53 53
        }
54 54

  
......
121 121
            {
122 122
                { ConnectionConstants.MAC_ADDR_KEY, MACAddress }
123 123
            };
124
            await ConnectionService.Connect(param);
124
            _ = await ConnectionService.Connect(param);
125 125
            return true;
126 126
        }
127 127

  
TestXamConnections/TestXamConnections/TestXamConnections.csproj
22 22
    <Compile Update="Views\AccueilView.xaml.cs">
23 23
      <DependentUpon>AccueilView.xaml</DependentUpon>
24 24
    </Compile>
25
    <Compile Update="Views\BluetoothReaderView.xaml.cs">
26
      <DependentUpon>BluetoothReaderView.xaml</DependentUpon>
27
    </Compile>
25 28
    <Compile Update="Views\TeoDeviceView.xaml.cs">
26 29
      <DependentUpon>TeoDeviceView.xaml</DependentUpon>
27 30
    </Compile>
......
31 34
    <EmbeddedResource Update="Views\AccueilView.xaml">
32 35
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
33 36
    </EmbeddedResource>
37
    <EmbeddedResource Update="Views\BluetoothReaderView.xaml">
38
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
39
    </EmbeddedResource>
34 40
    <EmbeddedResource Update="Views\InternServiceView.xaml">
35 41
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
36 42
    </EmbeddedResource>
TestXamConnections/TestXamConnections/ViewModels/AccueilViewModel.cs
67 67

  
68 68
        public override async void OnActivated()
69 69
        {
70
            await CheckAndRequestLocationPermission();
70
            _ = await CheckAndRequestLocationPermission();
71 71
        }
72 72

  
73 73
        public async Task NavigateToInternService()
TestXamConnections/TestXamConnections/ViewModels/BluetoothReaderViewModel.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Threading.Tasks;
5
using System.Windows.Input;
6
using TestXamConnections.Models;
7
using Xamarin.Forms;
8

  
9
namespace TestXamConnections.ViewModels
10
{
11
    public class BluetoothReaderViewModel : ViewModelBase
12
    {
13
        private BluetoothReader btReader;
14
        private bool isVisibleData;
15
        private string data;
16
        private string connectionIndicator;
17
        private string ciColor;
18

  
19
        public string CIColor
20
        {
21
            get => ciColor;
22
            set => SetProperty(ref ciColor, value);
23
        }
24

  
25
        public string ConnectionIndicator
26
        {
27
            get => connectionIndicator;
28
            set => SetProperty(ref connectionIndicator, value);
29
        }
30

  
31
        public string Data
32
        {
33
            get => data;
34
            set => SetProperty(ref data, value);
35
        }
36

  
37
        public bool IsVisibleData
38
        {
39
            get => isVisibleData;
40
            set => SetProperty(ref isVisibleData, value);
41
        }
42

  
43
        public BluetoothReader BTReader
44
        {
45
            get => btReader;
46
            set => SetProperty(ref btReader, value);
47
        }
48

  
49
        public async Task<bool> ConnectToDevice()
50
        {
51
            if (await BTReader.ConnectToBTReaderAsync())
52
            {
53
                ConnectionIndicator = "Connecté";
54
                CIColor = "Green";
55
                return true;
56
            }
57
            else
58
            {
59
                ConnectionIndicator = "Non Connecté";
60
                CIColor = "Red";
61
                return false;
62
            }
63
        }
64

  
65
        public ICommand ConnectToDeviceCommand { get; private set; }
66

  
67
        public BluetoothReaderViewModel(Models.Device device)
68
        {
69
            CIColor = "Red";
70
            ConnectionIndicator = "Non connecté";
71
            IsVisibleData = false;
72
            BTReader = new BluetoothReader(device);
73
            ConnectToDeviceCommand = new Command(async () => await ConnectToDevice());
74
            BTReader.BluetoothDataReceivedEvent += DataReceived;
75
        }
76

  
77
        private void DataReceived(object sender, byte[] e)
78
        {
79
            IsVisibleData = true;
80
            Data += Encoding.ASCII.GetString(e) + "\n";
81
        }
82
    }
83
}
TestXamConnections/TestXamConnections/ViewModels/DeviceItemViewModel.cs
29 29
            {
30 30
                await Application.Current.MainPage.Navigation.PushAsync(new TeoDeviceView(Device));
31 31
            }
32
            // Si c'est le scanner HoneyWell
33
            else if (Device.MACAddress == "00:10:20:30:92:7D")
34
            {
35
                await Application.Current.MainPage.Navigation.PushAsync(new BluetoothReaderView(Device));
36
            }
32 37
        }
33 38

  
34 39
        public ICommand NavigateToDeviceViewCommand { private set; get; }
TestXamConnections/TestXamConnections/Views/BluetoothReaderView.xaml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<ContentPage
3
    x:Class="TestXamConnections.Views.BluetoothReaderView"
4
    xmlns="http://xamarin.com/schemas/2014/forms"
5
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6
    xmlns:xe="clr-namespace:XamEffects;assembly=XamEffects">
7
    <ContentPage.Content>
8
        <StackLayout
9
            Padding="10"
10
            BackgroundColor="#001729"
11
            VerticalOptions="FillAndExpand">
12
            <StackLayout VerticalOptions="CenterAndExpand">
13

  
14
                <Frame
15
                    Padding="0"
16
                    BackgroundColor="Transparent"
17
                    BorderColor="#4f5a64"
18
                    CornerRadius="5"
19
                    HasShadow="False"
20
                    HorizontalOptions="Center"
21
                    VerticalOptions="CenterAndExpand"
22
                    WidthRequest="250">
23
                    <StackLayout Spacing="0">
24
                        <StackLayout Margin="0,10">
25
                            <Label
26
                                Margin="0"
27
                                FontAttributes="Bold"
28
                                FontSize="28"
29
                                HorizontalOptions="CenterAndExpand"
30
                                HorizontalTextAlignment="Center"
31
                                Text="{Binding BTReader.Name}"
32
                                TextColor="White"
33
                                VerticalOptions="CenterAndExpand" />
34
                            <Label
35
                                Margin="0"
36
                                FontAttributes="Italic"
37
                                FontSize="12"
38
                                HorizontalOptions="CenterAndExpand"
39
                                HorizontalTextAlignment="Center"
40
                                Text="{Binding BTReader.MACAddress}"
41
                                TextColor="DarkGray"
42
                                VerticalOptions="CenterAndExpand" />
43
                        </StackLayout>
44
                        <!--  Bouton Connect  -->
45
                        <xe:BorderView
46
                            Margin="0,10,0,20"
47
                            xe:Commands.Tap="{Binding ConnectToDeviceCommand}"
48
                            xe:TouchEffect.Color="#0060a8"
49
                            BackgroundColor="#003266"
50
                            CornerRadius="65"
51
                            HorizontalOptions="Center">
52
                            <StackLayout Padding="15,10">
53
                                <Label
54
                                    FontSize="16"
55
                                    HorizontalOptions="Center"
56
                                    Text="Connect"
57
                                    TextColor="#34a9ff" />
58
                            </StackLayout>
59
                        </xe:BorderView>
60
                        <Label
61
                            Margin="0,0,0,20"
62
                            FontAttributes="Bold"
63
                            FontSize="Medium"
64
                            HorizontalOptions="CenterAndExpand"
65
                            Text="{Binding ConnectionIndicator}"
66
                            TextColor="{Binding CIColor}"
67
                            VerticalOptions="CenterAndExpand" />
68
                    </StackLayout>
69
                </Frame>
70

  
71
                <Frame
72
                    Padding="0"
73
                    BackgroundColor="Transparent"
74
                    BorderColor="#4f5a64"
75
                    CornerRadius="5"
76
                    HasShadow="False"
77
                    HorizontalOptions="Center"
78
                    IsVisible="{Binding IsVisibleData}"
79
                    VerticalOptions="CenterAndExpand"
80
                    WidthRequest="200">
81
                    <StackLayout HorizontalOptions="CenterAndExpand">
82
                        <Label
83
                            FontAttributes="Bold"
84
                            FontSize="18"
85
                            HorizontalTextAlignment="Center"
86
                            Text="Received Data"
87
                            TextColor="White" />
88
                        <Label
89
                            FontAttributes="Italic"
90
                            FontSize="14"
91
                            HorizontalTextAlignment="Center"
92
                            Text="{Binding Data}"
93
                            TextColor="DarkGray" />
94
                    </StackLayout>
95
                </Frame>
96
            </StackLayout>
97
        </StackLayout>
98
    </ContentPage.Content>
99
</ContentPage>
TestXamConnections/TestXamConnections/Views/BluetoothReaderView.xaml.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using TestXamConnections.ViewModels;
7
using Xamarin.Forms;
8
using Xamarin.Forms.Xaml;
9

  
10
namespace TestXamConnections.Views
11
{
12
    [XamlCompilation(XamlCompilationOptions.Compile)]
13
    public partial class BluetoothReaderView : ContentPage
14
    {
15
        public BluetoothReaderView(Models.Device device)
16
        {
17
            InitializeComponent();
18
            BindingContext = new BluetoothReaderViewModel(device);
19
        }
20
    }
21
}

Formats disponibles : Unified diff