Android 蓝牙搜索

     最近学习了蓝牙的搜索,不过在调试的时候发现模拟器上要报错,后来想了一下,模拟器上没有蓝牙的模块,根本不能运行啊。所以像蓝牙这种需要硬件支持的项目,还是需要真机调试的。

    其他的不多说了吧,下面就直接上代码吧:

  下面呢就是蓝牙搜索的全部代码,里面包含的有蓝牙的打开,关闭以及搜索附近的蓝牙:

package cn.chenwei.android.app;

import java.util.ArrayList;

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.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SearchBluetoothActivity extends Activity {
	/** Called when the activity is first created. */

	BluetoothAdapter bluetoothAdapter = null;
	private ListView listview;
	ArrayAdapter<String> adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 得到listview
		listview = (ListView) findViewById(R.id.list);

		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();// 获取蓝牙适配器

		boolean flag = bluetoothAdapter.isEnabled();
		if (flag) {
			Toast.makeText(this, "蓝牙已经打开", Toast.LENGTH_SHORT).show();
		} else {
			bluetoothAdapter.enable();// 打开蓝牙设备
			Toast.makeText(this, "正在打开蓝牙", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		menu.add("搜索附近蓝牙设备");

		return super.onCreateOptionsMenu(menu);
	}

	boolean flag = true;// 线程停止的状态

	String[] strs = null;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "正在搜索蓝牙", Toast.LENGTH_SHORT).show();

		// 动态注册
		IntentFilter filter = new IntentFilter();
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
		filter.addAction(BluetoothDevice.ACTION_FOUND);
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

		registerReceiver(new MyBroadcastReceiver(), filter);

		new Thread(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub

				while (true) {
					bluetoothAdapter.startDiscovery();// 开始搜索
					if (!flag) {
						for (BluetoothDevice bd : list) {
							System.out.println("搜索蓝牙" + bd.getName());
 
						}
 
					}

					break;
				}

			}
		}).start();

		return super.onOptionsItemSelected(item);
	}

	// 创建数组用于储存 搜索到的蓝牙
	ArrayList<BluetoothDevice> list = null;

	// 蓝牙的三个状态,打开,发现,结束
	class MyBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			System.out.println("kaishile ");
			String action = intent.getAction();
			if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {// 开始
				Toast.makeText(SearchBluetoothActivity.this, "开始状态",
						Toast.LENGTH_LONG).show();
				list = new ArrayList<BluetoothDevice>();
			} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {// 搜索

				Toast.makeText(SearchBluetoothActivity.this, "发现状态",
						Toast.LENGTH_LONG).show();
				BluetoothDevice bluetoothDevice = (BluetoothDevice) intent
						.getExtras().get(BluetoothDevice.EXTRA_DEVICE);

				list.add(bluetoothDevice);

			} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
					.equals(action)) {//结束
				Toast.makeText(SearchBluetoothActivity.this, "结束状态",
						Toast.LENGTH_LONG).show();

				flag = false;// 修改状态
				unregisterReceiver(this);// 注销广播
			}
		}

	}

}

      不过到这个地方,搜索蓝牙的功能还不能实现,因为缺少了调用蓝牙的权限:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

   以上就是蓝牙搜索的全部代码,希望对朋友们有所帮助,各位朋友有什么问题请多多指教啊

    

原文地址:https://www.cnblogs.com/boyuanmeng/p/3555015.html