Skip to content

Instantly share code, notes, and snippets.

@minakhan01
Last active June 2, 2017 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minakhan01/f7c99fd428ecf1aa2a528df11a4e199e to your computer and use it in GitHub Desktop.
Save minakhan01/f7c99fd428ecf1aa2a528df11a4e199e to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Threading.Tasks;
using Windows.Storage.Streams;
#if NETFX_CORE
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Bluetooth.Advertisement;
#endif
public class Watcher : MonoBehaviour
{
#if NETFX_CORE
BluetoothLEAdvertisementWatcher watcher;
public static ushort BEACON_ID = 1775;
#endif
void Awake()
{
#if NETFX_CORE
watcher = new BluetoothLEAdvertisementWatcher();
var manufacturerData = new BluetoothLEManufacturerData
{
CompanyId = BEACON_ID
};
//watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData);
watcher.Received += Watcher_Received;
watcher.Start();
#endif
}
#if NETFX_CORE
private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
if (args != null && args.Advertisement != null)
{
Debug.Log("device found");
if (args.Advertisement.ManufacturerData != null)
{
Debug.Log("manufacture data != null");
var manufacturerData = args.Advertisement.ManufacturerData;
Debug.Log("manufacture data isn't corrupted");
if (manufacturerData.Count > 0)
{
ushort identifier = args.Advertisement.ManufacturerData[0].CompanyId;
// string uuid = args.Advertisement.ServiceUuids[0].ToString();
// byte[] data = args.Advertisement.ManufacturerData[0].Data.ToArray();
Debug.Log("received: " );
}
}
Debug.Log("manufacture data found");
}
var uuid = args.Advertisement.ServiceUuids;
if (uuid.Count > 0)
{
string uuidString = uuid[0].ToString();
Debug.Log("uuid: " + uuidString);
string MyoServiceuuid = "D5060001-A904-DEB9-4748-2C7F4A124842";
string classifierServiceString = "D5060003-A904-DEB9-4748-2C7F4A124842";
if (uuidString.Equals(MyoServiceuuid) ||
uuidString.Equals(MyoServiceuuid.ToLower()))
{
Debug.Log("Myo found!");
ulong bluetoothAddress = args.BluetoothAddress;
//args.
try
{
BluetoothLEDevice bleDevice =
await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
watcher.Stop();
if (bleDevice != null)
{
DevicePairingResult result = await bleDevice.DeviceInformation.Pairing.PairAsync();
bleDevice.ConnectionStatusChanged += BleDevice_ConnectionStatusChanged;
//GattDeviceServicesResult gattServices = await bleDevice.GetGattServicesForUuidAsync(Guid.Parse(classifierServiceString));
//bool connected = false;
//int tryCount = 0;
//while (!connected)
//{
// tryCount++;
// GattDeviceServicesResult gattServices = await bleDevice.GetGattServicesAsync();
//}
Debug.Log("Yayyyyyy! Myo connected");
Debug.Log("Services:" + bleDevice.GattServices.Count);
if (bleDevice.GattServices.Count > 0)
{
foreach (GattDeviceService service in bleDevice.GattServices)
{
Debug.Log("service.Uuid: " + service.Uuid);
var gattCharacteristics = service.GetAllCharacteristics();
foreach (GattCharacteristic characteristic in gattCharacteristics) {
Debug.Log("characteristic.Uuid: " + characteristic.Uuid);
characteristic.GetAllDescriptors();
GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
if (properties.HasFlag(GattCharacteristicProperties.Read))
{
// This characteristic supports reading from it.
GattReadResult result1 = await characteristic.ReadValueAsync();
if (result1.Status == GattCommunicationStatus.Success)
{
var reader = DataReader.FromBuffer(result1.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
Debug.Log(input);
// Utilize the data as needed
}
}
}
}
}
}
}
catch (Exception ex)
{
Debug.Log("Not able to get");
Debug.Log(ex);
}
}
}
}
private void BleDevice_ConnectionStatusChanged(BluetoothLEDevice sender, object args)
{
Debug.Log("BleDevice_ConnectionStatusChanged: " + sender.ConnectionStatus);
}
private void BleDevice_GattServicesChanged(BluetoothLEDevice sender, object args)
{
Debug.Log("Count increased:" +sender.GattServices.Count);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment