2017-03-17 73 views
1

我已修改蓝牙聊天应用程序同时发送两个字符串(交易号码&密码),问题是当我按发送按钮只有一个字符串(密码)将发送,我不知道如何通过两个字符串Simulataneously .. This is my Application Model蓝牙聊天应用程序中不能同时发送两个字符串?

主要活动

private EditText et_tnum; 
private EditText et_pass; 
private TextView status; 
private Button btnConnect; 
private ListView listView; 
private Dialog dialog; 
private TextInputLayout inputLayout; 
private ArrayAdapter<String> chatAdapter; 
private ArrayList<String> chatMessages; 
private BluetoothAdapter bluetoothAdapter; 

public static final int MESSAGE_STATE_CHANGE = 1; 
public static final int MESSAGE_READ = 2; 
public static final int MESSAGE_WRITE = 3; 
public static final int MESSAGE_DEVICE_OBJECT = 4; 
public static final int MESSAGE_TOAST = 5; 
public static final String DEVICE_OBJECT = "device_name"; 

private static final int REQUEST_ENABLE_BLUETOOTH = 1; 
private ChatController chatController; 
private BluetoothDevice connectingDevice; 
private ArrayAdapter<String> discoveredDevicesAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    findViewsByIds(); 

    //check device support bluetooth or not 
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (bluetoothAdapter == null) { 
     Toast.makeText(this, "Bluetooth is not available!", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 

    //show bluetooth devices dialog when click connect button 
    btnConnect.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showPrinterPickDialog(); 
     } 
    }); 

    //set chat adapter 
    chatMessages = new ArrayList<>(); 
    chatAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, chatMessages); 
    listView.setAdapter(chatAdapter); 
} 

private Handler handler = new Handler(new Handler.Callback() { 

    @Override 
    public boolean handleMessage(Message msg) { 
     switch (msg.what) { 
      case MESSAGE_STATE_CHANGE: 
       switch (msg.arg1) { 
        case ChatController.STATE_CONNECTED: 
         setStatus("Connected to: " + connectingDevice.getName()); 
         btnConnect.setEnabled(false); 
         break; 
        case ChatController.STATE_CONNECTING: 
         setStatus("Connecting..."); 
         btnConnect.setEnabled(false); 
         break; 
        case ChatController.STATE_LISTEN: 
        case ChatController.STATE_NONE: 
         setStatus("Not connected"); 
         break; 
       } 
       break; 
      case MESSAGE_WRITE: 
       byte[] writeBuf = (byte[]) msg.obj; 

       String writeMessage = new String(writeBuf); 
       chatMessages.add("Me: " + writeMessage); 
       chatAdapter.notifyDataSetChanged(); 
       break; 
      case MESSAGE_READ: 
       byte[] readBuf = (byte[]) msg.obj; 

       String readMessage = new String(readBuf, 0, msg.arg1); 
       chatMessages.add(connectingDevice.getName() + ": " + readMessage); 
       chatAdapter.notifyDataSetChanged(); 
       break; 
      case MESSAGE_DEVICE_OBJECT: 
       connectingDevice = msg.getData().getParcelable(DEVICE_OBJECT); 
       Toast.makeText(getApplicationContext(), "Connected to " + connectingDevice.getName(), 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case MESSAGE_TOAST: 
       Toast.makeText(getApplicationContext(), msg.getData().getString("toast"), 
         Toast.LENGTH_SHORT).show(); 
       break; 
     } 
     return false; 
    } 
}); 

private void showPrinterPickDialog() { 
    dialog = new Dialog(this); 
    dialog.setContentView(R.layout.layout_bluetooth); 
    dialog.setTitle("Bluetooth Devices"); 

    if (bluetoothAdapter.isDiscovering()) { 
     bluetoothAdapter.cancelDiscovery(); 
    } 
    bluetoothAdapter.startDiscovery(); 

    //Initializing bluetooth adapters 
    ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1); 
    discoveredDevicesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1); 

    //locate listviews and attatch the adapters 
    ListView listView = (ListView) dialog.findViewById(R.id.pairedDeviceList); 
    ListView listView2 = (ListView) dialog.findViewById(R.id.discoveredDeviceList); 
    listView.setAdapter(pairedDevicesAdapter); 
    listView2.setAdapter(discoveredDevicesAdapter); 

    // Register for broadcasts when a device is discovered 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(discoveryFinishReceiver, filter); 

    // Register for broadcasts when discovery has finished 
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(discoveryFinishReceiver, filter); 

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); 

    // If there are paired devices, add each one to the ArrayAdapter 
    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
      pairedDevicesAdapter.add(device.getName() + "\n" + device.getAddress()); 
     } 
    } else { 
     pairedDevicesAdapter.add(getString(R.string.none_paired)); 
    } 

    //Handling listview item click event 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      bluetoothAdapter.cancelDiscovery(); 
      String info = ((TextView) view).getText().toString(); 
      String address = info.substring(info.length() - 17); 

      connectToDevice(address); 
      dialog.dismiss(); 
     } 

    }); 

    listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      bluetoothAdapter.cancelDiscovery(); 
      String info = ((TextView) view).getText().toString(); 
      String address = info.substring(info.length() - 17); 

      connectToDevice(address); 
      dialog.dismiss(); 
     } 
    }); 

    dialog.findViewById(R.id.cancelButton).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    dialog.setCancelable(false); 
    dialog.show(); 
} 

