private IEnumerator Start()
    {
        yield return(new WaitForSeconds(3));

        BLEService.Initialize(() =>
        {
            Debug.Log("### Initialized");
            BLEService.StartScan(device =>
            {
                if (device.Name.Equals("UARTService"))
                {
                    BLEService.ConnectToDevice(device,
                                               characteristic =>
                    {
                        Debug.Log(characteristic.CharacteristicUuid);
                        if (characteristic.CharacteristicUuid.Equals(CharacteristicUuid))
                        {
                            BLEService.Subscribe(characteristic, OnDataReceived);
                        }
                    },
                                               disconnectedAddress => { Debug.Log($"### Device Disconnected: {disconnectedAddress}"); });
                }
            });
        },
                              error =>
        {
            Debug.Log("### Intialization error");
        });
        // UARTService
    }
示例#2
0
    private void OnGUI()
    {
        GUI.skin.button.fontSize = 40;
        GUI.skin.label.fontSize  = 40;

        if (!BLEService.IsInitialized)
        {
            if (GUILayout.Button("Initialize"))
            {
                BLEService.Initialize(() => Debug.Log("Initialized"), error => Debug.Log($"Error: {error}"));
            }
        }
        else
        {
            GUILayout.Label($"State: {BLEService.CurrentState}");
            switch (BLEService.CurrentState)
            {
            case BLEService.State.ReadyToScan:
                if (GUILayout.Button("Scan"))
                {
                    BLEService.StartScan(device =>
                    {
                        if (!devices.Contains(device))
                        {
                            devices.Add(device);
                        }
                        Debug.Log($"Device found: {device.Name}");
                    });
                }
                break;

            case BLEService.State.Disconnecting:
                break;

            case BLEService.State.Scanning:
                PrintAvailableDevicesList();
                break;

            case BLEService.State.Connecting:
                break;

            case BLEService.State.Connected:
                foreach (BLEService.Characteristic characteristic in characteristics)
                {
                    if (GUILayout.Button($"{characteristic.Device.Name}: {characteristic.CharacteristicUuid}"))
                    {
                        BLEService.Subscribe(characteristic, OnDataReceived);
                    }
                }

                break;

            case BLEService.State.Subscribing:
                break;

            case BLEService.State.Communicating:
                GUILayout.Label($"Data: {s}");
                break;

            case BLEService.State.Disconnected:
                PrintAvailableDevicesList();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
    }