2014-01-09 139 views
2

我有一個應用程序需要實現「共享」選項,以將設備本地數據庫中包含的數據傳輸到另一個設備,該設備可以將該數據插入到該設備設備數據庫中的適當表格。將數據庫數據從一個Android設備傳輸到另一個設備

我有藍牙通信工作,我只是尋找一個很好的方式來傳輸這些數據。有沒有簡單的方法在設備A上執行sqlite dump,使用藍牙將其傳輸到設備B,並讓設備B重新插入此數據?

回答

1

實施ContentProvider是您如何利用Android框架對數據庫執行CRUD操作。

一個好的方法可能是將一行轉換成Json,並在接收端解包該字符串以插入它。這種方式更易於測試,更容易從發送過程中的錯誤中恢復,但可能會很慢,具體取決於數據的大小。爲了加快速度,我會批量發送多行並測試以查看哪種批量大小對於速度和可靠性是最好的。

一種快速的方法可能是轉儲數據平面文件,然後用FileProvider提供訪問該文件作爲URI和嘗試像從here

public void sendFile(Uri uri, BluetoothSocket bs) throws IOException { 
      BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri)); 
      OutputStream os = bs.getOutputStream(); 
     try { 
      int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     // we need to know how may bytes were read to write them to the byteBuffer 
     int len = 0; 
     while ((len = bis.read(buffer)) != -1) { 
      os.write(buffer, 0, len); 
     } 
    } finally { 
     bis.close(); 
     os.flush(); 
     os.close(); 
     bs.close(); 
    } 
} 
以下
相關問題