示例#1
0
    /// <summary>
    /// Bluetooth device picker를 열고 시리얼 포트의 서비스에서 스트림을 여는 것을 시도합니다.
    /// </summary>
    private static Stream OpenBluetoothStream(DeviceInformation deviceInformation, RfcommServiceId serviceId)
    {
        //서비스 받아오기
        var device   = BluetoothDevice.FromDeviceInformation(deviceInformation);
        var result   = device.GetRfcommServices(BluetoothCacheMode.Cached);
        var services = result.Services;

        //print(serviceId);

        //요청한 서비스를 찾고 접속합니다.
        for (int i = 0; i < services.Count; ++i)
        {
            print(services[0].ServiceId.Uuid);
            print(services[1].ServiceId.Uuid);
            print(services.Count);

            var current = services[i];
            if (current.ServiceId == serviceId)
            {
                return(current.OpenStream());
            }
        }

        return(null);
    }
示例#2
0
        public void Init()
        {
            _config    = LabTools.GetConfig <BlueToothDeviceConfig>();
            DeviceType = DeviceType.BlueTooth;

            switch (_config.BluetoothGetType)
            {
            case BluetoothGetType.Pick:
                var deviceInformation = PickDevice();
                if (deviceInformation == null)
                {
                    throw new InvalidDataException("Fail to retrieve device information - is the device turned on? (if so, try pairing it in Windows and try again)");
                }
                _bluetoothDevice = BluetoothDevice.FromDeviceInformation(deviceInformation);

                break;

            case BluetoothGetType.Read:
                _bluetoothDevice = BluetoothDevice.FromBluetoothAddress(ulong.Parse(_config.BluetoothAddress, System.Globalization.NumberStyles.HexNumber));

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        /// <summary>
        /// opens Bluetooth device picker and tries to open stream on it's SerialPort service,
        /// </summary>
        /// <returns>SerialPort stream, or null if it cannot connect</returns>
        private static Stream OpenBluetoothStream(DeviceInformation deviceInformation, RfcommServiceId serviceId)
        {
            /* get services from selected device */
            var device   = BluetoothDevice.FromDeviceInformation(deviceInformation);
            var result   = device.GetRfcommServices(BluetoothCacheMode.Cached);
            var services = result.Services;

            /* find requested service and open connection*/
            for (int i = 0; i < services.Count; ++i)
            {
                var current = services[i];
                if (current.ServiceId == serviceId)
                {
                    return(current.OpenStream());
                }
            }

            /* if the reqired service does not exist */
            return(null);
        }
示例#4
0
 public bool Connect(DeviceInformation deviceInfo)
 {
     device = BluetoothDevice.FromDeviceInformation(deviceInfo);
     return(Connect(device));
 }