-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncClient.cs
146 lines (122 loc) · 4.22 KB
/
SyncClient.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Android;
using Android.Bluetooth;
using Android.Content.PM;
using Android.Runtime;
using AndroidX.AppCompat.App;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using Java.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShishaBacon
{
public class SyncClient
{
public static void startSyncing()
{
}
}
class BluetoothHelper
{
private static bool hasPermission = false;
public static BluetoothAdapter GetAdapter()
{
BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
if (adapter == null)
throw new Exception("No Bluetooth adapter found.");
if (!adapter.IsEnabled)
throw new Exception("Bluetooth adapter is not enabled.");
if (!hasPermission)
throw new Exception("Bluetooth Permission not granted.");
return adapter;
}
public static void getPermission(Android.Content.Context content,Android.App.Activity activity)
{
if (ContextCompat.CheckSelfPermission(content, Manifest.Permission.Bluetooth) != (int)Permission.Granted)
{
ActivityCompat.RequestPermissions(activity, new string[] { Manifest.Permission.Bluetooth }, 1);
}
hasPermission = ContextCompat.CheckSelfPermission(content, Manifest.Permission.Bluetooth) == (int)Permission.Granted;
}
public static string isPossible()
{
try
{
GetAdapter();
return "";
} catch (Exception e)
{
return e.Message;
}
}
public static string getDeviceName()
{
BluetoothAdapter adapter = GetAdapter();
return adapter.Name;
}
public static List<BluetoothDevice> getAvaiableDevices()
{
BluetoothAdapter adapter = GetAdapter();
return adapter.BondedDevices.ToList();
}
public static List<string> getAvaiableDeviceNames()
{
List<string> list = new List<string>();
BluetoothAdapter adapter = GetAdapter();
foreach (var device in adapter.BondedDevices)
{
list.Add(device.Name);
}
return list;
}
public static BluetoothDevice GetDeviceByName(string name)
{
BluetoothAdapter adapter = GetAdapter();
foreach (var device in adapter.BondedDevices)
{
if (device.Name == name)
{
return device;
}
}
return null;
}
public static async void SendTo(BluetoothSocket socket)
{
var list = TabaccoList.GetList();
string listString = JsonConvert.SerializeObject(list, Formatting.Indented);
byte[] array = Encoding.ASCII.GetBytes(listString);
await socket.OutputStream.WriteAsync(array, 0, array.Length);
}
public static async void RecieveFrom(BluetoothSocket socket)
{
string res = await Task.Factory.StartNew(() => {
StringBuilder builder = new StringBuilder();
byte[] buffer = new byte[1024];
int bytes;
while (true)
{
try
{
bytes = socket.InputStream.Read(buffer, 0, buffer.Length);
if (bytes > 0)
{
builder.Append(Encoding.ASCII.GetString(buffer));
}
}
catch (Java.IO.IOException)
{
break;
}
}
Console.WriteLine(builder.ToString());
return builder.ToString();
});
Console.WriteLine(res);
}
}
}