首先是一个简单的布局activity_chat.xml,就是后面进行通信的交互界面
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="@+id/content_text"/>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/screenshot"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/input_edittext"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:id="@+id /send_button"
android:onClick="onButtonClick"
/>
</LinearLayout>
</LinearLayout>
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="@+id/content_text"/>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/screenshot"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/input_edittext"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:id="@+id /send_button"
android:onClick="onButtonClick"
/>
</LinearLayout>
</LinearLayout>
因为android设备之间的连接涉及到主从设备的问题,所以在这边有一个区分,device包下有BaseChatActivity,ChatAtivity,ConnectActivity
public class ConnectActivity extends AppCompatActivity {
public static final String DEVICE_EXTRA_KEY = "device";
private UsbManager mUsbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
if (deviceList == null || deviceList.size() == 0) {
final Intent intent=new Intent(this, InfoActivity.class);
startActivity(intent);
finish();
return;
}
if (searchForUsbAccessory(deviceList)) {
return;
}
for (UsbDevice device:deviceList.values()) {
initAccessory(device);
}
finish();
}
private boolean searchForUsbAccessory(final HashMap<String, UsbDevice> deviceList) {
for (UsbDevice device:deviceList.values()) {
if (isUsbAccessory(device)) {
public static final String DEVICE_EXTRA_KEY = "device";
private UsbManager mUsbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
if (deviceList == null || deviceList.size() == 0) {
final Intent intent=new Intent(this, InfoActivity.class);
startActivity(intent);
finish();
return;
}
if (searchForUsbAccessory(deviceList)) {
return;
}
for (UsbDevice device:deviceList.values()) {
initAccessory(device);
}
finish();
}
private boolean searchForUsbAccessory(final HashMap<String, UsbDevice> deviceList) {
for (UsbDevice device:deviceList.values()) {
if (isUsbAccessory(device)) {