Search code examples
c#uwpwindows-10-iot-core

Can Raspberry Pi running Win10 IoT uses both Wifi and Bluetooth communication at the same time?


I'm still a beginner to programming. I have met some problem of my problem, where the pi couldnt use the wifi and bluetooth at the same time. This is because initially the code without the bluetooth element, it works fine. But when i added the bluetooth code into the program, it just went weird. It couldnt retrieve any data from the Firebase through Wifi. Is it really that they couldnt work together at the same time or it is wrong with the code. Is it because of the socketstream something like this? Really thanks for the help.

Link to the code as it is too long.


Solution

  • I have tested with your code. The app will get an exception as "Access is denied. ", it will cause the app crash. So you need to add try catch in the function DeviceWatcher_Added to ensure the app won't dump when the Bluetooth could not connect.

        private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            try
            {
                var device = await BluetoothDevice.FromIdAsync(args.Id);
    
                var services = await device.GetRfcommServicesAsync();
                if (services.Services.Count > 0)
                {
                    var service = services.Services[0];
    
                    stream = new StreamSocket();
                    await stream.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
    
                    rx = new DataReader(stream.InputStream);
                    tx = new DataWriter(stream.OutputStream);
    
                    await this.Dispatcher.RunAsync(
                        Windows.UI.Core.CoreDispatcherPriority.Normal,
                        () => { Device_9.IsEnabled = true; });
    
                    deviceWatcher.Stop();
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
    

    BTW, you may refer to the sample mentioned in my first comment, it shows how to connect the RfcommService to communicate with Bluetooth. Please note that, in the sample the server creates the RfcommServiceProvider by guid, and the client connects the server with RfcommService from this guid as uuid.

            var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(
                RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid), BluetoothCacheMode.Uncached);