蓝牙那些事之基础篇

      好久没有写博客了,这几个月一直在做蓝牙这方面的开发,其实一直就想写了,但是我这个人比较懒,所以到现在才开始写。当初做蓝牙的时候也是一脸茫然,不知道怎么做,不停的找资料,不停的百度、百度,哎,我知道这种方法是最不可取的,因为开发速度很慢,但是没办法,身边没有人做过,只能自己一点一点的去学,像一个无头苍蝇一样,不知道从哪里学起,哎....

 

还是从最基础的讲起吧,虽然百度一搜,一大堆,但是每个人有自己的归纳,写写咯..

先讲BluetoothAdapter这个类

获取系统默认的蓝牙适配器:

bluetoothadapter=BluetoothAdapter.getDefaultAdapter();//默认蓝牙设备

判断设备是否支持蓝牙

 1 if(bluetoothadapter==null){
 2       Utils.getinstance().getStr(context, "不支持蓝牙");
 3 }else{
 4       //判断是否支持蓝牙
 5       if(!bluetoothadapter.isEnabled()){
 6             bluetoothadapter.enable();//直接打开蓝牙
 7        }else{
 8              System.out.println("蓝牙已经打开");
 9        if(Myfinal.btpath!=null&&!Myfinal.btpath.equals("")){
10              System.out.println("自动连接设备"+Myfinal.btpath);
11              getClient(null,Myfinal.btpath,1234);//连接蓝牙设备
12          }
13     }
14 }

这里打开蓝牙有两种方法,一种是有提示用户是否打开蓝牙的,一种是没有提示,直接打开蓝牙,下面这种事有提示框,提示用户打开蓝牙

//判断是否启用蓝牙
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableIntent);

使蓝牙可见(可搜索)

//允许本机的蓝牙被搜索的意图
Intent discoveryintent = new Intent(bluetoothadapter.ACTION_REQUEST_DISCOVERABLE);
//设置时间间隔为300s
discoveryintent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoveryintent);

获取蓝牙已配对的设备,此方法类似一个获取系统本地的缓存,只要是配对过的设备,都会保存在本地

 1 //判断设备是否存在(获取已经配对的蓝牙设备)
 2         Set<BluetoothDevice> pairedDevices = bluetoothadapter.getBondedDevices();
 3         if (pairedDevices.size() > 0) {
 4             for (BluetoothDevice device : pairedDevices) {
 5                 //System.out.println("配对的===============设备名称===>>>"+device.getName()+"\n地址====>>>"+device.getAddress()); 
 6                 MyBlueToothDevice bt=new MyBlueToothDevice(0,device.getName(),device.getAddress(),device.getBondState()+"");        
 7                 btlist.add(bt);
 8             }
 9             if(btlist.size()>0){
10                 handler.sendEmptyMessage(1);
11             }
12             //关闭搜索
13             bluetoothadapter.cancelDiscovery();
14         }    
View Code

这里大家可以自己定义一个对象用于接收到蓝牙的数据,以方便处理数据

 

蓝牙搜索:使用的是BluetoothAdapter下面的一个startDiscovery方法,关闭搜索用cancelDiscovery

注册一个广播

1 //注册广播接收器
2 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
3 filter.addAction(BluetoothDevice.ACTION_FOUND);//发现远程设备 
4 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//本地蓝牙适配器已经开始对远程设备的搜寻过程。 
5 this.registerReceiver(receiver, filter); // 不要忘了之后解除绑定    

广播以及处理有相同名称出现,和取消注册广播

 1 //搜索设备(蓝牙)
 2     private final BroadcastReceiver receiver=new BroadcastReceiver() {
 3         @Override
 4         public void onReceive(Context arg0, Intent in) {
 5             
 6             String action=in.getAction();
 7             //发现设备
 8             if(BluetoothDevice.ACTION_FOUND.equals(action)){
 9                 //从intent中获取设备对象
10                 BluetoothDevice device=in.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
11                 //查找未连接配对的蓝牙
12                 if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
13                     System.out.println("蓝牙的地址====>>>>"+device.getAddress()+"=====蓝牙的状态==="+device.getBondState());
14                     MyBlueToothDevice bt=new MyBlueToothDevice(0,device.getName(),device.getAddress(),device.getBondState()+"");
15                     btlist.add(bt); 
16                 }
17                 short rssi=in.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
18                 System.out.println(device.getName()+"=============这里的设备的RSSI类型是==================="+rssi);
19                 
20                 //将设备名称和地址放入ArrayAdapter,以便在ListView中显示(将集合中相同的元素去除)
21                 for (int i = 0; i < btlist.size()-1; i++) {
22                     for (int j = btlist.size()-1; j > i; j--) {
23                         if(btlist.get(j).getBpath().equals(btlist.get(i).getBpath())){
24                             btlist.remove(j);
25                         }
26                     }
27                 }
28                 System.out.println("已经搜索到的设备的大小是...."+btlist.size());
29                 if(btlist.size()>0){
30                     handler.sendEmptyMessage(1);
31                 }
32             }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){    
33                 System.out.println("搜索完成");
34             }
35         }
36     };
37     
38     //解除注册
39     @Override
40     protected void onDestroy() {
41         super.onDestroy();
42         unregisterReceiver(receiver);
43         unregisterReceiver(sercherReceiver);
44     }
View Code

权限:

1    <!-- 蓝牙使用权限 -->
2     <uses-permission android:name="android.permission.BLUETOOTH"/>
3     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

 

在搜索广播的时候,可以查询到远程设备的蓝牙信号,好像、只能在搜索的时候才能取到蓝牙信号,其他时候取不到。。

好吧,暂时先写到这里,下次写通讯这块....

 

posted @ 2014-06-17 10:15  小小缘  阅读(429)  评论(0编辑  收藏  举报