找传奇、传世资源到传世资源站!

安卓蓝牙开发_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

eclipse 开发

安卓蓝牙开发_ Android平台开发-第1张

package com.example.btedemo;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	TextView tvOpen, tvSearch, tvServer;

	/* 取得默认的蓝牙适配器 */
	private BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

	private ListView lvDevicesList;

	private DevicesListAdapter deviceAdapter;

	private ArrayList<DevicesInfo> list;
	private BluetoothOpreate bleDevice;
	private TextView txtRead, txtSend;
	//
	private TextView tvBreak, tvSend;
	private Context mContext = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(1);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lvDevicesList = (ListView) findViewById(R.id.device_list);
		tvOpen = (TextView) findViewById(R.id.am_tv_open_id);
		tvSearch = (TextView) findViewById(R.id.am_tv_search_id);
		tvServer = (TextView) findViewById(R.id.am_tv_server_id);
		txtRead = (TextView) findViewById(R.id.am_tv_readtxt_id);
		txtSend = (TextView) findViewById(R.id.am_tv_sendtxt_id);
		tvBreak = (TextView) findViewById(R.id.am_tv_break_id);
		tvSend = (TextView) findViewById(R.id.am_tv_send_id);
		list = new ArrayList<DevicesInfo>();
		deviceAdapter = new DevicesListAdapter(this, list);
		lvDevicesList.setAdapter(deviceAdapter);
		lvDevicesList.setFastScrollEnabled(true);
		lvDevicesList.setOnItemClickListener(this);
		tvOpen.setOnClickListener(this);
		tvSearch.setOnClickListener(this);
		tvServer.setOnClickListener(this);

		tvBreak.setOnClickListener(this);
		tvSend.setOnClickListener(this);

		bleDevice = new BluetoothOpreate(mContext, txtRead, txtSend);
		// Register for broadcasts when a device is discovered
		IntentFilter discoveryFilter = new IntentFilter(
				BluetoothDevice.ACTION_FOUND);
		this.registerReceiver(mReceiver, discoveryFilter);

		// Register for broadcasts when discovery has finished
		IntentFilter foundFilter = new IntentFilter(
				BluetoothDevice.ACTION_FOUND);
		this.registerReceiver(mReceiver, foundFilter);
	}

	public void onStart() {
		super.onStart();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.am_tv_open_id:
			clickOpen();
			break;
		case R.id.am_tv_search_id:
			clickSearch();
			break;
		case R.id.am_tv_server_id:
			clickServer();
			break;
		case R.id.am_tv_send_id:
			bleDevice.sendMessageHandle("$CCICA,0,00*7B\r\n");
			break;
		case R.id.am_tv_break_id:
			bleDevice.close();
			break;
		default:
			break;
		}
	}

	private void clickOpen() {
		// If BT is not on, request that it be enabled.
		if (!mBtAdapter.isEnabled()) {
			Intent enableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, 3);
		}
	}

	private void clickServer() {
		// If BT is not on, request that it be enabled.
		mBtAdapter.cancelDiscovery();
		tvSearch.setText("重新搜索");
		BluetoothOpreate.serviceOrCilent = BluetoothOpreate.ServerOrCilent.SERVICE;
		bleDevice.open();
	}

	private void clickSearch() {
		if (mBtAdapter.isDiscovering()) {
			tvSearch.setText("重新搜索");
		} else {
			list.clear();
			deviceAdapter.notifyDataSetChanged();
			Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
			if (pairedDevices.size() > 0) {
				for (BluetoothDevice device : pairedDevices) {
					list.add(new DevicesInfo(device.getName()  "\n"
							 device.getAddress(), true));
					deviceAdapter.notifyDataSetChanged();
					lvDevicesList.setSelection(list.size() - 1);
				}
			} else {
				list.add(new DevicesInfo("No devices have been paired", true));
				deviceAdapter.notifyDataSetChanged();
				lvDevicesList.setSelection(list.size() - 1);
			}
			/* 开始搜索 */
			tvSearch.setText("停止搜索");
		}
		mBtAdapter.startDiscovery();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		DevicesInfo item = list.get(arg2);
		String info = item.message;
		String address = info.substring(info.length() - 17);
		BluetoothOpreate.BlueToothAddress = address;
		AlertDialog.Builder StopDialog = new AlertDialog.Builder(mContext);// 定义一个弹出框对象
		StopDialog.setTitle("连接");// 标题
		StopDialog.setMessage(item.message);
		StopDialog.setPositiveButton("连接",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						mBtAdapter.cancelDiscovery();
						tvSearch.setText("重新搜索");
						BluetoothOpreate.serviceOrCilent = BluetoothOpreate.ServerOrCilent.CILENT;
						bleDevice.open();
					}
				});
		StopDialog.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						BluetoothOpreate.BlueToothAddress = null;
					}
				});
		StopDialog.show();
	}

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

			// When discovery finds a device
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				// Get the BluetoothDevice object from the Intent
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				// If it's already paired, skip it, because it's been listed
				// already
				if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
					list.add(new DevicesInfo(device.getName()  "\n"
							 device.getAddress(), true));
					deviceAdapter.notifyDataSetChanged();
					lvDevicesList.setSelection(list.size() - 1);
				}
				// When discovery is finished, change the Activity title
			} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
					.equals(action)) {
				setProgressBarIndeterminateVisibility(false);
				if (lvDevicesList.getCount() == 0) {
					list.add(new DevicesInfo("没有发现蓝牙设备", false));
					deviceAdapter.notifyDataSetChanged();
					lvDevicesList.setSelection(list.size() - 1);
				}
				tvSearch.setText("重新搜索");
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();

		// Make sure we're not doing discovery anymore
		if (mBtAdapter != null) {
			mBtAdapter.cancelDiscovery();
		}
		// Unregister broadcast listeners
		this.unregisterReceiver(mReceiver);
	}
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复