蓝牙的开启以及搜索

        蓝牙在现在看来虽然不是很常用,但是,对于有时候还是传一下东西,以及,我在美剧中看到在蓝牙方面,我们程序员还是有很多操作空间的,切入窃入到别人的蓝牙里面然后得到一些重要的情报还是很有用的哦!

   所以,现在我学了如何操作蓝牙的第一步,所以,我还是想深入到里面去:

  1 package cn.android.app;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import android.app.Activity;
  9 import android.bluetooth.BluetoothAdapter;
 10 import android.bluetooth.BluetoothDevice;
 11 import android.content.BroadcastReceiver;
 12 import android.content.Context;
 13 import android.content.Intent;
 14 import android.content.IntentFilter;
 15 import android.graphics.SweepGradient;
 16 import android.os.Bundle;
 17 import android.view.View;
 18 import android.widget.ListView;
 19 import android.widget.SimpleAdapter;
 20 import android.widget.Toast;
 21 
 22 public class BlueToothActivity extends Activity {
 23     /**
 24      * 2-17 今天我们学的时蓝牙搜索 这里我们学习如何打开蓝牙,如何关闭以及搜索当前存在的蓝牙
 25      * 
 26      * 蓝牙这个东西呢,必须要在设配版本在2.0以及以上的才能运行起来
 27      * 
 28      * 打开蓝牙也有两种方式,一种是自己通过蓝牙适配器来判断 当前蓝牙的打开状态 通过enable()属性
 29      * 
 30      * 
 31      * 还有一种就是通过电脑自己来打开当前的蓝牙系统 但是这里还是要自己手动关闭蓝牙
 32      * 
 33      * 这里还要谨记:在配置文件里面我们必须要设置这两个权限,打开蓝牙权限,还有就是将自己的权限 设置为自己的管理
 34      * 
 35      * 
 36      * 在这之前呢,必须要创建一个适配器,但是,你会发现这个适配器我们无法new一个,那是因为
 37      * 蓝牙适配器是私有的,所以,这里我们只能getDefaultAdapter();
 38      * 
 39      * 
 40      * */
 41     /** Called when the activity is first created. */
 42     @Override
 43     public void onCreate(Bundle savedInstanceState) {
 44         super.onCreate(savedInstanceState);
 45         setContentView(R.layout.main);
 46         // 得到实例
 47         mListView = (ListView) findViewById(R.id.share_bluetooth);
 48         // 得到实例适配器
 49         blueToothAdapter = BluetoothAdapter.getDefaultAdapter();
 50         // 搜索蓝牙的时候才要调用的方法
 51         discoverBlueTooth();
 52         if (blueToothAdapter.isEnabled()) {
 53             System.out.println("蓝牙已经打开");
 54         } else {
 55             blueToothAdapter.enable();
 56         }
 57 
 58     }
 59 
 60     // 创建一个蓝牙适配器
 61     private BluetoothAdapter blueToothAdapter = null;
 62     // 因为这里我们可以自行的启动蓝牙所以,我们可以定义一个boolean值来判断当前蓝牙的状态
 63     Boolean flag = false;
 64 
 65     public void onAction(View v) {
 66 
 67         switch (v.getId()) {
 68         case R.id.open_bluetooth:
 69             // 这是第一种
 70             flag = blueToothAdapter.isEnabled();
 71             if (flag) {
 72                 ToastInfo("蓝牙已经打开");
 73             } else {
 74                 blueToothAdapter.enable();
 75                 ToastInfo("蓝牙正在打开");
 76             }
 77             break;
 78         case R.id.close_bluetooth:
 79             flag = blueToothAdapter.isEnabled();
 80             if (flag) {
 81                 blueToothAdapter.disable();
 82                 ToastInfo("蓝牙正在关闭");
 83             } else {
 84                 ToastInfo("蓝牙已经关闭");
 85             }
 86             break;
 87         case R.id.auto_bluetooth:
 88             // 这是第二种打开蓝牙的方式 调用蓝牙设置界面
 89             Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 90             startActivityForResult(intent, 0X1);
 91 
 92             break;
 93         case R.id.search_bluetooth:
 94             // 搜索当前蓝牙 (广播 线程) 我们必须不要忘记注册广播 和intent
 95             // 动态注册action
 96             IntentFilter filter = new IntentFilter();
 97             filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
 98             filter.addAction(BluetoothDevice.ACTION_FOUND);
 99             filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
100             registerReceiver(RegisterBlueTooth, filter);
101             // 开启一个线程,然后得到蓝牙
102             new Thread(new Runnable() {
103 
104                 public void run() {
105 
106                     blueToothAdapter.startDiscovery();// 开始扫描设备
107 
108                     while (true) {
109 
110                         if (!flag) {
111                             // 显示搜索列表
112                             for (BluetoothDevice bd : blueDevises) {
113                                 // 定义一个Map集合
114                                 Map<String, String> map = new HashMap<String, String>();
115                                 map.put("name", bd.getName());
116                                 map.put("adress", bd.getAddress());
117                                 System.out.println("BlueToothName:"
118                                         + bd.getName() + ",,BlueToothAdress:"
119                                         + bd.getAddress());
120                                 list.add(map);
121                             }
122                             break;
123                         }
124 
125                     }
126 
127                 }
128             }).start();
129             String[] from = { "name", "adress" };
130             int[] to = { R.id.name, R.id.adress };
131 
132             SimpleAdapter adapter = new SimpleAdapter(this, list,
133                     R.layout.list_cell, from, to);
134             mListView.setAdapter(adapter);
135 
136             break;
137         default:
138             break;
139         }
140 
141     }
142 
143     // 注册广播,搜索设备的时候调用
144 
145     private BroadcastReceiver RegisterBlueTooth = new BroadcastReceiver() {
146 
147         @Override
148         public void onReceive(Context context, Intent intent) {
149             // 得到action
150             String action = intent.getAction();
151             if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
152                 // 得到实例
153                 blueDevises = new ArrayList<BluetoothDevice>();
154             } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
155                 // 获取搜索结果的数据
156                 BluetoothDevice bluetoothDevice = (BluetoothDevice) intent
157                         .getExtras().get(BluetoothDevice.EXTRA_DEVICE);
158 
159                 blueDevises.add(bluetoothDevice);
160 
161             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
162                     .equals(action)) {
163 
164                 flag = false;
165                 unregisterReceiver(this);
166             }
167 
168         }
169     };
170     // 定义一个listview
171     private ListView mListView;
172     // 定义一个list
173     private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
174     // 用来存储搜索到的设备
175     private List<BluetoothDevice> blueDevises = null;
176 
177     // 在搜索之前呢,我们必须要打开蓝牙的发现功能
178     public void discoverBlueTooth() {
179 
180         // 打开蓝牙的发现功能
181         Intent discoveryInent = new Intent(
182                 BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
183         startActivityForResult(discoveryInent, 0X2);
184     }
185 
186     // 封装一下弹出的toast
187     public void ToastInfo(String str) {
188         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
189 
190     }
191 }

,这里由于没有截屏的原因,所以,不能给大家看了!   

这里   运行这个程序可不能直接用电脑上的设备哦!因为这里需要硬件的支持,所以,我们需要真机调试,这样的我,我们还需要下载一个驱动器!

一切只是为了充实自己!!stay hungry and stay foolish!!
原文地址:https://www.cnblogs.com/Catherine-Brain/p/3552487.html