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

时间:2022-04-10 15:22:52

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

在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<String> PairedMaclist;
private TextView textView1;
int flag;

@Override
protected 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<String>();

}

@Override
protected 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<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
if(devices.size() > 0){
for(Iterator<BluetoothDevice> 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{

@Override
public 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{

@Override
public void onClick(View arg0) {
mArrayAdapter.clear();

bluetoothAdapter.startDiscovery();
Toast.makeText(BluetoothActivity.this, "开始扫描",0).show();
}

}

class ListViewListener implements OnItemClickListener{

@Override
public 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);
}

}
}
显示和扫描蓝牙布局文件
<pre name="code" class="html"><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="20dp"    android:orientation="vertical" >                <Button         android:id="@+id/scanButton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="20dp"        android:text="扫描蓝牙设备"/>    <TextView         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="20dp"        android:textSize="20sp"        android:text="搜索到以下设备:"/>    <ListView         android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        ></ListView>     </LinearLayout><img src="http://img.blog.csdn.net/20150409144408261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZlbmdkZWp1YW5saWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />