我正在開發一個Windows應用商店,並試圖通過藍牙發送文件到android(和windows phone)設備。通過藍牙從winRT發送文件到android/WP設備
基於一個example from MSDN我寫了下面的代碼:
public async static void SendAsync(StorageFile file)
{
var id = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush);
var devices = await DeviceInformation.FindAllAsync(id);
// -> Returns one windows phone and two android devices
if (devices.Count > 0)
{
// Use the 3th device (android tablet)
var service = await RfcommDeviceService.FromIdAsync(devices[2].Id);
// Create a socket and connect to the target
using (var socket = new StreamSocket())
{
await socket.ConnectAsync(
service.ConnectionHostName,
service.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
byte[] bytes;
using (var stream = await file.OpenStreamForReadAsync())
{
// stream.Length = 4621038
bytes = new Byte[stream.Length];
await stream.ReadAsync(bytes, 0, bytes.Length);
}
using (var dataWriter = new DataWriter(socket.OutputStream))
{
dataWriter.WriteBytes(bytes);
Debug.WriteLine("Sending data...");
var result = await dataWriter.StoreAsync();
var flushed = await dataWriter.FlushAsync();
dataWriter.DetachStream();
Debug.WriteLine("Sending data finished. Result: " + result + " flushed: " + flushed);
// Output:
// Sending data...
// Sending data finished. Result: 4621038 flushed: True
}
}
}
}
的Package.appxmanifest樣子:
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="name:obexObjectPush" />
<m2:Function Type="name:obexFileTransfer" />
<m2:Function Type="name:genericFileTransfer" />
</m2:Device>
</m2:DeviceCapability>
運行代碼時,是似乎工作。該應用程序問「可能[AppName]使用您的[DeviceName]?」並且這些字節似乎是發送的(dataWriter.StoreAsync()返回要發送的字節數)。
Android設備將被激活(亮起)一秒鐘。但就是這些。我希望在Android設備上獲取請求,例如:「Windows 8嘗試發送文件,接受是/否」,但我不這樣做。
發送的文件不在android設備上(通常情況下,通過藍牙發送的文件位於Bluetooth文件夾中)。
你有什麼想法如何使它工作/發現錯誤?
感謝, 揚
鏈接從您的文章中缺失。 – Nipun