VoteActivity
package com.example.bluetoothvote;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 发起投票模块
*/
public class VoteActivity extends Activity{
//定义全局变量
private BluetoothAdapter mBluetoothAdapter; //适配器
private EditText groupName ; //组名
private EditText voteIntroduce ; //投票内容简介
//投票选项6个
private EditText edtOpt1;
private EditText edtOpt2;
private EditText edtOpt3;
private EditText edtOpt4;
private EditText edtOpt5;
private EditText edtOpt6;
private Button btnUpdate; //修改按钮
private Button btnVote; //发起投票按钮
boolean isEnable = false; //用于确定EditText是否可以修改
//标志变量
private String flag="vn";//v表示投票使用蓝牙名
//n表示投票未结束
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote); //设置界面
Initial();//初始化各个参数
unEnabledVote();//设置EditText禁用
btnVote.setEnabled(false);//发起投票按钮不可用
btnUpdate.setText("修改");
btnVote.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
setVote();
Toast.makeText(getApplicationContext(), "发起成功: "+ mBluetoothAdapter.getName(),Toast.LENGTH_SHORT).show();
btnVote.setEnabled(false);//设置发起投票按钮不可用
btnUpdate.setText("修改");
isEnable = false;
ensureDiscoverable();//使本机蓝牙在300秒内可被搜索
}
});
btnUpdate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if(!isEnable){
EnabledVote();//设置EditText可用
btnUpdate.setText("禁用");
btnVote.setEnabled(true);
isEnable = true;
}
else {
unEnabledVote();//设置EditText不可用
btnVote.setEnabled(false);
btnUpdate.setText("修改");
isEnable = false;
}
}
});
}
private void Initial() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //适配器
groupName = (EditText)findViewById(R.id.groupName); //组名
voteIntroduce = (EditText)findViewById(R.id.voteIntroduce); //投票内容简介
//投票选项6个
edtOpt1 = (EditText)findViewById(R.id.option1);
edtOpt2 = (EditText)findViewById(R.id.option2);
edtOpt3 = (EditText)findViewById(R.id.option3);
edtOpt4 = (EditText)findViewById(R.id.option4);
edtOpt5 = (EditText)findViewById(R.id.option5);
edtOpt6 = (EditText)findViewById(R.id.option6);
btnUpdate = (Button)findViewById(R.id.buttonUpdate); //修改按钮
btnVote = (Button)findViewById(R.id.buttonVote); //发起投票按钮
}
//发起投票
private void setVote(){
String strVote,strgName,strIntroduce,
strOpt1,strOpt2,strOpt3,strOpt4,
strOpt5,strOpt6;//定义存储相应信息的字符串
strgName = getStrContent(8,groupName); //获取"组名"字符串
strIntroduce = getStrContent(60,voteIntroduce); //获取"投票简介"字符串
//获取6个"选项"字符串
strOpt1 = getStrContent(10,edtOpt1);
strOpt2 = getStrContent(10,edtOpt2);
strOpt3 = getStrContent(10,edtOpt3);
strOpt4 = getStrContent(10,edtOpt4);
strOpt5 = getStrContent(10,edtOpt5);
strOpt6 = getStrContent(10,edtOpt6);
strVote = strgName + strIntroduce +
strOpt1 + strOpt2 + strOpt3 + strOpt4 +
strOpt5 + strOpt6 +flag;
for(int i = 132 ; i < 248 ; i++ ){
strVote += " ";
}
mBluetoothAdapter.setName(strVote);//设置蓝牙名
unEnabledVote();//设置EditText禁用
}
//获取投票的相关信息
private String getStrContent(int size,EditText edt){ //获取相应字符长度的信息
String str="";
if(edt.getText().toString().getBytes().length < size){
str = edt.getText().toString();
for(int i= edt.getText().toString().getBytes().length; i<size; i++)
str += " ";
}
else str = edt.getText().toString().substring(0,size);
return str;
}
//设置EditText禁用
private void unEnabledVote(){
groupName.setEnabled(false);
voteIntroduce.setEnabled(false);
edtOpt1.setEnabled(false);
edtOpt2.setEnabled(false);
edtOpt3.setEnabled(false);
edtOpt4.setEnabled(false);
edtOpt5.setEnabled(false);
edtOpt6.setEnabled(false);
}
//设置EditText可用
private void EnabledVote(){
groupName.setEnabled(true);
voteIntroduce.setEnabled(true);
edtOpt1.setEnabled(true);
edtOpt2.setEnabled(true);
edtOpt3.setEnabled(true);
edtOpt4.setEnabled(true);
edtOpt5.setEnabled(true);
edtOpt6.setEnabled(true);
}
//使本机蓝牙在300秒内可被搜索
private void ensureDiscoverable(){
//得到BluetoothAdapter对象,本地蓝牙设备
if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
//可见时间最多为300秒
startActivity(discoverableIntent);
}
}
}
//SearchvoteActivity
package com.example.bluetoothvote;
import java.util.ArrayList;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class SearchvoteActivity extends Activity {
private Button btnBack;
private Button btnSearch;
private ListView lvBluetooth;
private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private SimpleAdapter lvAdapter;//适配器,用于记录搜索到的蓝牙信息
private HashMap<String, String> blName= new HashMap<String,String>();
private BluetoothReceiver mReceiver = new BluetoothReceiver();
private BluetoothAdapter mBluetoothAdapter;
private List<String> addressAll = new ArrayList<String>();//存储搜到的蓝牙名
//private String strName;
public class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 获得已经搜索到的蓝牙设备
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName()+" ");
// if(device.getName().substring(128, 129) == "v"){//消除非投票参与者的蓝牙名
if(device.getName().substring(128, 130).equals("v")){//消除非投票参与者的蓝牙名
if(!isExist(device.getName())){
blName.put("groupName", device.getName().substring(0, 8));
blName.put("introduce", device.getName().substring(8, 68));
if(device.getName().substring(130,132).equals("n"))
{
blName.put("status", "投票正在进行");
}
else
{
blName.put("status", "投票已结束");
}
list.add(blName);
addressAll.add(device.getName().toString());
}
}
}
else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
System.out.println("搜索完成!");
}
}
}
//获取相应字符长度的信息
public String getStrContent(int size,String strName,int begin){
String str="";
if(strName.substring(begin).toString().getBytes().length <= size){
str = strName.toString();
for(int i= strName.substring(begin).toString().getBytes().length; i<size; i++)
str += " ";
}
else str = strName.substring(begin).toString().substring(0,size);
return str;
}
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchvote);
Initial();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//注册用以接收到已搜索到的蓝牙设备的receiver
IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, mFilter);
// 注册搜索完时的receiver
mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, mFilter);
}
//初始化列表
private void InitListView(){
lvBluetooth = (ListView)findViewById(R.id.lvBluetooth);
lvAdapter = new SimpleAdapter(this, list, R.layout.search_adapter,
new String[]{"groupName","introduce","status"}, new int[]{R.id.groupName,R.id.introduce,R.id.status});//定义适配器
lvBluetooth.setAdapter(lvAdapter);//用适配器给listView添加数据项
lvBluetooth.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Group group = adapter.getItem(arg2);
Intent intent = new Intent(SearchvoteActivity.this,ParticipateActivity.class);
Bundle bundle = new Bundle();
for (String in : addressAll) {
//TODO-蓝牙名传至参与投票页面
//if(getStrContent(8,in.toString(),0).equals(getStrContent(8,group.getName().toString(),0)))
System.out.println("蓝牙名:"+ in);
bundle.putString("blname", in.toString());
//Toast.makeText(SearchvoteActivity.this, (CharSequence)in, Toast.LENGTH_SHORT).show();
break;
}
intent.putExtras(bundle); //传的时空值
startActivity(intent);
}
} );
}
//初始化各组件
private void Initial(){
btnBack = (Button)findViewById(R.id.btnBack);
btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回
mBluetoothAdapter.startDiscovery();
InitListView();
addressAll.clear();
}
} );
btnBack.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
finish();
}
} );
}
//解除注册
@Override
protected void onDestroy() {
super.onDestroy();
//解除注册
unregisterReceiver(mReceiver);
}
//消除重复的蓝牙名
private boolean isExist(String address){
for (String in : addressAll) {
if(in.equals(address)){
return true;
}
}
return false;
}
}
异常不是说的很清楚?