简体   繁体   中英

Windows Phone Silverlight 8.1 Bluetooth app to device element not found exception

I am trying to connect to bluetooth printer from my windows Phone silverlight 8.1 app but I am getting the following exception at _socket.ConnectAsync() method

{System.Exception: Element not found. (Exception from HRESULT: 0x80070490)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PhoneApp1.MainPage.<ConnectToDevice>d__4.MoveNext()}

My app is running on BLUEBIRD Windows 8.1 handheld device. My code looks like

        _socket = new StreamSocket();
        await _socket.ConnectAsync(peer.HostName ,"1" );

I have internetClientServer and proximity capabilities enabled

Same code works on app targetting Windows Phone 8

I finally got the solution of this problem. The main goal is in ConnectAsync method second parameter (Service Name) which says to application which service is supported by device (Something like TCP port). When you programming both sides of comunication then you simply can put some number on both sides, but most of hardwere devices has own services built-in. So you need to know which service ID you hardware supporting, but for most of these devices like printers it's serialPort. So you need to use diferent method for search a devices which fills peer.ServiceName too and then connect with service name.

Thats real mistake from msdn manual for Windows phone bluetooth just simply put 1 for serviceName.

Finally you must set capabilities manualy to specific service like this:

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

And you peer finder must be initialized like this

PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{00001101-0000-1000-8000-00805f9b34fb}";

All you need to know about how to search peers and connect to device is on this beautiful blog.

http://dream-forever.net/Blog/2013/11/28/using-bluetooth-in-windows-phone-8/

Also you maybe will need to use another services which list is here:

http://msdn.microsoft.com/en-US/library/windows/apps/dn263090

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM