Exemplo n.º 1
0
		public Device(BTDevice device)
		{
			if (device == null)
				throw new ArgumentNullException("device");
#if NETFX_CORE
			if (device.ServiceId.Uuid != RfcommServiceId.SerialPort.Uuid)
				throw new NotSupportedException("Only SerialPort devices supported");
#endif
			m_device = device;
		}
        public async Task <string> Connect()
        {
            var rfcommServices = await bluetoothDeviced.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(RfcommServiceId.SerialPort.Uuid), BluetoothCacheMode.Uncached);

            if (rfcommServices.Services.Count > 0)
            {
                try
                {
                    var service = rfcommServices.Services[0];
                    //  await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
                    _service = service;

                    // Create a socket and connect to the target
                    _socket = new StreamSocket();
                    await _socket.ConnectAsync(
                        _service.ConnectionHostName,
                        _service.ConnectionServiceName,
                        SocketProtectionLevel
                        .BluetoothEncryptionAllowNullAuthentication);

                    sost        = true;
                    inputStream = _socket.InputStream.AsStreamForRead();

                    streamReader = new StreamReader(inputStream);

                    return("Connect");
                }
                catch (Exception ex)
                {
                    sost = false;
                    return(ex.Message);
                }
            }
            sost = false;
            return("0");
        }
