首页 > 代码库 > andriod 蓝牙开发之蓝牙搜索

andriod 蓝牙开发之蓝牙搜索

蓝牙的搜索是通过广播来实现的。关键代码如下:

1、用于发现蓝牙的广播接受者

/**
	 * 接收器
	 * 当搜索蓝牙设备完成时调用
	 */
	private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			
			BluetoothDevice device = intent
					.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
			//将结果添加到列表中 
			_devices.add(device);
			DeviceInfo info=new DeviceInfo();
			info.setmDeviceName(device.getName());
			info.setmDeviceMacAddr(device.getAddress());
			infos.add(info);
			info=null;
			// 显示列表 
			showDevices();
		}
	};

private BroadcastReceiver _discoveryReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			// 卸载注册的接收器 
			unregisterReceiver(_foundReceiver);
			unregisterReceiver(this);
			_discoveryFinished = true;
		}
	};


2、获得蓝牙适配器

_bluetooth=BluetoothAdapter.getDefaultAdapter();
		//判断设备是否支持蓝牙
		if(_bluetooth==null){
			Toast.makeText(getApplicationContext(),"当前设备不支持蓝牙", 300).show();
			return;
		}
		//判断蓝牙是否打开
		if(!_bluetooth.isEnabled()){
			//请求开启蓝牙
			Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			 // 设置蓝牙可见性,最多300秒      
		   // intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
		  
		    
			startActivity(intent);

3、注册接收器

//注册接收器 
		IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		registerReceiver(_discoveryReceiver, discoveryFilter);
		IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(_foundReceiver, foundFilter);
		// 显示一个对话框,正在搜索蓝牙设备 
		SamplesUtils.indeterminate(DiscoveryActivity.this, _handler, "蓝牙设备搜索中...", _discoveryWorkder, new OnDismissListener() {
			public void onDismiss(DialogInterface dialog)
			{

				for (; _bluetooth.isDiscovering();)
				{

					_bluetooth.cancelDiscovery();
				}

				_discoveryFinished = true;
			}
		}, false);


	if (list.indexOf(s) == -1) {// 防止重复
				list.add(s);


工具类如下:

class SamplesUtils
{
	public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener)
	{
		try
		{

			indeterminateInternal(context, handler, message, runnable, dismissListener, true);
		}
		catch (Exception e)
		{

			; // nop.
		}
	}


	public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener,
		boolean cancelable)
	{

		try
		{

			indeterminateInternal(context, handler, message, runnable, dismissListener, cancelable);
		}
		catch (Exception e)
		{

			; // nop.
		}
	}

	private static ProgressDialog createProgressDialog(Context context, String message)
	{

		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setIndeterminate(false);
		dialog.setMessage(message);

		return dialog;
	}


	private static void indeterminateInternal(Context context, final Handler handler, String message, final Runnable runnable,
		OnDismissListener dismissListener, boolean cancelable)
	{

		final ProgressDialog dialog = createProgressDialog(context, message);
		dialog.setCancelable(cancelable);

		if (dismissListener != null)
		{

			dialog.setOnDismissListener(dismissListener);
		}

		dialog.show();

		new Thread() {

			@Override
			public void run()
			{
				runnable.run();

				handler.post(new Runnable() {

					public void run()
					{

						try
						{

							dialog.dismiss();
						}
						catch (Exception e)
						{

							; // nop.
						}

					}
				});
			};
		}.start();
	}
}



andriod 蓝牙开发之蓝牙搜索