Sending files via bluetooth from winRT to android / WP devices

天涯浪子 提交于 2019-12-08 06:19:55

问题


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

Based on an example from MSDN I wrote the following 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
            }
        }
    }
}

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>

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

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.

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?

Thanks, Jan


回答1:


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



回答2:


You have to implement obex protocol Connect , put ,disconnect operations to do this. please refer This THREAD : Send data through Bluetooth windows 10 universal app



来源:https://stackoverflow.com/questions/22140947/sending-files-via-bluetooth-from-winrt-to-android-wp-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!