废话不多说直接上效果图
:与蓝牙相关的最重要的两个API
1:BuletoothAdapter
这个类的对象代表了本地的蓝牙适配器,相当于蓝牙工作流程图中的手机里的蓝牙适配器,也就是说比如这个应用程序是运行在手机上,那么手机上的蓝牙适配器就是本地蓝牙适配器。
2:BuletoothDevice
这个类的对象代表了远程的蓝牙设备,相当于蓝牙工作流程图中的计算机里的蓝牙适配器,也就是说比如这个应用程序是运行在手机上,那么BuletoothDevice代表了你要连接的远程的那个设备上面的蓝牙适配器。
硬件准备
今天这个示例必须运行在具有安卓2.0SDK以上的手机上面,不能运行在模拟器上面,因为现在的模拟器是不能模拟蓝牙的,所以必须有个安卓的手机,另外要有台具有蓝牙适配器的电脑。手机和电脑来进行配对,只能通过手动来进行,不可能通过代码是实现配对,因为安全性的问题不能通过应用程序自动的来进行配对,一旦配对成功就可以进行文件的传输了。如何配对在这里就不讲解相信大家都了解!
创建BlueToothActivity类
package com.weixin;
import java.util.List;
import java.util.Set;
import android.annotation.SuppressLint;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BlueToothActivity extends Activity {
private Button btn_search;
private TextView tv_devices;
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_CODE=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
initView();
}
private void initView() {
// TODO Auto-generated method stub
btn_search = (Button) findViewById(R.id.btn_search);
tv_devices = (TextView) findViewById(R.id.tv_devices);
}
@SuppressLint("NewApi")
public void Search(View v) {
// 获取本地蓝牙
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 判断手机是否支持蓝牙
if (mBluetoothAdapter == null) {
Toast.makeText(this, "此设备不支持蓝牙", Toast.LENGTH_SHORT).show();
this.finish();
}
// 判断是否打开蓝牙
if (!mBluetoothAdapter.isEnabled()) {
// 弹出对话框提示用户是后打开
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_CODE);
//不提示强行关闭
//mBluetoothAdapter.disable();
// 不做提示,强行打开
// mBluetoothAdapter.enable();
}
// 获取已经配对的设备列表
Set<BluetoothDevice> pList=mBluetoothAdapter.getBondedDevices();
if(pList!=null&&pList.size()>0){
for (BluetoothDevice bluetoothDevice : pList) {
tv_devices.append(bluetoothDevice.getName()+":"+bluetoothDevice.getAddress()+":"+bluetoothDevice.getBondState());
Log.i("已配对设备", tv_devices.getText().toString());
}
}
// // 判断是否有配对过的设备
// if (pairedDevices.size() > 0) {
// for (BluetoothDevice device : pairedDevices) {
// // 遍历到列表中
// tv_devices.append(device.getName() + ":" + device.getAddress());
// Log.i("已配对设备", tv_devices.getText().toString());
// }
// }
/**
* 异步搜索蓝牙设备——广播接收
*/
// 找到设备的广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// 注册广播
registerReceiver(receiver, filter);
// 搜索完成的广播
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注册广播
registerReceiver(receiver, filter);
setProgressBarIndeterminateVisibility(true);
setTitle("正在搜索附近的蓝牙设备...");
// 判断是否在搜索,如果在搜索,就取消搜索
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 开始搜索
mBluetoothAdapter.startDiscovery();
}
// 广播接收器
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
// 发现设备的广播
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 从intent中获取设备
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 判断是否配对过
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
// 将搜索到的设备添加到列表
tv_devices.append(device.getName() + ":"
+ device.getAddress() + "\n");
Log.i("tag", tv_devices.getText().toString());
}
// 搜索完成
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
// 关闭进度条
setProgressBarIndeterminateVisibility(true);
//setTheme(R.drawable.gril);
setTitle("搜索结束!");
}
}
};
}
添加两个权限
<!-- 添加蓝牙设备权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
布局文件activity_bluetooth.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="Search"
android:text="搜索蓝牙设备"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示附近的蓝牙设备"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
完工了!