Sunteți pe pagina 1din 3

using Android.

App;
using Android.Widget;
using Android.OS;
using Plugin.BLE;
using System.Collections.Generic;
using Plugin.BLE.Abstractions.Contracts;
using System.Linq;
using Plugin.BLE.Abstractions.Exceptions;
using System;

namespace qLabs
{
[Activity(Label = "qLabs", MainLauncher = true)]
public class MainActivity : Activity
{
private Guid UUIDCharacteristicRead =new Guid("0000fff4-0000-1000-8000-
00805f9b34fb");
private Guid UUIDCharacteristicWrite = new Guid("0000fff1-0000-1000-8000-
00805f9b34fb");
private Guid UUIDService = new Guid("0000fff0-0000-1000-8000-
00805f9b34fb");

private List<IDevice> deviceList;


private Plugin.BLE.Abstractions.Contracts.IAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource


SetContentView(Resource.Layout.Main);

this.deviceList = new List<IDevice>();


this.adapter = CrossBluetoothLE.Current.Adapter;
this.Start();
}

private async void Start()


{
adapter.DeviceDiscovered += (s, a) =>
{
deviceList.Add(a.Device);
if (a.Device.Name == "qLabsV3J0500061")
Toast.MakeText(this, "Found qLab", ToastLength.Short).Show();
};
await adapter.StartScanningForDevicesAsync();
var qLab = this.deviceList.Where(x => x.Name ==
"qLabsV3J0500061").First();
try
{
await adapter.ConnectToDeviceAsync(qLab);
Toast.MakeText(this, "Connected to qLab",
ToastLength.Short).Show();

string cmd = "hello client";


byte[] payload = System.Text.Encoding.ASCII.GetBytes(cmd);
string hexCmd = BitConverter.ToString(payload);
hexCmd = hexCmd.Replace("-", "");
hexCmd = string.Format("02{0}03", hexCmd);
payload = HexStringToByteArray("0268656c6c6f20636c69656e74034dee");
var service = await qLab.GetServiceAsync(this.UUIDService);
var characteristicRead = await
service.GetCharacteristicAsync(this.UUIDCharacteristicRead);
var characteristicWrite = await
service.GetCharacteristicAsync(this.UUIDCharacteristicWrite);
characteristicRead.ValueUpdated += CharacteristicRead_ValueUpdated;
await characteristicRead.StartUpdatesAsync();
await characteristicWrite.WriteAsync(payload.ToArray());
}
catch (Exception ex)
{
// ... could not connect to device
}
}

public byte[] FromHex(string hex)


{
hex = hex.Replace("-", "");
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return raw;
}

public static byte[] HexStringToByteArray(string hex)


{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}

public static string ByteArrayToString(byte[] ba)


{
return BitConverter.ToString(ba).Replace("-", "");
}

private void CharacteristicRead_ValueUpdated(object sender,


Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e)
{
var b = ByteArrayToString(e.Characteristic.Value);
var a = System.Text.Encoding.Default.GetString(FromHex(b));
}

public static class Crc16


{
const ushort polynomial = 0xA001;
static readonly ushort[] table = new ushort[256];

public static ushort ComputeChecksum(byte[] bytes)


{
ushort crc = 0;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(crc ^ bytes[i]);
crc = (ushort)((crc >> 8) ^ table[index]);
}
return crc;
}

static Crc16()
{
ushort value;
ushort temp;
for (ushort i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
{
value = (ushort)((value >> 1) ^ polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
}
}
}

S-ar putea să vă placă și