I ran into this alongside the findAllAsync trouble I was having in the previous post. To be more specific, I've been converting a small sample that's in the ZIP file for //build 2013 session 3-026 (http://channel9.msdn.com/Events/Build/2013/3-026, the ZIP is http://files.channel9.msdn.com/thumbnail/f9025b07-120c-4320-8de6-a5de2d255b7c.zip) . This is a C# sample to talk to a Sphero device using the Windows 8.1 Bluetooth.Rfcomm API.

The problem I was having was that write.storeAsync (write is a Windows.Storage.Streams.DataWriter object) was throwing an exception, hence my try/catch block:

function changeColor() {
    output.innerText = "";

    if (writer == null) {
        return;
    }

    var packet = generateRandomColorPacket();
    writer.writeBytes(packet);

    try { 
        writer.storeAsync().done(function () {
        }, function (e) {
            output.innerText = "Failed to write packet; " + e;
        });
    } catch (e) {
        output.innerText = "DataWriter.storeAsync failed (exception); " + e;
    }
}

The exception message wasn't at all helpful: it just said: "The operation identifier is not valid." Huh?

With the help of some colleagues in the Bluetooth team who looked this over, we found a small glitch in the code below that opens the socket to the device. Do you see the error?

function openSocket() {
    output.innerText = "";

    if (device == null) {
        return;
    }

    // Create a socket and connect to the target
    var sockNS = Windows.Networking.Sockets;
    socket = new sockNS.StreamSocket();
    socket.connectAsync(device.connectionHostName, device.connectionServiceName,
        sockNS.SocketProtectionLevel.bluetoothEncryptionAllowNullAuthentication)
        .done(function () {
            writer = new Windows.Storage.Streams.DataWriter(socket.OutputStream);
        }, function (e) {
            output.innerText = "Failed to open socket; " + e;
        });
}

Don't feel bad if you missed it, because I did. It's this line here, which I'd brought over from the C# code. Now do you see the problem?

writer = new Windows.Storage.Streams.DataWriter(socket.OutputStream);

It's hard to see: socket.OutputStream should be socket.outputStream! In other words, I was creating a DataWriter with undefined rather than the real stream. DataWriter didn't complain, though, until it tried to access the backing stream via storeAsync.

Changing that one character solved the problem.


One Comment

  1. Nasrin
    Posted January 21, 2015 at 11:30 pm | Permalink

    Hi ,

    I am getting the same error. But the StreamSocket Class does not have a field named "outputStream"