Exemple #1
0
        public async Task <bool> BLE_Connect(IBlePeripheral device)
        {
            // Get BLE device
            if ((null != g_BLEAdapterObj) && (null != device))
            {
                // Try connect to ble device
                BlePeripheralConnectionRequest connection = await g_BLEAdapterObj.ConnectToDevice(device, TimeSpan.FromSeconds(15));

                if (connection.IsSuccessful())
                {
                    // Invoke connection state callback
                    OnBLEConnection_StateChange(ConnectionState.Connected);

                    if (true == await SetupBLEConnection(connection))
                    {
                        return(true);
                    }
                    else
                    {
                        await connection.GattServer.Disconnect();
                    }
                }
            }

            return(false);
        }
        public async Task <bool> BLE_ScanAndConnect(string deviceName)
        {
            // Get BLE device
            if (null != deviceName)
            {
                // Try connect to ble device
                ScanFilter filter = new ScanFilter().SetAdvertisedDeviceName(deviceName);

                BlePeripheralConnectionRequest connection = await g_BLEAdapterObj.FindAndConnectToDevice(filter, TimeSpan.FromSeconds(15));

                if (connection.IsSuccessful())
                {
                    if (true == await SetupBLEConnection(connection))
                    {
                        return(true);
                    }
                    else
                    {
                        await connection.GattServer.Disconnect();
                    }
                }
            }

            return(false);
        }