Exemplo n.º 1
0
 public override void DisconnectDevice()
 {
     //need to disconnect and deinitialize and then finish it otherwise it won't be disconnected the right way.
     BluetoothLEHardwareInterface.DisconnectAll();
     ConnectDevice(adressFinal);
     BluetoothLEHardwareInterface.DeInitialize(() => { });
     BluetoothLEHardwareInterface.FinishDeInitialize();
     isConnected = false;
 }
Exemplo n.º 2
0
    void InitBluetooth(DeviceName[] _supportedDevices)
    {
        Debug.Log("Bluetooth Initializing...");
        BluetoothDeviceScript receiver = BluetoothLEHardwareInterface.Initialize(true, false, () =>
        {
            Debug.Log("Bluetooth Initialized.");
            Scan(_supportedDevices);
        }, (err) =>
        {
            Debug.Log("Bluetooth Error: " + err);
            if ("Bluetooth LE Not Enabled" == err)
            {
                BluetoothLEHardwareInterface.FinishDeInitialize();
                BluetoothLEHardwareInterface.DeInitialize(() => { StartCoroutine(ReInitBluetooth(_supportedDevices)); });
            }
        });

        DontDestroyOnLoad(receiver.gameObject);
    }
Exemplo n.º 3
0
    private void ConnectDevice(string addr)
    {
        BluetoothLEHardwareInterface.ConnectToPeripheral(addr, (address) =>
        {
        },
                                                         (address, serviceUUID) =>
        {
        },
                                                         (address, serviceUUID, characteristicUUID) =>
        {
            if (IsEqual(serviceUUID, _serviceUUID))
            {
                _connectedID = address;
                isConnected  = true;

                if (IsEqual(characteristicUUID, _readCharacteristicUUID))
                {
                    _readFound = true;
                }
                if (IsEqual(characteristicUUID, _writeCharacteristicUUID))
                {
                    _writeFound = true;
                }
                adressFinal = addr;
                Debug.Log("BLE Spiro Connected");
                BluetoothLEHardwareInterface.StopScan();
            }
        }, (address) =>
        {
            // this will get called when the device disconnects
            // be aware that this will also get called when the disconnect
            // is called above. both methods get call for the same action
            // this is for backwards compatibility
            Debug.Log("Connection Lost");
            isConnected = false;
            //we first need to disconnect and deinitialize before we can connect again to the Air next otherwise we can't reconnect.
            BluetoothLEHardwareInterface.DisconnectAll();
            BluetoothLEHardwareInterface.DeInitialize(() => { });
            BluetoothLEHardwareInterface.FinishDeInitialize();
            DeviceManager.Instance.MakeSpiroControllerNull();
        });
    }
