0
votes

I connected the Windows Phone 8 with arudino using bluetooth as given in sample here

http://developer.nokia.com/community/wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth#Complete_example

This is working okay for Windows Phone 8 but when i retarget the app to Windows Phone Silverlight 8.1, i am getting Debugger.Break and on continue,i get exception "exception has been thrown by target of invocation".

I used code:

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
        var pairedDevices = await PeerFinder.FindAllPeersAsync();

        if (pairedDevices.Count == 0)
        {
            Debug.WriteLine("No paired devices were found.");
        }
        else
        { 
            foreach (var pairedDevice in pairedDevices)
            {
                if (pairedDevice.DisplayName == DeviceName.Text)
                {
                    connectionManager.Connect(pairedDevice.HostName);
                    ConnectAppToDeviceButton.Content = "Connected";
                    DeviceName.IsReadOnly = true;
                    ConnectAppToDeviceButton.IsEnabled = false;
                    continue;
                }
            }
        } 

Where connect function is defined as:

public async void Connect(HostName deviceHostName)
    {
        if (socket != null)
        {
            await socket.ConnectAsync(deviceHostName, "1");
            dataReader = new DataReader(socket.InputStream);
            dataReadWorker.RunWorkerAsync();
            dataWriter = new DataWriter(socket.OutputStream);
        }
    }

Please Help Me.

1

1 Answers

1
votes

Have you tried working with the Windows.Devices.Bluetooth.Rfcomm namespace? It's pretty much made for Windows 8.1 bluetooth communication.

Setting device capabilities.

    <m2:DeviceCapability Name="bluetooth.rfcomm">
  <m2:Device Id="any">
    <m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>
  </m2:Device>
</m2:DeviceCapability>

Selecting Device: This is where you have to parse the Guid of the device you are using. Afterwards you use the parsed Guid to find every device that offers that service. (I used the SerialPort Guid)

Guid RfcommChatServiceUuid = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");
await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(RfcommChatServiceUuid)));

Connecting: DeviceInformation returns a list of DeviceInformation. With a chatserviceInfo.Id you can create a new RfcommDeviceService. (In this case it's called "service")

StreamSocket _socket;    
RfcommDeviceService service = await RfcommDeviceService.FromIdAsync(chatserviceInfo1._id);
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);

Disconnecting:

this._socket.Dispose();
this._socket = null;