实例介绍
【实例简介】eclipse 开发
【实例截图】
【核心代码】
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); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论