Android蓝牙搜索设备-刘宇

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuyu973971883/article/details/52491444

Bluetooth是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换。今天,我们一起来探讨在Android设备中如何去操作蓝牙。

效果图:

首先蓝牙有三种打开方式:

第一:手动打开,这个普通用户基本都会

第二:Intent代码打开

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);

这种开启方式开启的时候会弹出一个提示框,提示用户是否开启蓝牙,而且提示框的UI我们不可控制,这样体验感就大大下降了。

第三:代码开启:BluetoothAdapter

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter()
adapter.enable();
adapter.disable();

这种方法没有提示,完全在后台打开,但是需要两个权限说明:

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

——————————————————————————————————————

下面我们来简单做一个Demo,搜索周边的蓝牙设备,值得注意的是,在搜索周边蓝牙设备的时候也是需要上面两个权限的。运行程序需要手动开启蓝牙,代码中没有写自动开启蓝牙功能,如需添加,请自行添加。

首先现将布局布好

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick_Search"
        android:text="搜索蓝牙设备" />
    <TextView
        android:id="@+id/tvDevices"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

随后我们进入代码区域:

MainActivity.java:

package com.oak.learnbluetoothsearch;

import java.util.Set;
import com.oak.learnbluetoothsearch.R;
import android.os.Bundle;
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.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	//首先创建蓝牙适配器
	private BluetoothAdapter bluetoothAdapter;
	//用来显示蓝牙的TextView
	private TextView tvDevices;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取到TextView组件
		tvDevices = (TextView) findViewById(R.id.tvDevices);
		//获取到蓝牙的默认适配器
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		//获取已绑定的设备,将其存放于set集合
		Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
		//判断set集合里面是否有内容,如果有将其显示在TextView中
		if (devices.size() > 0) {
			//遍历Set集合中的数据
			for (BluetoothDevice bluetoothDevice : devices) {
				tvDevices.append(bluetoothDevice.getName() + ":"
						+ bluetoothDevice.getAddress());
			}
		}
		//因为蓝牙在搜索到设备和搜索完毕都是通过广播发送的,这里我们需要注册广播接收器
		IntentFilter intentFilter = new IntentFilter(
				BluetoothDevice.ACTION_FOUND);
		registerReceiver(receiver, intentFilter);
		intentFilter = new IntentFilter(
				BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		registerReceiver(receiver, intentFilter);
	}
	//点击按钮进行扫描,再次点击终止
	public void onClick_Search(View view) {
		setTitle("正在扫描...");
		if (bluetoothAdapter.isDiscovering()) {
			bluetoothAdapter.cancelDiscovery();
		}
		bluetoothAdapter.startDiscovery();
	}
	//注册广播接收者
	private final BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context arg0, Intent intent) {
			String action = intent.getAction();
			//判断这个广播是否是蓝牙搜索到设备的广播
			if(action.equals(BluetoothDevice.ACTION_FOUND)){
				//获取到传递过来的设备信息
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				//判断是否是绑定的,因为之前绑定的已经添加到TextView中去了
				if(device.getBondState() != BluetoothDevice.BOND_BONDED){
					tvDevices.append(device.getName()+":"+device.getAddress()+"\n");
				}
			//判断是否为搜索完毕的广播
			}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
				setTitle("搜索完成");
			}
			
		}
	};
}

到这里简单的蓝牙设备搜索就完成了,更多蓝牙操作请及时关注,下面还有蓝牙数据传输等教程。

By:Brycen Liu

猜你喜欢

转载自blog.csdn.net/liuyu973971883/article/details/52491444
今日推荐