-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fragment_Sync_Bluetooth.cs
76 lines (61 loc) · 2.58 KB
/
Fragment_Sync_Bluetooth.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
using Android.OS;
using Android.Views;
using Android.Widget;
using AndroidX.Fragment.App;
using Android.Bluetooth;
using System;
using Java.Util;
using Newtonsoft.Json;
using System.Text;
using Android.Runtime;
using System.Reflection;
namespace ShishaBacon
{
public class Fragment_Sync_Bluetooth : Fragment
{
public EventHandler<BTDeviceArgs> finished;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.layout_sync_bluetooth, container, false);
string possible = BluetoothHelper.isPossible();
if (possible != "")
{
view.FindViewById<TextView>(Resource.Id.sb_text).Text = possible;
return view;
}
view.FindViewById<TextView>(Resource.Id.sb_text).Text = "Dein GeräteName: " + BluetoothHelper.getDeviceName();
ListView list = view.FindViewById<ListView>(Resource.Id.sb_list);
list.Adapter = new ArrayAdapter<string>(Context, Resource.Layout.sb_listitem, BluetoothHelper.getAvaiableDeviceNames());
list.TextFilterEnabled = true;
list.ItemClick += (sender, e) =>
{
BluetoothDevice device = BluetoothHelper.GetDeviceByName(((TextView)e.View).Text);
if (device != null)
{
BluetoothHelper.GetAdapter().CancelDiscovery();
Console.WriteLine(device.GetType().ToString());
device.CreateBond();
Type t = device.GetType();
MethodInfo info = t.GetMethod("CreateRfcommSocket");
object o = info.Invoke(device, new object[] { 1 });
// Name "CreateRfcommSocketToServiceRecord" string
var socket = (BluetoothSocket)o;
//var socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
socket.Connect();
BTDeviceArgs args = new BTDeviceArgs();
args.socket = socket;
finished(this,args);
}
};
return view;
}
}
public class BTDeviceArgs: EventArgs
{
public BluetoothSocket socket { get; set; }
}
}