蓝牙(2)用BluetoothAdapter搜索蓝牙设备示例

注意在搜索之前要先打开蓝牙设备

 1 package com.e.search.bluetooth.device;
 2 
 3 import java.util.Set;
 4 
 5 import android.app.Activity;
 6 import android.bluetooth.BluetoothAdapter;
 7 import android.bluetooth.BluetoothDevice;
 8 import android.content.BroadcastReceiver;
 9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.IntentFilter;
12 import android.os.Bundle;
13 import android.view.View;
14 import android.view.Window;
15 import android.widget.TextView;
16 
17 public class Main extends Activity {
18     //搜索蓝牙设备第1步,在manifest.xml中加入权限
19     /*
20      *  <uses-permission android:name="android.permission.BLUETOOTH" />
21      *    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
22      */
23     
24     //搜索蓝牙设备第2步,准备BluetoothAdapter
25     private BluetoothAdapter mBluetoothAdapter;
26     private TextView tvDevices;
27     
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
32         setContentView(R.layout.main);
33 
34         tvDevices = (TextView) findViewById(R.id.tvDevices);
35         //搜索蓝牙设备第3步,获取或创建BluetoothAdapter
36         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
37 
38         //搜索蓝牙设备第4步,得到已经配对的蓝牙设备.
39         Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
40 
41         if (pairedDevices.size() > 0) {
42             //枚举已经配对的设备的名字和地址.
43             for (BluetoothDevice device : pairedDevices) {
44                 tvDevices.append(device.getName() + ":" + device.getAddress());
45             }
46         }
47 
48         //搜索蓝牙设备第5步,用广播接收器得到蓝牙找到设备和搜索完成广播消息.
49         //注册找到蓝牙设备广播接收器,用filter注册
50         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
51         this.registerReceiver(receiver, filter);
52         
53         //注册搜索蓝牙设备已完成广播接收器,用filter注册
54         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
55         this.registerReceiver(receiver, filter);
56 
57     }
58 
59     /*
60      * 开始搜索按钮
61      */
62     public void onClick_Search(View view) {
63         //显示进度条
64         setProgressBarIndeterminateVisibility(true);
65         setTitle("正在搜索...");
66         //搜索蓝牙设备第6步,开始搜索蓝牙设备.
67         if (mBluetoothAdapter.isDiscovering()) {//如果正在搜索,那么先取消.
68             mBluetoothAdapter.cancelDiscovery();
69         }
70         mBluetoothAdapter.startDiscovery();//异步的开始搜索.
71     }
72     //搜索蓝牙设备第7步,处理找到蓝牙设备和搜索完成广播消息.
73     private final BroadcastReceiver receiver = new BroadcastReceiver() {
74 
75         @Override
76         public void onReceive(Context context, Intent intent) {
77             String action = intent.getAction();
78             if (BluetoothDevice.ACTION_FOUND.equals(action)) {//找到蓝牙设备
79                 
80                 //搜索蓝牙设备第8步,搜到蓝牙设备.
81                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
82                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
83                 //搜索蓝牙设备第9步,把设备添加到已找到列表中,这里只是显示它的信息.
84                     tvDevices.append(device.getName() + ":"+ device.getAddress() + "\n");
85                 }
86             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.//搜索完成
87                     equals(action)) {
88                 //搜索蓝牙设备第10步,搜索完毕.
89                 setProgressBarVisibility(false);
90                 setTitle("已搜素完成");
91             }
92         }
93     };
94 }

 

posted @ 2015-09-06 17:22  f9q  阅读(659)  评论(0编辑  收藏  举报