Esempio n. 1
0
    public void Initialize()
    {
        ButtonStartAdvertising.SetActive(false);
        Button1Highlight.SetActive(false);
        LEDHighlight.SetActive(false);
        Button1.SetActive(false);

        BluetoothLEHardwareInterface.Initialize(false, true, () => {
            BluetoothLEHardwareInterface.PeripheralName("Simulated RFduino");
            BluetoothLEHardwareInterface.CreateCharacteristic("2221", BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable, null, 0, null);

            BluetoothLEHardwareInterface.CreateCharacteristic("2222", BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable, null, 0,
                                                              (characteristicUUID, bytes) => {
                if (bytes.Length > 0)
                {
                    if (bytes[0] == 0x01)
                    {
                        LEDHighlight.SetActive(true);
                    }
                    else
                    {
                        LEDHighlight.SetActive(false);
                    }
                }
            });

            BluetoothLEHardwareInterface.CreateService("2220", true, (serviceUUID) => {
                ButtonStartAdvertising.SetActive(true);
            });
        }, (error) => {
        });
    }
Esempio n. 2
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();
                }
            });
        }
    private void StartScan()
    {
        if (scaning)
        {
            MyDebug.LogYellow("Scaning!!!");
            return;
        }
        scaning = true;

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

        CategoryInstance curCategoryInstance = new CategoryInstance(curPatternID, curAmountID, curSymbolID, curDigitID, curOperandID);

        FightController.Instance.CurCategoryInstance = curCategoryInstance;

        string serviceUUID = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "0";
        string readUUID    = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "1";
        string writeUUID   = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "2";

        BluetoothController.Instance.ServiceUUID = BluetoothLEHardwareInterface.FullUUID(serviceUUID);
        BluetoothController.Instance.ReadUUID    = BluetoothLEHardwareInterface.FullUUID(readUUID);
        BluetoothController.Instance.WriteUUID   = BluetoothLEHardwareInterface.FullUUID(writeUUID);

        MyDebug.LogGreen("ServiceUUID:" + BluetoothController.Instance.ServiceUUID);
        MyDebug.LogGreen("ReadUUID:" + BluetoothController.Instance.ReadUUID);
        MyDebug.LogGreen("WriteUUID:" + BluetoothController.Instance.WriteUUID);

        if (isCentral)
        {
            MyDebug.LogGreen("Central Start Scaning!");
            bluetoothScanResultContent.SetActive(true);
            RefreshScanResultContent();
            CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, true);
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(new string[] { BluetoothController.Instance.ServiceUUID },
                                                                        (address, name) =>
            {
                AddPeripheral(address, name);
            });
        }
        else
        {
            MyDebug.LogGreen("Peripheral Start Scaning!");
            BluetoothLEHardwareInterface.PeripheralName(GameManager.Instance.UserName);
            MyDebug.LogGreen("PeripheralName:" + GameManager.Instance.UserName);

            BluetoothLEHardwareInterface.CreateCharacteristic(BluetoothController.Instance.ReadUUID,
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable, null, 0, null);
            MyDebug.LogGreen("CreateCharacteristic:Read!");

            BluetoothLEHardwareInterface.CreateCharacteristic(BluetoothController.Instance.WriteUUID,
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable, null, 0,
                                                              BluetoothController.Instance.PeripheralReceiveMessage);
            MyDebug.LogGreen("CreateCharacteristic:Write!");

            BluetoothLEHardwareInterface.CreateService(BluetoothController.Instance.ServiceUUID, true, (message) =>
            {
                MyDebug.LogGreen("Create Service Success:" + message);
            });
            MyDebug.LogGreen("CreateService!");

            BluetoothLEHardwareInterface.StartAdvertising(() =>
            {
                MyDebug.LogGreen("Start Advertising!");
                bluetoothScanResultContent.SetActive(true);
                RefreshScanResultContent();
                StartCoroutine(AdvertisingCountDown());
                CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, true);
            });
        }
    }
