Example #1
0
        public void StartClient(string networkName, string clientName, Action onStartedAdvertising, Action <string, string, byte[]> onCharacteristicWritten)
        {
            Reset();

            BluetoothLEHardwareInterface.PeripheralName(networkName + ":" + clientName);

            BluetoothLEHardwareInterface.RemoveServices();
            BluetoothLEHardwareInterface.RemoveCharacteristics();

            BluetoothLEHardwareInterface.CBCharacteristicProperties properties =
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite |
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify;

            BluetoothLEHardwareInterface.CBAttributePermissions permissions =
                BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable |
                BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable;

            BluetoothLEHardwareInterface.CreateCharacteristic(SampleCharacteristic.CharacteristicUUID, properties, permissions, null, 5, (characteristic, bytes) => {
                if (onCharacteristicWritten != null)
                {
                    onCharacteristicWritten(clientName, characteristic, bytes);
                }
            });

            BluetoothLEHardwareInterface.CreateService(SampleCharacteristic.ServiceUUID, true, (characteristic) => {
                StatusMessage = "Created service";
            });

            BluetoothLEHardwareInterface.StartAdvertising(() => {
                StatusMessage = "8";
                if (onStartedAdvertising != null)
                {
                    onStartedAdvertising();
                }
            });
        }
Example #2
0
    public void OnButton(Button button)
    {
        if (button.name.Equals("ButtonCentral"))
        {
            _isCentral = true;
            BottomPanel.SetActive(false);
            SetState(States.Scan, 0.5f);
        }
        else if (button.name.Equals("ButtonPeripheral"))
        {
            _isCentral = false;
            BottomPanel.SetActive(false);

            BluetoothLEHardwareInterface.PeripheralName(DeviceName.text);

            BluetoothLEHardwareInterface.RemoveServices();
            BluetoothLEHardwareInterface.RemoveCharacteristics();

            BluetoothLEHardwareInterface.CBCharacteristicProperties properties =
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite |
                BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify;

            BluetoothLEHardwareInterface.CBAttributePermissions permissions =
                BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable |
                BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable;

            BluetoothLEHardwareInterface.CreateCharacteristic(SampleCharacteristic.CharacteristicUUID, properties, permissions, null, 5, (characteristicWritten, bytes) => {
                ValueInputField.text = Encoding.UTF8.GetString(bytes);
            });

            BluetoothLEHardwareInterface.CreateService(SampleCharacteristic.ServiceUUID, true, (characteristic) => {
                StatusMessage = "Created service";
            });

            BluetoothLEHardwareInterface.StartAdvertising(() => {
                MiddlePanel.SetActive(true);
            });
        }
        else if (button.name.Equals("ButtonSend"))
        {
            if (_isCentral)
            {
                BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, SampleCharacteristic.ServiceUUID, SampleCharacteristic.CharacteristicUUID, Encoding.UTF8.GetBytes(ValueInputField.text), ValueInputField.text.Length, true, (characteristicWrite) => {
                });
            }
            else
            {
                BluetoothLEHardwareInterface.UpdateCharacteristicValue(SampleCharacteristic.CharacteristicUUID, Encoding.UTF8.GetBytes(ValueInputField.text), ValueInputField.text.Length);
            }
        }
        else if (button.name.Equals("ButtonStop"))
        {
            if (_isCentral)
            {
                SetState(States.Disconnect, 0.5f);
            }
            else
            {
                BluetoothLEHardwareInterface.StopAdvertising(() => {
                    Reset();
                });
            }
        }
    }