Exemplo n.º 3
0
        async private void ConnectDevice_Click(object sender, RoutedEventArgs e)
        {
            //Revision: No need to requery for Device Information as we alraedy have it:
            DeviceInformation DeviceInfo; // = await DeviceInformation.CreateFromIdAsync(this.TxtBlock_SelectedID.Text);
            PairedDeviceInfo pairedDevice = (PairedDeviceInfo)ConnectDevices.SelectedItem;
            DeviceInfo = pairedDevice.DeviceInfo;

            bool success = true;
            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    this.buttonDisconnect.IsEnabled = true;
                    this.buttonSend.IsEnabled = true;
                    this.buttonStartRecv.IsEnabled = true;
                    this.buttonStopRecv.IsEnabled = false;
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    //MessageDialog md = new MessageDialog(msg, Title);
                    System.Diagnostics.Debug.WriteLine(msg);
                    //await md.ShowAsync();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BluetoothDevice"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 public BluetoothDevice(BTDevice device)
 {
     m_device = device;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BluetoothDevice"/> class.
 /// </summary>
 /// <param name="service">The RF Comm Device service.</param>
 /// <param name="disposeService">Whether this devicee should also dispose the RfcommDeviceService provided when this device disposes.</param>
 public BluetoothDevice(RfcommDeviceService service, bool disposeService = false)
 {
     m_deviceService  = service;
     m_disposeService = disposeService;
 }
        async private void Connect(DeviceInformation DeviceInfo)
        {
            System.Diagnostics.Debug.WriteLine("Connect");

            bool success = true;
            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try
                {
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                    success = false;
                    System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    this.DoButton.IsEnabled = true;
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    System.Diagnostics.Debug.WriteLine(msg);
                    this.NowConnectedDevice = DeviceInfo;
                    //Listen(); If listening works at all, output is not handle yet.
                }
                else
                {
                    this.DoButton.IsEnabled = false;
                    this.NowConnectedDevice = null;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
                _socket.Dispose();
                _socket = null;
                this.NowConnectedDevice = null;
            }
        }
        async private void ConnectDevice_Click(object sender, RoutedEventArgs e)
        {
            DeviceInformation DeviceInfo = ((PairedDeviceInfo)ConnectDevices.SelectedItem).DeviceInfo;// await DeviceInformation.CreateFromIdAsync(this.TxtBlock_SelectedID.Text);
            bool success = true;
            try
            {
                _service = await RfcommDeviceService.FromIdAsync(DeviceInfo.Id);

                if (_socket != null)
                {
                    // Disposing the socket with close it and release all resources associated with the socket
                    _socket.Dispose();
                }

                _socket = new StreamSocket();
                try { 
                    // Note: If either parameter is null or empty, the call will throw an exception
                    await _socket.ConnectAsync(_service.ConnectionHostName, _service.ConnectionServiceName);
                }
                catch (Exception ex)
                {
                        success = false;
                        System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
                }
                // If the connection was successful, the RemoteAddress field will be populated
                if (success)
                {
                    _Mode = Mode.JustConnected;
                    this.buttonDisconnect.IsEnabled = true;
                    this.buttonSend.IsEnabled = true;
                    this.buttonStartRecv.IsEnabled = true;
                    this.buttonStopRecv.IsEnabled = false;
                    string msg = String.Format("Connected to {0}!", _socket.Information.RemoteAddress.DisplayName);
                    //MessageDialog md = new MessageDialog(msg, Title);
                    string lcdMsg = "~C" + "BT Serial Connected";
                    lcdMsg += "~" + ArduinoLCDDisplay.LCD.CMD_DISPLAY_LINE_2_CH + "PressBack/Select   ";
                    Send(lcdMsg);
                    this.buttonStartRecv.IsEnabled = false;
                    this.buttonStopRecv.IsEnabled = true;
                    Listen();
                    System.Diagnostics.Debug.WriteLine(msg);
                    //await md.ShowAsync();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Overall Connect: " +ex.Message);
                _socket.Dispose();
                _socket = null;
            }
        }
Exemplo n.º 8
0
 public NmeaDevice(BTDevice device)
     : base(device)
 {
 }
Exemplo n.º 9
0
		public BluetoothDevice(BTDevice device)
		{
			m_device = device;
		}
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BluetoothDevice"/> class.
 /// </summary>
 /// <param name="service">The RF Comm Device service.</param>
 public BluetoothDevice(Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService service)
 {
     m_deviceService = service;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BluetoothDevice"/> class.
 /// </summary>
 /// <param name="service">The RF Comm Device service.</param>
 /// <param name="parser">Parser used for incoming messages</param>
 /// <param name="disposeService">Whether this devicee should also dispose the RfcommDeviceService provided when this device disposes.</param>
 public BluetoothDevice(IIncomingMessageParser parser, RfcommDeviceService service, bool disposeService = false) : base(parser)
 {
     m_deviceService  = service;
     m_disposeService = disposeService;
 }
Exemplo n.º 12
0
        public async Task <StreamSocket> InitializeSendSocket()
        {
            Windows.Networking.Sockets.StreamSocket _socket = null;
            try
            {
                // Enumerate devices with the object push service
                var services =
                    await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
                        RfcommDeviceService.GetDeviceSelector(
                            RfcommServiceId.ObexObjectPush));

                PostMessage("OBEX_Sender.Initialize", string.Format("Services count = {0}", services.Count));

                if (services.Count > 0)
                {
                    //var sdf = services[0];
                    //var azx = sdf.Name;
                    //var asxc = sdf.Kind;
                    //var azxwsx= sdf.Id;
                    //var qaaszx = sdf.Pairing;
                    //var wsdfg = sdf.EnclosureLocation;

                    //System.Diagnostics.Debug.WriteLine(azx, " " , asxc, " ", azxwsx, " ", qaaszx, " ", wsdfg);


                    // Initialize the target Bluetooth BR device
                    var service = await RfcommDeviceService.FromIdAsync(services[0].Id);

                    // Check that the service meets this App's minimum requirement
                    bool isCompatible = await IsCompatibleVersion(service);

                    if ((IgnoreAttributeErrors) && (!isCompatible))
                    {
                        PostMessage("OBEX_Sender.Initialize", "Ignoring attribute error.");
                        isCompatible = true;
                    }
                    if (SupportsProtection(service) && isCompatible)
                    {
                        _service = service;

                        // Create a socket and connect to the target
                        _socket = new StreamSocket();
                        PostMessage("OBEX_Sender.Initialize", "Connecting ...");
                        IsConnected = false;
                        await _socket.ConnectAsync(
                            _service.ConnectionHostName,
                            _service.ConnectionServiceName,
                            SocketProtectionLevel
                            .BluetoothEncryptionAllowNullAuthentication);

                        IsConnected = true;
                        PostMessage("OBEX_Sender.Initialize", "Is Connected");
                        // The socket is connected. At this point the App can wait for
                        // the user to take some action, e.g. click a button to send a
                        // file to the device, which could invoke the Picker and then
                        // send the picked file. The transfer itself would use the
                        // Sockets API and not the Rfcomm API, and so is omitted here for
                        // brevity.
                    }
                    else
                    {
                        IsConnected = false;
                        if (_socket != null)
                        {
                            _socket.Dispose();
                        }
                        PostMessage("OBEX_Sender.Initialize", "Service Not Compatible Or does not Support Protection Level ");
                    }
                }
                else
                {
                    IsConnected = false;
                    if (_socket != null)
                    {
                        _socket.Dispose();
                    }
                    PostMessage("OBEX_Sender.Initialize", "No Services");
                }
            }
            catch (Exception ex)
            {
                if (_socket != null)
                {
                    _socket.Dispose();
                }
                PostMessage("OBEX_Sender.Initialize", ex.Message);
            }
            return(_socket);
        }
Exemplo n.º 13
0
		public void Dispose()
		{
			if (tcs != null)
			{
				tcs.Cancel();
				tcs = null;
			}
			m_device = null;
			if(socket != null)
				socket.Dispose();
			socket = null;
		}