private void setStatus(String s) { 
    status.setText(s); 
} 

private void connectToDevice(String deviceAddress) { 
    bluetoothAdapter.cancelDiscovery(); 
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); 
    chatController.connect(device); 
} 

private void findViewsByIds() { 
    status = (TextView) findViewById(R.id.status); 
    btnConnect = (Button) findViewById(R.id.btn_connect); 
    listView = (ListView) findViewById(R.id.list); 
    et_tnum = (EditText) findViewById(R.id.et_tnum); 
    et_pass = (EditText) findViewById(R.id.et_pass); 
    View btnSend = (Button) findViewById(R.id.btn_send); 

    btnSend.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (et_tnum.getText().toString().equals("")) { 
       Toast.makeText(MainActivity.this, "Please input some texts", Toast.LENGTH_SHORT).show(); 
      } else { 
       //TODO: here 
       sendMessage(et_tnum.getText().toString()); 
       et_tnum.setText(""); 
      } 
     } 
    }); 

    btnSend.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (et_pass.getText().toString().equals("")) { 
       Toast.makeText(MainActivity.this, "Please input some texts", Toast.LENGTH_SHORT).show(); 
      } else { 
       //TODO: here 
       sendMessage(et_pass.getText().toString()); 
       et_pass.setText(""); 
      } 
     } 
    }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case REQUEST_ENABLE_BLUETOOTH: 
      if (resultCode == Activity.RESULT_OK) { 
       chatController = new ChatController(this, handler); 
      } else { 
       Toast.makeText(this, "Bluetooth still disabled, turn off application!", Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
    } 
} 

private void sendMessage(String message) { 
    if (chatController.getState() != ChatController.STATE_CONNECTED) { 
     Toast.makeText(this, "Connection was lost!", Toast.LENGTH_SHORT).show(); 
     return; 
    } 

    if (message.length() > 0) { 
     byte[] send = message.getBytes(); 
     chatController.write(send); 
    } 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    if (!bluetoothAdapter.isEnabled()) { 
     Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH); 
    } else { 
     chatController = new ChatController(this, handler); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    if (chatController != null) { 
     if (chatController.getState() == ChatController.STATE_NONE) { 
      chatController.start(); 
     } 
    } 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (chatController != null) 
     chatController.stop(); 
} 

private final BroadcastReceiver discoveryFinishReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
       discoveredDevicesAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      if (discoveredDevicesAdapter.getCount() == 0) { 
       discoveredDevicesAdapter.add(getString(R.string.none_found)); 
      } 
     } 
    } 
}; 

}

Activity_layout.xml

<LinearLayout 
    android:layout_width="348dp" 
    android:layout_height="80dp" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/status" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Not Connected" /> 

    <Button 
     android:id="@+id/btn_connect" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="Connect" /> 
</LinearLayout> 


<ListView 
    android:id="@+id/list" 
    android:layout_width="332dp" 
    android:layout_height="164dp" /> 

<EditText 
    android:id="@+id/et_tnum" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="Transaction Number" 
    android:inputType="textEmailAddress|textPersonName" /> 

<EditText 
    android:id="@+id/et_pass" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="Password" 
    android:inputType="textPassword" /> 

<Button 
    android:id="@+id/btn_send" 
    android:layout_width="213dp" 
    android:layout_height="wrap_content" 
    android:text="Send" /> 

回答

1

将您的密码和手机号码追加到一个字符串中,然后通过BT发送。

+0

这里即时通讯使用两个不同的编辑文本,所以我没有得到任何想法如何append.im newbie.pls看到我的应用程序模型..帮我 –

+1

字符串passwordNMobile =密码+“,”+ mobileNumber; 另一边,String [] params = String.split(passwordNMobile,“,”); 现在PARAMS [0]包含密码而params [1]包含移动电话号码 –

+1

btnSend.setOnClickListener(新View.OnClickListener(){ @Override 公共无效的onClick(查看视图){ 字符串数= et_tnum.gettext .toString(); String password = et_pass.getText()。toString(); if(et_pass.getText()。toString()。equals(“”)){ToS.makeText(MainActivity.this,“Please输入一些文本“,Toast.LENGTH_SHORT).show(); } else { // TODO:here String numberpass = password +”,“+ number; sendMessage(numberpass); et_pass.setText(“”); } } }); } – Jordon

相关问题