-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBleServer.cs
95 lines (75 loc) · 2.72 KB
/
BleServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using Android.Bluetooth;
using Android.Content.PM;
using Android.Runtime;
using BleRedux.Shared;
using Plugin.BluetoothLE.Server;
using Plugin.BluetoothLE.Server.Internals;
using Xamarin.Essentials;
using Xamarin.Forms;
[assembly: Dependency(typeof(BleRedux.Droid.BleServer))]
namespace BleRedux.Droid
{
[Preserve(AllMembers = true)]
public class BleServer: IBleServer
{
private GattContext _context;
private BluetoothAdapter _adapter;
private GattServer _server;
private Advertiser _advertiser;
public event EventHandler<Plugin.BluetoothLE.AdapterStatus> StatusChanged;
public void Initialise()
{
_context = new GattContext();
_adapter = _context.Manager.Adapter;
//Check if bluetooth is enabled
if (_adapter == null || !_adapter.IsEnabled)
{
Console.WriteLine($"PLEASE ENABLE BLUETOOTH.");
}
var activity = Platform.CurrentActivity;
var hasBle = activity.PackageManager.HasSystemFeature(PackageManager.FeatureBluetoothLe);
if (!hasBle)
{
Console.WriteLine($"DEVICE DOES NOT SUPPORT BLE.");
}
}
public Plugin.BluetoothLE.AdapterStatus GetStatus()
{
return (_adapter.State == State.On) ? Plugin.BluetoothLE.AdapterStatus.PoweredOn : Plugin.BluetoothLE.AdapterStatus.Unknown;
}
//https://developer.android.com/guide/topics/connectivity/bluetooth-le
public IGattService CreateService(Guid uuid, bool primary)
{
if (!MainThread.IsMainThread)
{
Console.WriteLine("CALL CreateService ON THE MAIN THREAD.");
return null;
}
//This can return null in the Ble Plugin
//var gattServer = _context.Manager.OpenGattServer(activity, _context.Callbacks);
//if (gattServer == null)
//{
// Console.WriteLine("A GATT SERVER OBJECT COULD NOT BE CREATED.");
// return null;
//}
if (_server == null) _server = new GattServer();
return new GattService(_context, _server, uuid, primary);
}
public void AddService(IGattService service)
{
_server.AddService(service);
}
public void StartAdvertiser(AdvertisementData advertisingData)
{
if (_advertiser != null) StopAdvertiser();
_advertiser = new Advertiser();
_advertiser.Start(advertisingData);
}
public void StopAdvertiser()
{
if (_advertiser == null) return;
_advertiser.Stop();
}
}
}