I'm stuck for many hours now on a problem trying to communicate with a bluetooth device (not BLE).
I'm on a Windows Phone 8.1 Silverlight app.
I set the DeviceCapability section with the serviceid in the appxmanifest:
<!-- bluetooth -->
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="serviceId:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" />
</m2:Device>
</m2:DeviceCapability>
I enabled ID_CAP_PROXIMITY and ID_CAP_NETWORKING in the WPAppManifest and proximity in the appxmanifest.
I searched my device with two possible ways (and succeeded each time to get the PeerInformation), either with paired filter or serviceid:
if(serviceId.HasValue)
{
// Filter devices with serviceId
PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}";
}
else
{
// Filter devices (all paired)
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
}
// Search
var peers = await PeerFinder.FindAllPeersAsync();
// Make some filtering to get right PeerInformation
var peer = GetGoodPeerInformation();
It's when i try to write some data to my device that i got the "Element not found" Error:
try
{
//PeerFinder.Start();
//using (var streamSocket = await PeerFinder.ConnectAsync(Peer))
using (var streamSocket = new StreamSocket())
{
// I have to put manually the serviceid because the ServiceId property in the PeerInformation object is always empty...
await streamSocket.ConnectAsync(PeerInformation.HostName, "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
// data is a byte[] filled earlier
await streamSocket.OutputStream.WriteAsync(data.AsBuffer());
}
return true;
}
catch
{
return false;
}
finally
{
//PeerFinder.Stop();
}
When i remove the curly braces from the ServiceId in ConnectAsync(), I have another error (Filename incorrect 0x8007007B).
If you have any idea, thanks for helping !
[Edit: 6/4/2015]
I tested on a WinRT (WP 8.1) App, and I successfully connected to my device, changing the way of doing it:
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(new Guid("00001101-0000-1000-8000-00805F9B34FB"))));
var device = devices.First();
var service = await RfcommDeviceService.FromIdAsync(device.Id);
byte[] data = new byte[] { 0xB0, 0x11, 0x11, 0x01, 0xAA, 0xFA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB6 };
try
{
using (StreamSocket socket = new StreamSocket())
{
await socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
await socket.OutputStream.WriteAsync(data.AsBuffer());
}
}
catch (Exception)
{
throw;
}
So, I moved that piece of code to my WP8.1 Silverlight App, but the variable service is always null...
User contributions licensed under CC BY-SA 3.0