Conexão RFCOMM funciona instável
Eu tenho o aplicativo Windows Phone, que controla um dispositivo por Bluetooth (Bluetooth App To Device). No WP8, funciona bem. Isso é código
1) pesquisando todos os dispositivos emparelhados
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var peers = await PeerFinder.FindAllPeersAsync();
ObservableCollection<PeerInformation> devicesCollection = new ObservableCollection<PeerInformation>();
foreach (var peer in peers)
{
devicesCollection.Add(peer);
}
2) conectar ao dispositivo
socket = new StreamSocket();
await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
3) conecte ao PC ou a outro telefone para enviar o arquivo por Obex
socket = new Windows.Networking.Sockets.StreamSocket();
await socket.ConnectAsync(_selectedDevice.HostName, "{00001105-0000-1000-8000-00805f9b34fb}");
Após a migração para o Windows Phone 8.1 (SilverLight), isso não funciona:
await socket.ConnectAsync(selectedPeer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
Eu tenho uma exceção de tempo de execução durante a conexão: elemento não encontrado, dados não estão mais disponíveis, acesso negado
Eu já tenho os recursos SilverLight ID_CAP_NETWORK, ID_CAP_PROXIMITY
Eu configurei .... no 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>
Eu tentei usar as classes "Rfcomm"
var devicesInfoCollection = await DeviceInformation.FindAllAsync(
RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
sensorCollection.Clear();
foreach (DeviceInformation dInfo in devicesInfoCollection)
{
sensorCollection.Add(dInfo);
}
Conexão
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(selectedDeviceInfo.Id);
socket = new StreamSocket();
await socket.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
Desconexão
if (dataWriter != null)
{
dataWriter.DetachStream();
dataWriter.Dispose();
dataWriter = null;
}
if (socket != null)
{
socket.Dispose();
socket = null;
}
O problema é que funciona muito estranho. Quando tento conectar, desconectar e conectar novamente, não consigo. O dispositivo desaparece da lista. O dispositivo está na lista de dispositivos emparelhados, mas desaparece da lista de dispositivos SerialPort. Quando tento conectar, recebo "Elemento não encontrado". Parece que o telefone não interrompe a conexão Bluetooth. Eu tenho que ir para as configurações de Bluetooth e quebrá-lo manualmente.