我正在开发一个Windows应用商店应用并尝试通过蓝牙将文件发送到Android(和Windows手机)设备。
基于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设备上(通常情况下,通过蓝牙发送的文件位于蓝牙文件夹中)。
你有什么想法让它发挥作用/发现错误吗?
谢谢, 扬
答案 0 :(得分:0)
刚才简短的说明。 OBEX有一个协议,它不只是发送原始文件。我会稍后再写,但我们会看到类似的东西:
CONNECT->
<-OK
PUT+filename+data->
<-CONTINUE
PUT+FINAL+data->
<-OK
disconnect
答案 1 :(得分:0)
您必须实施obex协议连接,放置,断开操作才能执行此操作。 请参考此线程: Send data through Bluetooth windows 10 universal app