(ConnectPrinter);
25 | ConnectDeviceCommand.Execute(peripheral);
26 | }
27 |
28 | void ConnectPrinter(IPeripheral selectedPeripheral)
29 | {
30 | if(!selectedPeripheral.IsConnected())
31 | selectedPeripheral.Connect();
32 |
33 | _perifDisposable = selectedPeripheral.WhenAnyCharacteristicDiscovered().Subscribe((characteristic) =>
34 | {
35 | //System.Diagnostics.Debug.WriteLine(characteristic.Description); //this is not suppported at this momment, and no neccesary I guess
36 | if (characteristic.CanWrite() && !characteristic.CanRead() && !characteristic.CanNotify())
37 | {
38 | IsReadyToPrint = true;
39 | _savedCharacteristic = characteristic;
40 | System.Diagnostics.Debug.WriteLine($"Writing {characteristic.Uuid} - {characteristic.CanRead()} - {characteristic.CanIndicate()} - {characteristic.CanNotify()}");
41 | _perifDisposable.Dispose();
42 | }
43 | });
44 | }
45 |
46 | void PrintText()
47 | {
48 | _savedCharacteristic?.Write(Encoding.UTF8.GetBytes($"{TextToPrint}\n")).Subscribe(
49 | result => {
50 | ShowMessage("Yayy", "Data Printed");
51 | },
52 | exception => {
53 | ShowMessage("Error", "Unable to print");
54 | });
55 | }
56 |
57 | void ShowMessage(string title, string message)
58 | {
59 | Device.BeginInvokeOnMainThread(async () =>
60 | {
61 | await App.Current.MainPage.DisplayAlert(title, message, "Ok");
62 | });
63 | }
64 | public event PropertyChangedEventHandler PropertyChanged;
65 |
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/BluetoothPrintSample/Views/MainPage.xaml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
21 |
22 |
26 |
27 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
44 |
45 |
49 |
50 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/BluetoothPrintSample/Views/MainPage.xaml.cs:
--------------------------------------------------------------------------------
1 | using BluetoothPrintSample.ViewModels;
2 | using Xamarin.Forms;
3 |
4 | namespace BluetoothPrintSample.Views
5 | {
6 | public partial class MainPage : ContentPage
7 | {
8 | public MainPage()
9 | {
10 | InitializeComponent();
11 | BindingContext = new MainPageViewModel();
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/BluetoothPrintSample/Views/PrintPage.xaml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
11 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/BluetoothPrintSample/Views/PrintPage.xaml.cs:
--------------------------------------------------------------------------------
1 | using BluetoothPrintSample.ViewModels;
2 | using Shiny.BluetoothLE;
3 | using Xamarin.Forms;
4 |
5 | namespace BluetoothPrintSample.Views
6 | {
7 | public partial class PrintPage : ContentPage
8 | {
9 | public PrintPage(IPeripheral peripheral)
10 | {
11 | InitializeComponent();
12 | BindingContext = new PrintPageViewModel(peripheral);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 CrossGeeks
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Bluetooth Print Sample
2 |
3 |
4 |
5 |
6 |
7 | Blog post: https://www.xamboy.com/2020/04/20/bluetooth-printing-in-xamarin-forms-using-shiny/
8 |
--------------------------------------------------------------------------------
/Sample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CrossGeeks/BluetoothPrintSample/e8d896ebbdfa2546b648be46371727cef469a139/Sample.gif
--------------------------------------------------------------------------------