Alljoyn 如何利用Raw格式传输文件

这里的利用到了demo中的RawClient和RawService。请参考SDK。
下面的是demo中传输一个字符串所采用的方式:
if(mIsConnected == false|| mStreamUp == false) {
                    try{
 
                        shortcontactPort = 888;
                         
 
                        SessionOpts sessionOpts = newSessionOpts();
                        sessionOpts.traffic = SessionOpts.TRAFFIC_RAW_RELIABLE;
                        Mutable.IntegerValue sessionId = newMutable.IntegerValue();
                        Status status = mBus.joinSession(RAW_SERVICE_NAME, contactPort, sessionId, sessionOpts,newSessionListener());            
                        if(status != Status.OK) {
                            break;
                        }
                        mRawSessionId = sessionId.value;
 
                        Mutable.IntegerValue sockFd = newMutable.IntegerValue();
                        status = mBus.getSessionFd(mRawSessionId, sockFd);
                        if(status != Status.OK) {
                            break;
                        }
 
 
                        Field field = FileDescriptor.class.getDeclaredField("descriptor");
                        field.setAccessible(true);
                        FileDescriptor fd = newFileDescriptor();
                        field.set(fd, sockFd.value);
                         
                        mOutputStream = newFileOutputStream(fd);
                        mStreamUp = true;
                    }catch(Throwable ex) {
                        Log.i(TAG,"ERROR "+ex);
                    }
                }
 


////doSendFile(); 如果传输文件则替换下面的代码为这个函数即可。
                if(mStreamUp == true) {
                    try{                       
                    String string = "AllJoynis a peer-to-peer technology that enables ad hoc proximity-based";/*in my case I have a big string, something like 400 lines*/                        
                         
                        mOutputStream.write(string.getBytes());
                        mOutputStream.flush();
                         
                    }catch(IOException ex) {
                        logInfo("Exception writing and flushing the file");
                    }
                }

如果是传输文件的话,需要改写,即把if(mStreamUp == true)后面的代码改写为如下的函数即可。

private void doSendFile(){
 
        Log.i(TAG,"doSendFile()");
 
        if(mStreamUp == true){
 
            try{
            String filepath = "/sdcard/Teste.jpg";             
 
            file = newFile(filepath);             
 
                mOutputStream.write(getBytesFromFile(file));
 
                mOutputStream.flush();                                     
 
            }catch(Exception ex) {
 
                Log.i(TAG,"ERRO writing file "+ex);
 
            }
        }  
    }
 
public byte[] getBytesFromFile(File file) throwsIOException {
 
        Log.i(TAG,"getBytesFromFile()");  
 
        InputStream is = newFileInputStream(file);
 
        longlength = file.length();
 
        if(length > Integer.MAX_VALUE) {
 
            System.out.println("Arquivo muito grande");
 
        }
 
        byte[] bytes = newbyte[(int) length];
 
        intoffset = 0;
 
        intnumRead = 0;
 
        while(offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >=0) {
 
            offset += numRead;
 
        }
        if(offset < bytes.length) {
 
            thrownewIOException("Could not completely read file " + file.getName());
 
        }
        is.close();
        returnbytes;
 
    }

你可能感兴趣的:(Alljoyn 如何利用Raw格式传输文件)