Exemplo n.º 1
0
        public async Task Initialize()
        {
            try
            {
                // Initialize the provider for the hosted RFCOMM service
                _provider = await Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider.CreateAsync(RfcommServiceId.ObexObjectPush);

                //reader = null;
                // Create a listener for this service and start listening
                StreamSocketListener listener = new StreamSocketListener();
                listener.ConnectionReceived += OnConnectionReceived;
                await listener.BindServiceNameAsync(
                    _provider.ServiceId.AsString(),
                    SocketProtectionLevel
                    .BluetoothEncryptionAllowNullAuthentication);

                // Set the SDP attributes and start advertising
                InitializeServiceSdpAttributes(_provider);
                _provider.StartAdvertising(listener);
                PostMessage("OBEX_Receiver.Initialize", "Listening");
            }
            catch (Exception ex)
            {
                PostMessage("OBEX_Receiver.Initialize", ex.Message);
            }
        }
Exemplo n.º 2
0
        private async System.Threading.Tasks.Task MakeDiscoverable()
        {
            // Make the system discoverable. Don'd repeatedly do this or the StartAdvertising will throw "cannot create a file when that file already exists"
            if (!App.IsBluetoothDiscoverable)
            {
                Guid BluetoothServiceUuid = new Guid("17890000-0068-0069-1532-1992D79BE4D8");
                try
                {
                    provider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(BluetoothServiceUuid));

                    Windows.Networking.Sockets.StreamSocketListener listener = new Windows.Networking.Sockets.StreamSocketListener();
                    listener.ConnectionReceived += OnConnectionReceived;
                    await listener.BindServiceNameAsync(provider.ServiceId.AsString(), Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket);

                    //     SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
                    // Don't bother setting SPD attributes
                    provider.StartAdvertising(listener, true);
                    App.IsBluetoothDiscoverable = true;
                }
                catch (Exception e)
                {
                    string formatString        = BluetoothDeviceInformationDisplay.GetResourceString("BluetoothNoDeviceAvailableFormat");
                    string confirmationMessage = string.Format(formatString, e.Message);
                    DisplayMessagePanelAsync(confirmationMessage, MessageType.InformationalMessage);
                }
            }
        }
            public async Task <bool> StartServer()
            {
                // Initialize the provider for the hosted RFCOMM service.
                provider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.ObexObjectPush);

                if (provider != null)
                {
                    // Create a listener for this service and start listening.
                    socketListener = new StreamSocketListener();
                    socketListener.ConnectionReceived += OnConnectionReceived;


                    await socketListener.BindServiceNameAsync(provider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

                    // Set the SDP attributes and start advertising.
                    InitializeServiceSdpAttributes(provider);
                    provider.StartAdvertising(socketListener);
                    isAdvertising = true;
                    return(true);
                }
                else
                {
                    await new MessageDialog("No").ShowAsync();
                    return(false);
                }
                return(false);
            }
 public void Disconnect()
 {
     Listening = false;
     if (provider != null)
     {
         if (isAdvertising)
         {
             provider.StopAdvertising();
         }
         provider = null;
     }                                                                                         // StopAdvertising relentlessly causes a crash if not advertising.
     if (socketListener != null)
     {
         socketListener.Dispose(); socketListener = null;
     }
     if (writer != null)
     {
         writer.DetachStream(); writer.Dispose(); writer = null;
     }
     if (reader != null)
     {
         reader.DetachStream(); reader.Dispose(); reader = null;
     }
     if (socket != null)
     {
         socket.Dispose(); socket = null;
     }
     if (listeningTask != null)
     {
         listeningTask = null;
     }
 }
        //Receive File as a Server
        public async void InitializeServer()
        {
            // Initialize the provider for the hosted RFCOMM service
            _provider =
                await Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider.CreateAsync(
                    RfcommServiceId.ObexObjectPush);

            // Create a listener for this service and start listening
            StreamSocketListener listener = new StreamSocketListener();

            listener.ConnectionReceived += OnConnectionReceivedAsync;
            await listener.BindServiceNameAsync(
                _provider.ServiceId.AsString(),
                SocketProtectionLevel
                .BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start advertising
            InitializeServiceSdpAttributes(_provider);
            _provider.StartAdvertising(listener);
        }
Exemplo n.º 6
0
 private async System.Threading.Tasks.Task MakeDiscoverable()
 {
     // Make the system discoverable. Don'd repeatedly do this or the StartAdvertising will throw "cannot create a file when that file already exists"
     if (!App.IsBluetoothDiscoverable)
     {
         Guid BluetoothServiceUuid = new Guid("17890000-0068-0069-1532-1992D79BE4D8");
         try
         {
             provider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(BluetoothServiceUuid));
             Windows.Networking.Sockets.StreamSocketListener listener = new Windows.Networking.Sockets.StreamSocketListener();
             listener.ConnectionReceived += OnConnectionReceived;
             await listener.BindServiceNameAsync(provider.ServiceId.AsString(), Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket);
             //     SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
             // Don't bother setting SPD attributes
             provider.StartAdvertising(listener, true);
             App.IsBluetoothDiscoverable = true;
         }
         catch (Exception e)
         {
             string formatString = BluetoothDeviceInformationDisplay.GetResourceString("BluetoothNoDeviceAvailableFormat");
             string confirmationMessage = string.Format(formatString, e.Message);
             DisplayMessagePanel(confirmationMessage, MessageType.InformationalMessage);
         }
     }
 }
 private RfcommServiceProvider(Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider provider)
 {
     _provider = provider;
 }