Exemplo n.º 1
0
 public void StopClient(Action onStoppedAdvertising)
 {
     BluetoothLEHardwareInterface.StopAdvertising(() => {
         if (onStoppedAdvertising != null)
         {
             onStoppedAdvertising();
         }
     });
 }
Exemplo n.º 2
0
 public void OnStartAdvertising()
 {
     if (IsAdvertising)
     {
         BluetoothLEHardwareInterface.StopAdvertising(() => {
             IsAdvertising = false;
         });
     }
     else
     {
         BluetoothLEHardwareInterface.StartAdvertising(() => {
             IsAdvertising = true;
         });
     }
 }
 private void StopScan()
 {
     scaning = false;
     if (isCentral)
     {
         BluetoothLEHardwareInterface.StopScan();
         CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, false);
     }
     else
     {
         StopAllCoroutines();
         BluetoothLEHardwareInterface.StopAdvertising(() =>
         {
             CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, false);
         });
     }
 }
Exemplo n.º 4
0
    public void OnBluetoothFightFinish()
    {
        MyDebug.LogGreen("OnBluetoothFightFinish");
        if (isCentral)
        {
            MyDebug.LogGreen("OnBluetoothFightFinish:Central");
            BluetoothLEHardwareInterface.UnSubscribeCharacteristic(CurPeripheralInstance.address,
                                                                   ServiceUUID,
                                                                   ReadUUID,
                                                                   (characteristic) =>
            {
                MyDebug.LogGreen("UnSubscribeCharacteristic Success :" + characteristic);
            });

            BluetoothLEHardwareInterface.DisconnectPeripheral(CurPeripheralInstance.address,
                                                              (disconnectAddress) =>
            {
                MyDebug.LogGreen("DisconnectPeripheral Success:" + disconnectAddress);
            });
        }
        else
        {
            MyDebug.LogGreen("OnBluetoothFightFinish:Peripheral");
            BluetoothLEHardwareInterface.StopAdvertising(() =>
            {
                MyDebug.LogGreen("Stop Advertising!");
            });
        }

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

        BluetoothLEHardwareInterface.DeInitialize(() =>
        {
            MyDebug.LogGreen("DeInitialize Success!");
        });
        BluetoothLEHardwareInterface.BluetoothEnable(false);
    }
Exemplo n.º 5
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);
        }
    }
Exemplo n.º 6
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();
                });
            }
        }
    }