这是我的代码的主要活动java文件
package com.ramimartin.sample.multibluetooth;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ToggleButton;
import com.ramimartin.multibluetooth.activity.BluetoothFragmentActivity;
import com.ramimartin.multibluetooth.bluetooth.mananger.BluetoothManager;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class MainActivity extends BluetoothFragmentActivity implements DiscoveredDialogFragment.DiscoveredDialogListener {
@InjectView(R.id.listview)
ListView mListView;
ArrayAdapter<String> mAdapter;
List<String> mListLog;
@InjectView(R.id.communication)
EditText mEditText;
@InjectView(R.id.send)
ImageButton mSendBtn;
@InjectView(R.id.client)
ToggleButton mClientToggleBtn;
@InjectView(R.id.serveur)
ToggleButton mServerToggleBtn;
@InjectView(R.id.connect)
Button mConnectBtn;
@InjectView(R.id.disconnect)
Button mDisconnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mListLog = new ArrayList<String>();
mAdapter = new ArrayAdapter<String>(this, R.layout.item_console, mListLog);
mListView.setAdapter(mAdapter);
}
@Override
public int myNbrClientMax() {
return 7;
}
@OnClick(R.id.serveur)
public void serverType() {
setLogText("===> Start Server ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_3600_SEC);
selectServerMode();
mServerToggleBtn.setChecked(true);
mClientToggleBtn.setChecked(false);
mConnectBtn.setEnabled(true);
mConnectBtn.setText("Scan Devices");
}
@OnClick(R.id.client)
public void clientType() {
setLogText("===> Start Client ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_120_SEC);
selectClientMode();
mServerToggleBtn.setChecked(false);
mClientToggleBtn.setChecked(true);
mConnectBtn.setEnabled(true);
}
@OnClick(R.id.connect)
public void connect() {
setLogText("===> Start Scanning devices ...");
if (getTypeBluetooth() == BluetoothManager.TypeBluetooth.Client) {
showDiscoveredDevicesDialog();
}
scanAllBluetoothDevice();
}
@OnClick(R.id.disconnect)
public void disconnect() {
setLogText("===> Disconnect");
disconnectClient();
disconnectServer();
}
@OnClick(R.id.send)
public void send() {
if (isConnected()) {
sendMessage(mEditText.getText().toString());
setLogText("===> Send : " + mEditText.getText().toString());
}
}
@Override
public void onBluetoothStartDiscovery() {
setLogText("===> Start discovering ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
}
@Override
public void onBluetoothDeviceFound(BluetoothDevice device) {
if(getTypeBluetooth() == BluetoothManager.TypeBluetooth.Server) {
setLogText("===> Device detected and Thread Server created for this address : " + device.getAddress());
}else{
setLogText("===> Device detected : "+ device.getAddress());
}
}
@Override
public void onClientConnectionSuccess() {
setLogText("===> Client Connexion success !");
mEditText.setText("Client");
mSendBtn.setEnabled(true);
mConnectBtn.setEnabled(false);
mDisconnect.setEnabled(true);
}
@Override
public void onClientConnectionFail() {
setLogText("===> Client Connexion fail !");
mServerToggleBtn.setChecked(false);
mClientToggleBtn.setChecked(false);
mDisconnect.setEnabled(false);
mConnectBtn.setEnabled(false);
mConnectBtn.setText("Connect");
mEditText.setText("");
}
@Override
public void onServeurConnectionSuccess() {
setLogText("===> Serveur Connexion success !");
mEditText.setText("Server");
mDisconnect.setEnabled(true);
}
@Override
public void onServeurConnectionFail() {
setLogText("===> Serveur Connexion fail !");
}
@Override
public void onBluetoothCommunicator(String messageReceive) {
setLogText("===> receive msg : " + messageReceive);
}
@Override
public void onBluetoothNotAviable() {
setLogText("===> Bluetooth not aviable on this device");
mSendBtn.setEnabled(false);
mClientToggleBtn.setEnabled(false);
mServerToggleBtn.setEnabled(false);
mConnectBtn.setEnabled(false);
mDisconnect.setEnabled(false);
}
public void setLogText(String text) {
mListLog.add(text);
mAdapter.notifyDataSetChanged();
mListView.setSelection(mListView.getCount() - 1);
}
private void showDiscoveredDevicesDialog() {
String tag = DiscoveredDialogFragment.class.getSimpleName();
DiscoveredDialogFragment fragment = DiscoveredDialogFragment.newInstance();
fragment.setListener(this);
showDialogFragment(fragment, tag);
}
private void showDialogFragment(DialogFragment dialogFragment, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialogFragment, tag);
ft.commitAllowingStateLoss();
}
@Override
public void onDeviceSelectedForConnection(String addressMac) {
setLogText("===> Connect to " + addressMac);
createClient(addressMac);
}
@Override
public void onScanClicked() {
scanAllBluetoothDevice();
}
}
****************************************************************************************************************************************************
这是应用程序的布局
stackoverflow不允许我提问,我是初学者,很难问问题plz试着找出错误 你的回复将不胜感激<RelativeLayout 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:background="@android:color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rami_logo"
android:layout_centerHorizontal="true"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/communication"
android:divider="@android:color/transparent"
android:layout_alignParentTop="true">
</ListView>
<EditText
android:id="@+id/communication"
android:layout_above="@+id/connexion"
android:singleLine="true"
android:layout_toLeftOf="@+id/send"
android:layout_marginBottom="2dp"
android:textColor="@android:color/white"
android:dividerHeight="0dip"
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/send"
android:layout_above="@+id/connexion"
android:layout_alignTop="@+id/communication"
android:layout_alignParentRight="true"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:src="@drawable/send"/>
<LinearLayout
android:id="@+id/connexion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/connect"
android:padding="2dp">
<ToggleButton
android:id="@+id/client"
android:layout_weight="1"
android:enabled="true"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Client"
android:textOn="Client"/>
<ToggleButton
android:id="@+id/serveur"
android:layout_weight="1"
android:enabled="true"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Serveur"
android:textOn="Serveur"/>
</LinearLayout>
<Button
android:id="@+id/connect"
android:layout_weight="1"
android:layout_margin="2dp"
android:enabled="false"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/disconnect"
android:text="Connect"/>
<Button
android:id="@+id/disconnect"
android:layout_margin="2dp"
android:enabled="false"
android:textColor="@android:color/white"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disconnect"/>
</RelativeLayout>