Esempio n. 4
0
    void OnGUI()
    {
        GUI.skin = skin;

        if (GUI.Button(new Rect(10, 0, 600, 100), "DeInitialize"))
        {
            BluetoothLEHardwareInterface.DeInitialize(null);
        }

        if (GUI.Button(new Rect(10, 100, 300, 50), "Initialize Central"))
        {
            bluetoothDeviceScript = BluetoothLEHardwareInterface.Initialize(true, false, null, null);
        }

        if (GUI.Button(new Rect(10, 150, 300, 50), "Scan for 1851"))
        {
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(new string[] { serviceUUID }, null);
        }

        if (GUI.Button(new Rect(10, 200, 300, 50), "Scan for Any"))
        {
            BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null);
        }

        if (GUI.Button(new Rect(10, 250, 300, 50), "Retrieve Connected"))
        {
            BluetoothLEHardwareInterface.RetrieveListOfPeripheralsWithServices(new string[] { serviceUUID }, null);
        }

        if (GUI.Button(new Rect(10, 300, 300, 50), "Stop Scan"))
        {
            BluetoothLEHardwareInterface.StopScan();
        }

        if (GUI.Button(new Rect(10, 350, 300, 50), "Connect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.ConnectToPeripheral(bluetoothDeviceScript.DiscoveredDeviceList[0], null, null, null);
        }

        if (GUI.Button(new Rect(10, 400, 300, 50), "Disconnect") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.DisconnectPeripheral(bluetoothDeviceScript.DiscoveredDeviceList[0], null);
        }

        if (GUI.Button(new Rect(10, 450, 300, 50), "Read Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.ReadCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
        }

        if (GUI.Button(new Rect(10, 500, 300, 50), "Write Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            if (data == null)
            {
                data = new byte[64];
                for (int i = 0; i < 64; ++i)
                {
                    data[i] = (byte)i;
                }
            }

            BluetoothLEHardwareInterface.WriteCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, data, data.Length, true, null);
        }

        if (GUI.Button(new Rect(10, 550, 300, 50), "Subscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.SubscribeCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null, null);
        }

        if (GUI.Button(new Rect(10, 600, 300, 50), "UnSubscribe Characteristic") && bluetoothDeviceScript != null && bluetoothDeviceScript.DiscoveredDeviceList != null && bluetoothDeviceScript.DiscoveredDeviceList.Count > 0)
        {
            BluetoothLEHardwareInterface.UnSubscribeCharacteristic(bluetoothDeviceScript.DiscoveredDeviceList[0], serviceUUID, characteristicUUID, null);
        }

        if (GUI.Button(new Rect(310, 100, 300, 100), "Initialize Peripheral"))
        {
            BluetoothLEHardwareInterface.Initialize(false, true, null, null);
        }

        if (GUI.Button(new Rect(310, 200, 300, 100), "Create Service\nand Characteristic"))
        {
            BluetoothLEHardwareInterface.PeripheralName("Test Device");

            if (data == null)
            {
                data = new byte[64];
                for (int i = 0; i < 64; ++i)
                {
                    data[i] = (byte)i;
                }
            }

            BluetoothLEHardwareInterface.CreateCharacteristic(characteristicUUID,
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite |
                                                              BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify,
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable |
                                                              BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable,
                                                              null, 0, null);

            BluetoothLEHardwareInterface.CreateService(serviceUUID, true, null);
        }

        if (GUI.Button(new Rect(310, 300, 300, 100), "Start Advertising"))
        {
            BluetoothLEHardwareInterface.StartAdvertising(null);
        }

        if (GUI.Button(new Rect(310, 400, 300, 100), "Stop Advertising"))
        {
            BluetoothLEHardwareInterface.StopAdvertising(null);
        }

        if (GUI.Button(new Rect(310, 500, 300, 100), "Update Characteristic Value"))
        {
            for (int i = 0; i < data.Length; ++i)
            {
                data[i] = (byte)(data[i] + 1);
            }

            BluetoothLEHardwareInterface.UpdateCharacteristicValue(characteristicUUID, data, data.Length);
        }
    }
Esempio n. 5
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();
                });
            }
        }
    }