码迷,mamicode.com
首页 > 移动开发 > 详细

Android 蓝牙设备的查找和连接

时间:2014-12-11 19:05:38      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   io   ar   os   使用   sp   for   

1.权限
使用蓝牙设备需要先在Manifest中开放权限,位置如下。

[html] view plaincopy
 
  1. <manifest ...>        
  2.     <application ...>  
  3.            ...  
  4.     </application>  
  5.   
  6.     // 使用蓝牙设备的权限  
  7.     <uses-permission android:name="android.permission.BLUETOOTH" />  
  8.     // 管理蓝牙设备的权限  
  9.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
  10. </manifest>  


2.打开蓝牙
获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。

[java] view plaincopy
 
  1. // 检查设备是否支持蓝牙  
  2. adapter = BluetoothAdapter.getDefaultAdapter();  
  3. if (adapter == null)  
  4. {  
  5.     // 设备不支持蓝牙  
  6. }  
  7. // 打开蓝牙  
  8. if (!adapter.isEnabled())  
  9. {  
  10.     Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  11.     // 设置蓝牙可见性,最多300秒  
  12.     intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  
  13.     context.startActivity(intent);  
  14. }  


3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice)
首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。

[java] view plaincopy
 
  1. BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  
  2. Set<BluetoothDevice> devices = adapter.getBondedDevices();  
  3. for(int i=0; i<devices.size(); i++)  
  4. {  
  5.     BluetoothDevice device = (BluetoothDevice) devices.iterator().next();  
  6.     System.out.println(device.getName());  
  7. }  


4.搜索周围的蓝牙设备
适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。

[java] view plaincopy
 
  1. // 设置广播信息过滤  
  2. IntentFilter intentFilter = new IntentFilter();  
  3. intentFilter.addAction(BluetoothDevice.ACTION_FOUND);  
  4. intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);  
  5. intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);  
  6. intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);  
  7. // 注册广播接收器,接收并处理搜索结果  
  8. context.registerReceiver(receiver, intentFilter);  
  9. // 寻找蓝牙设备,android会将查找到的设备以广播形式发出去  
  10. adapter.startDiscovery();  


自定义广播类

[java] view plaincopy
 
  1. private BroadcastReceiver receiver = new BroadcastReceiver() {  
  2.    @Override  
  3.    public void onReceive(Context context, Intent intent) {  
  4.         String action = intent.getAction();  
  5.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  6.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  7.             System.out.println(device.getName());  
  8.         }  
  9.    }  
  10. }  


5.蓝牙设备的配对和状态监视

[java] view plaincopy
 
  1. private BroadcastReceiver receiver = new BroadcastReceiver() {  
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.         String action = intent.getAction();  
  5.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  6.             // 获取查找到的蓝牙设备  
  7.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  8.             System.out.println(device.getName());  
  9.             // 如果查找到的设备符合要连接的设备,处理  
  10.             if (device.getName().equalsIgnoreCase(name)) {  
  11.                 // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索  
  12.                 adapter.cancelDiscovery();  
  13.                 // 获取蓝牙设备的连接状态  
  14.                 connectState = device.getBondState();  
  15.                 switch (connectState) {  
  16.                     // 未配对  
  17.                     case BluetoothDevice.BOND_NONE:  
  18.                         // 配对  
  19.                         try {  
  20.                             Method createBondMethod = BluetoothDevice.class.getMethod("createBond");  
  21.                             createBondMethod.invoke(device);  
  22.                         } catch (Exception e) {   
  23.                             e.printStackTrace();  
  24.                         }  
  25.                         break;  
  26.                     // 已配对  
  27.                     case BluetoothDevice.BOND_BONDED:  
  28.                         try {  
  29.                             // 连接  
  30.                             connect(device);  
  31.                         } catch (IOException e) {  
  32.                             e.printStackTrace();  
  33.                         }  
  34.                         break;  
  35.                 }  
  36.             }  
  37.        } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {  
  38.             // 状态改变的广播  
  39.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  40.             if (device.getName().equalsIgnoreCase(name)) {   
  41.                 connectState = device.getBondState();  
  42.                 switch (connectState) {  
  43.                     case BluetoothDevice.BOND_NONE:  
  44.                         break;  
  45.                     case BluetoothDevice.BOND_BONDING:  
  46.                         break;  
  47.                     case BluetoothDevice.BOND_BONDED:  
  48.                         try {  
  49.                             // 连接  
  50.                             connect(device);  
  51.                         } catch (IOException e) {  
  52.                             e.printStackTrace();  
  53.                         }  
  54.                         break;  
  55.                 }  
  56.             }  
  57.         }  
  58.     }  
  59. }  


6.蓝牙设备的连接

[java] view plaincopy
 
    1. private void connect(BluetoothDevice device) throws IOException {  
    2.     // 固定的UUID  
    3.     final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";  
    4.     UUID uuid = UUID.fromString(SPP_UUID);  
    5.     BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);  
    6.     socket.connect();  
    7. }  

Android 蓝牙设备的查找和连接

标签:android   blog   http   io   ar   os   使用   sp   for   

原文地址:http://www.cnblogs.com/pinksnow520/p/4158234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!