android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备

蓝牙是一种重要的短距离无线通信协议,广泛应用于各种设备(手机,医疗,汽车等)。蓝牙是比较常用的无线通信设备,早研究成为手机的标配。现在的安卓手机基本上都有蓝牙,所有通过蓝牙对数据有很好的硬件基础

在Android中,与蓝牙有关的类和接口在android.bluetooth包中。其中BluetoothAdapter是蓝牙中的核心类,代表本地的蓝牙适配器设备。BluetoothAdapter类让用户能执行基本的蓝牙任务。例如: 初始化设备的搜索,查询可匹配的设备集,使用一个已知的MAC地址来初始化一个BluetoothDevice类,创建一个 BluetoothServerSocket类以监听其它设备对本机的连接请求等。

打开和扫描蓝牙,并将扫面到的蓝牙设备显示在listview上

BluetoothActivity

package cn.edu.cqu.bluetooth;import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import com.example.project.R;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.os.Bundle;
import android.view.View;
import android.view.Window;
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.TextView;
import android.widget.Toast;public class BluetoothActivity extends Activity{private Button button2;private ListView list;private ArrayAdapter mArrayAdapter;  private BluetoothReceiver bluetoothReceiver;private BluetoothAdapter bluetoothAdapter;private BluetoothDevice device;private ArrayList PairedMaclist;private TextView textView1;int flag;@Overrideprotected void onCreate(Bundle savedInstanceState) {this.requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.bluetooth);list = (ListView)findViewById(R.id.listView);textView1 = (TextView)findViewById(R.id.textView);//创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND,查找蓝牙IntentFilter intentFileter = new IntentFilter(BluetoothDevice.ACTION_FOUND);bluetoothReceiver = new BluetoothReceiver();//注册广播接收器registerReceiver(bluetoothReceiver, intentFileter);bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();button2 = (Button)findViewById(R.id.scanButton);button2.setOnClickListener(new ScanButtonListener());list.setOnItemClickListener(new ListViewListener());openBluetooth();mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);PairedMaclist=new ArrayList();}@Overrideprotected void onDestroy() {unregisterReceiver(bluetoothReceiver);super.onDestroy();}private void openBluetooth() {//bluetoothAdapter.enable();if(bluetoothAdapter != null){if(!bluetoothAdapter.isEnabled()){Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivity(intent);}//得到所有已经被对的蓝牙适配器对象Set devices = bluetoothAdapter.getBondedDevices();if(devices.size() > 0){for(Iterator iterator = devices.iterator();iterator.hasNext();){BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();//得到远程蓝牙设备的地址System.out.println(bluetoothDevice.getAddress());}}}else {//System.out.println("没有蓝牙设备");Toast.makeText(BluetoothActivity.this, "没有蓝牙设备",0).show();}}class BluetoothReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(BluetoothDevice.ACTION_FOUND.equals(action)){//得到intent里面的信息device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Toast.makeText(BluetoothActivity.this, "发现蓝牙设备",0).show();//System.out.println(device.getAddress());mArrayAdapter.add(device.getName() + "\n" + device.getAddress());PairedMaclist.add(device.getAddress());list.setAdapter(mArrayAdapter);}}}class ScanButtonListener implements OnClickListener{@Overridepublic void onClick(View arg0) {mArrayAdapter.clear();bluetoothAdapter.startDiscovery();Toast.makeText(BluetoothActivity.this, "开始扫描",0).show();}}class ListViewListener implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView arg0, View view, int position,long id) {String str = PairedMaclist.get(position);//System.out.println("Str-------------" + str);Intent intent = new Intent(BluetoothActivity.this,BluetoothWaveform.class);intent.putExtra("BluetoothMAC",str);startActivity(intent);}}
}
显示和扫描蓝牙布局文件

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部