2016-04-15 209 views
0

好的我正在爲一些藍牙作品製作應用程序,所以有線程類,我用它與其他設備連接,現在我想將該線程類實例傳遞給另一個活動,所以我不必重新連接它,因此我實現該線程類作爲Parcelable但它不工作,並拋出空指針異常。如何將線程類對象從活動傳遞到使用parcelable的活動

這裏是threa類ClientThread.java: -

public class ClientThread extends Thread implements Runnable,Parcelable { 

private BluetoothDevice device; 
private final BluetoothSocket socket; 
private Context context; 
private BluetoothAdapter adapter; 
private static String address = "14:36:05:7B:39:9B";//"98:D3:31:60:06:CA"; 
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private Handler handle; 
protected InputStream is = null; 
protected OutputStream os = null; 
private boolean first; 
private String password; 

public ClientThread(Context context, Handler handle, String pass, boolean check) { 
    this.context = context; 
    adapter = BluetoothAdapter.getDefaultAdapter(); 
    this.password = pass; 
    this.handle = handle; 
    this.first = check; 
    BluetoothSocket tmpSocket = null; 

    try { 
     device = adapter.getRemoteDevice(address); 
     tmpSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    socket = tmpSocket; 
} 

@Override 
public void run() { 
    // TODO Auto-generated method stub 
    try { 

     adapter.cancelDiscovery(); 

     socket.connect(); 

     handle.sendEmptyMessage(2); 

     dataTransfer(socket); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     handle.sendEmptyMessage(3); 
     e.printStackTrace(); 
     try { 
      socket.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

    } 
} 

private boolean listen = true; 

public void dataTransfer(BluetoothSocket soc) throws IOException { 

    is = soc.getInputStream(); 
    os = soc.getOutputStream(); 

    byte[] buffer = new byte[256]; 
    int bytes; 

    if (first) { 
     send(("Validate" + password).getBytes()); 
     first = false; 
    } 

    while (listen) { 
     try { 
      bytes = is.read(buffer); 
      handle.obtainMessage(1, bytes, -1, buffer).sendToTarget(); 
      // final String data = new String(buffer, 0, bytes); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

public void send(byte[] buffer) { 
    try { 
     os.write(buffer); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void cancel() { 
    try { 
     listen = false; 
     socket.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

int mData=0; 

@Override 
public int describeContents() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    // TODO Auto-generated method stub 

} 

public static final Parcelable.Creator<ClientThread> CREATOR = new Parcelable.Creator<ClientThread>() { 
    public ClientThread createFromParcel(Parcel in) { 
     return new ClientThread(in); 
    } 

    public ClientThread[] newArray(int size) { 
     return new ClientThread[size]; 
    } 
}; 

// example constructor that takes a Parcel and gives you an object populated with it's values 
private ClientThread(Parcel in) { 
    mData = in.readInt(); 
    socket=null; 
} 

} 

請幫助我如何正確地使它parcelable,這樣我可以傳遞的B/W活動

回答

3

您無法通過Intent額外的Thread,作爲Thread無法制作ParcelableSerializable。或者:

  • 這些不應該是單獨的活動,而應該只是一個具有不斷變化的UI的活動(例如,通過片段);或

  • 此線程應該由Service管理,特別是如果線程需要處於活動狀態,即使用戶從您的應用程序導航到其他地方也是如此;或

  • 這個線程應該管理仔細一些獨立的,並沒有任何

+0

OK,你可以展示如何實現這一目標的活動或任何referel環節所擁有 –

相關問題