Exemplo n.º 4
0
    public void OnBluetoothMessage(string message)
    {
        if (message != null)
        {
            char[]   delim = new char[] { '~' };
            string[] parts = message.Split(delim);

            for (int i = 0; i < parts.Length; ++i)
            {
                BluetoothLEHardwareInterface.Log(string.Format("Part: {0} - {1}", i, parts[i]));
            }

            if (message.Length >= deviceInitializedString.Length && message.Substring(0, deviceInitializedString.Length) == deviceInitializedString)
            {
                Initialized = true;

                if (InitializedAction != null)
                {
                    InitializedAction();
                }
            }
            else if (message.Length >= deviceDeInitializedString.Length && message.Substring(0, deviceDeInitializedString.Length) == deviceDeInitializedString)
            {
                BluetoothLEHardwareInterface.FinishDeInitialize();
                Initialized = false;

                if (DeinitializedAction != null)
                {
                    DeinitializedAction();
                }
            }
            else if (message.Length >= deviceErrorString.Length && message.Substring(0, deviceErrorString.Length) == deviceErrorString)
            {
                string error = "";

                if (parts.Length >= 2)
                {
                    error = parts[1];
                }

                if (ErrorAction != null)
                {
                    ErrorAction(error);
                }
            }
            else if (message.Length >= deviceServiceAdded.Length && message.Substring(0, deviceServiceAdded.Length) == deviceServiceAdded)
            {
                if (parts.Length >= 2)
                {
                    if (ServiceAddedAction != null)
                    {
                        ServiceAddedAction(parts[1]);
                    }
                }
            }
            else if (message.Length >= deviceStartedAdvertising.Length && message.Substring(0, deviceStartedAdvertising.Length) == deviceStartedAdvertising)
            {
                BluetoothLEHardwareInterface.Log("Started Advertising");

                if (StartedAdvertisingAction != null)
                {
                    StartedAdvertisingAction();
                }
            }
            else if (message.Length >= deviceStoppedAdvertising.Length && message.Substring(0, deviceStoppedAdvertising.Length) == deviceStoppedAdvertising)
            {
                BluetoothLEHardwareInterface.Log("Stopped Advertising");

                if (StoppedAdvertisingAction != null)
                {
                    StoppedAdvertisingAction();
                }
            }
            else if (message.Length >= deviceDiscoveredPeripheral.Length && message.Substring(0, deviceDiscoveredPeripheral.Length) == deviceDiscoveredPeripheral)
            {
                if (parts.Length >= 3)
                {
                    // the first callback will only get called the first time this device is seen
                    // this is because it gets added to the a list in the DiscoveredDeviceList
                    // after that only the second callback will get called and only if there is
                    // advertising data available
                    if (!DiscoveredDeviceList.Contains(parts[1]))
                    {
                        DiscoveredDeviceList.Add(parts[1]);

                        if (DiscoveredPeripheralAction != null)
                        {
                            DiscoveredPeripheralAction(parts[1], parts[2]);
                        }
                    }

                    if (parts.Length >= 5 && DiscoveredPeripheralWithAdvertisingInfoAction != null)
                    {
                        // get the rssi from the 4th value
                        int rssi = 0;
                        if (!int.TryParse(parts[3], out rssi))
                        {
                            rssi = 0;
                        }

                        // parse the base 64 encoded data that is the 5th value
                        byte[] bytes = System.Convert.FromBase64String(parts[4]);

                        DiscoveredPeripheralWithAdvertisingInfoAction(parts[1], parts[2], rssi, bytes);
                    }
                }
            }
            else if (message.Length >= deviceRetrievedConnectedPeripheral.Length && message.Substring(0, deviceRetrievedConnectedPeripheral.Length) == deviceRetrievedConnectedPeripheral)
            {
                if (parts.Length >= 3)
                {
                    DiscoveredDeviceList.Add(parts[1]);

                    if (RetrievedConnectedPeripheralAction != null)
                    {
                        RetrievedConnectedPeripheralAction(parts[1], parts[2]);
                    }
                }
            }
            else if (message.Length >= devicePeripheralReceivedWriteData.Length && message.Substring(0, devicePeripheralReceivedWriteData.Length) == devicePeripheralReceivedWriteData)
            {
                if (parts.Length >= 3)
                {
                    OnPeripheralData(parts[1], parts[2]);
                }
            }
            else if (message.Length >= deviceConnectedPeripheral.Length && message.Substring(0, deviceConnectedPeripheral.Length) == deviceConnectedPeripheral)
            {
                if (parts.Length >= 2 && ConnectedPeripheralAction != null)
                {
                    ConnectedPeripheralAction(parts[1]);
                }
            }
            else if (message.Length >= deviceDisconnectedPeripheral.Length && message.Substring(0, deviceDisconnectedPeripheral.Length) == deviceDisconnectedPeripheral)
            {
                if (parts.Length >= 2)
                {
                    if (ConnectedDisconnectPeripheralAction != null)
                    {
                        ConnectedDisconnectPeripheralAction(parts[1]);
                    }

                    if (DisconnectedPeripheralAction != null)
                    {
                        DisconnectedPeripheralAction(parts[1]);
                    }
                }
            }
            else if (message.Length >= deviceDiscoveredService.Length && message.Substring(0, deviceDiscoveredService.Length) == deviceDiscoveredService)
            {
                if (parts.Length >= 3 && DiscoveredServiceAction != null)
                {
                    DiscoveredServiceAction(parts[1], parts[2]);
                }
            }
            else if (message.Length >= deviceDiscoveredCharacteristic.Length && message.Substring(0, deviceDiscoveredCharacteristic.Length) == deviceDiscoveredCharacteristic)
            {
                if (parts.Length >= 4 && DiscoveredCharacteristicAction != null)
                {
                    DiscoveredCharacteristicAction(parts[1], parts[2], parts[3]);
                }
            }
            else if (message.Length >= deviceDidWriteCharacteristic.Length && message.Substring(0, deviceDidWriteCharacteristic.Length) == deviceDidWriteCharacteristic)
            {
                if (parts.Length >= 2 && DidWriteCharacteristicAction != null)
                {
                    DidWriteCharacteristicAction(parts[1]);
                }
            }
            else if (message.Length >= deviceDidUpdateNotificationStateForCharacteristic.Length && message.Substring(0, deviceDidUpdateNotificationStateForCharacteristic.Length) == deviceDidUpdateNotificationStateForCharacteristic)
            {
                if (parts.Length >= 3)
                {
                    if (DidUpdateNotificationStateForCharacteristicAction != null && DidUpdateNotificationStateForCharacteristicAction.ContainsKey(parts[1]))
                    {
                        var characteristicAction = DidUpdateNotificationStateForCharacteristicAction[parts[1]];
                        if (characteristicAction != null && characteristicAction.ContainsKey(parts[2]))
                        {
                            var action = characteristicAction[parts[2]];
                            if (action != null)
                            {
                                action(parts[2]);
                            }
                        }
                    }

                    if (DidUpdateNotificationStateForCharacteristicWithDeviceAddressAction != null && DidUpdateNotificationStateForCharacteristicWithDeviceAddressAction.ContainsKey(parts[1]))
                    {
                        var characteristicAction = DidUpdateNotificationStateForCharacteristicWithDeviceAddressAction[parts[1]];
                        if (characteristicAction != null && characteristicAction.ContainsKey(parts[2]))
                        {
                            var action = characteristicAction[parts[2]];
                            if (action != null)
                            {
                                action(parts[1], parts[2]);
                            }
                        }
                    }
                }
            }
            else if (message.Length >= deviceDidUpdateValueForCharacteristic.Length && message.Substring(0, deviceDidUpdateValueForCharacteristic.Length) == deviceDidUpdateValueForCharacteristic)
            {
                if (parts.Length >= 4)
                {
                    OnBluetoothData(parts[1], parts[2], parts[3]);
                }
            }
        }
    }