我有Windows Phone应用程序,通过蓝牙(Bluetooth App To Device)控制设备。在WP8中它运行良好。这是代码
1)搜索所有配对设备
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var peers = await PeerFinder.FindAllPeersAsync();
ObservableCollection<PeerInformation> devicesCollection = new ObservableCollection<PeerInformation>();
foreach (var peer in peers)
{
devicesCollection.Add(peer);
}
2)连接到设备
socket = new StreamSocket();
await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
3)连接到PC或其他手机以通过Obex发送文件
socket = new Windows.Networking.Sockets.StreamSocket();
await socket.ConnectAsync(_selectedDevice.HostName, "{00001105-0000-1000-8000-00805f9b34fb}");
迁移到Windows Phone 8.1(SilverLight)后,这件事情无效:
await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
我在连接期间遇到了运行时异常:找不到元素,数据不再可用,访问被拒绝
我已经拥有SilverLight功能ID_CAP_NETWORK,ID_CAP_PROXIMITY
我在Package.appmainfest中设置....
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="name:serialPort" />
<m2:Function Type="name:obexObjectPush" />
<m2:Function Type="name:obexFileTransfer" />
</m2:Device>
我尝试使用&#34; Rfcomm&#34;课程
var devicesInfoCollection = await DeviceInformation.FindAllAsync(
RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
sensorCollection.Clear();
foreach (DeviceInformation dInfo in devicesInfoCollection)
{
sensorCollection.Add(dInfo);
}
连接
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(selectedDeviceInfo.Id);
socket = new StreamSocket();
await socket.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
断线
if (dataWriter != null)
{
dataWriter.DetachStream();
dataWriter.Dispose();
dataWriter = null;
}
if (socket != null)
{
socket.Dispose();
socket = null;
}
问题在于它非常奇怪。当我尝试连接时,断开连接然后重新连接 我不能。设备从列表中消失。设备位于配对设备列表中,但它从SerialPort设备列表中消失。当我尝试连接时,我发现了#34;未找到元素&#34;。看起来电话并没有打破蓝牙连接。我必须转到蓝牙设置并手动中断。