Android下蓝牙简介


      一、Package name: Android.bluetooth.*,主要相关类介绍如下:

    * BluetoothAdapter: 本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成;

      cancelDiscovery() 取消发现

      disable()关闭蓝牙

      enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:

      getAddress()获取本地蓝牙地址

      getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter

      getName()获取本地蓝牙名称

      getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备

      getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)

      isDiscovering()判断当前是否正在查找设备,是返回true

      isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

      listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步

      startDiscovery()开始搜索,这是搜索的第一步


    * BluetoothDevice: 蓝牙设备类,代表了蓝牙通讯过程中的远端设备;

      createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket


    * BluetoothSocket: 蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过inputstream和outputstream与远端程序进行通讯;
    * BluetoothServerSocket: 服务器通讯套接字,与TCP ServerSocket类似;

      close(),关闭

      connect()连接

      getInptuStream()获取输入流

      getOutputStream()获取输出流

      getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备


    * BluetoothClass: 用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法获取代表远端设备属性的BluetoothClass对象。

二、使用蓝牙必须获取的权限:

  1. <uses-permission android:name="android.permission.BLUETOOTH" />  
  2.   
  3. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  

三、建立蓝牙连接:

1.通过BluetoothAdapter.getDefaultAdapter()方法获取BluetoothAdapter对象。

2.判断当前蓝牙是否启动,如果没有启动提示用户手动启动:

  1. if (!mBluetoothAdapter.isEnabled()) {  
  2.     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  3.     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  4. }  

3.在Activity的onActivityResult()方法中,对用户的设定结果进行处理。

4.搜寻远端蓝牙设备

首先获取已配对的远端设备:

  1. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();  


然后通过BluetoothAdapter.startDiscovery()方法启动蓝牙设备的搜寻。这是个异步方法,调用的时候立刻就会返回。为了获得搜寻的结果,必须在用户自己的Activity中注册一个BroadcastReceiver,代码如下:

  1. // Create a BroadcastReceiver for ACTION_FOUND  
  2. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  3.     public void onReceive(Context context, Intent intent) {  
  4.         String action = intent.getAction();  
  5.         // When discovery finds a device  
  6.         if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  7.             // Get the BluetoothDevice object from the Intent  
  8.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  9.             // Add the name and address to an array adapter to show in a ListView  
  10.             mArrayAdapter.add(device.getName() + "\n" + device.getAddress());  
  11.         }  
  12.     }  
  13. };  
  14. // Register the BroadcastReceiver  
  15. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  16. registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy  

5.设置本地设备可以被发现:只有将本地设备设置为可被发现,远端的蓝牙设备才能够找到并和本地设备建立连接。通过下面的代码发送Intent对象,让用户手动启动可发现设置。

  1. Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  2. discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  
  3. startActivity(discoverableIntent);  
  4. <pre class="html" name="code">   

相关内容