通过蓝牙从WinRT的发送文件到Android / WP设备 [英] Sending files via bluetooth from winRT to android / WP devices

查看:143
本文介绍了通过蓝牙从WinRT的发送文件到Android / WP设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发Windows商店应用,并试图通过蓝牙发送文件到Android(和Windows Phone)的设备。

I am developing a windows store app and trying to send files via bluetooth to android (and windows phone) devices.

以下code:

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如下:

The Package.appxmanifest looks like:

<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>

在运行code,是似乎工作。该应用程序问:愿[AppName的]使用[设备名称]?和字节似乎被发送(dataWriter.StoreAsync()返回的字节数来发送)。

When running the code, is seems to work. The app ask "May the [AppName] use your [DeviceName]?" and the bytes seems to be send (dataWriter.StoreAsync() returns the number of bytes to send).

在Android设备将被激活(灯亮)如属第二。但仅此而已。我希望得到像在Android设备上的请求:Windows 8中试图发送一个文件,接受是/否,但我不

The android device will be activated (light goes on) for a second. But that's all. I would expect to get a request on the android device like: "Windows 8 tries to send a file, accept yes/no", but i don't.

发送的文件不位于Android设备上(normaly,文件通过蓝牙位于蓝牙发送文件夹)。

The sent file is not located on the android device (normaly, files send via bluetooth are located in the Bluetooth folder).

你有什么想法如何使它工作/发现错误了吗?

Do you have any ideas how to make it work / found the mistake?

谢谢,

推荐答案

只是一个简短的说明刚才。 OBEX有一个协议,它不只是把文件中的原料。我会写多了以后,但是我们看到类似这样的:

Just a brief note just now. OBEX has a protocol, it doesn't just send the file in the raw. I'll write more later but we see something like:

CONNECT->
<-OK
PUT+filename+data->
<-CONTINUE
PUT+FINAL+data->
<-OK
disconnect

这篇关于通过蓝牙从WinRT的发送文件到Android / WP设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