Android蓝牙设备的检测

Android蓝牙设备的检测

        最近,在项目工作中,遇到一个与蓝牙设备相关的问题,需要在特定情况下,检测手机是否有接入其他蓝牙设备。搜了很多资料,都不满足我的功能要求。最后,还是在Android developer官网上找到了解决方案。当时在官网搜索Bluetooth关键字,发现了BluetoothDevice类,通过阅读SDK文档,了解此类的用途,类方法的功能,终于解决了蓝牙设备的检测问题。

1. BluetoothDevice和BluetoothAdapter

        BluetoothDevice代表一个远程的蓝牙设备,可以通过它与设备建立连接,获取相关信息,包括设备名称、设备地址和绑定状态。

        BluetoothAdapter则表示一个本地的蓝牙适配器,可以通过它实现一些基本的蓝牙相关任务:初始化蓝牙检测,查找绑定设备,初始化蓝牙设备对象等。例如,使用BluetoothAdapter.getRemoteDevice(String address)来获取指定硬件地址的BluetoothDevice对象。

2. 检测蓝牙设备

项目遇到的功能要求是,检测当前的蓝牙设备连接情况,下面是简单的测试代码。

package com.dale.bluetoothtest;

import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = (Button) findViewById(R.id.bn);        
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
            	/*
            	 * 获取默认的蓝牙适配器
            	 */
                BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
                /*
                 * 获取与profile关联的profile代理,并实现BluetoothProfile.ServiceListener监听的两个方法
                 * 从而实现设备检测功能
                 */
                ba.getProfileProxy(MainActivity.this, new BluetoothProfile.ServiceListener() {
                    
                    @Override
                    public void onServiceDisconnected(int profile) {
                        System.out.println("onServiceDisconnected");
                    }
                    
                    @Override
                    public void onServiceConnected(int profile, BluetoothProfile proxy) {
                        System.out.println("onServiceConnected");
                        List devices = proxy.getConnectedDevices();
                        for(BluetoothDevice d : devices) {
                            String name = d.getName();
                            System.out.println("connected name = "+name);
                        }
                    }
                }, 4);  //4即 BluetoothProfile.INPUT_DEVICE的值,, 因为 是@hide状态,故此处只能显示写为对应的整型
            }
        });
        
    }

}
        界面很简单,仅一个Button,用于触发检测动作,在此不贴布局代码。

        当点击检测后,首先获取到一个默认蓝牙适配器,然后调用getProfileProxy,并实现第二个接口类型参数。getProfileProxy原型如下:   

        public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)
        参数说明:
  •   listener:代理连接状态监听,包括两个回调,onServiceConnected和onServiceDisconnected
  •   profile:类型,可能为BluetoothProfile.HEALTH, BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.GATT, 或者BluetoothProfile.GATT_SERVER. 还有INPUT_DEVICE, PAN等等,但均为@hide状态,无法调用。

        比较重要的是onServiceConnected(int profile, BluetoothProfile proxy),当代理对象成功连接时,将会回调,并传入类型profile和代理对象。由于项目关注的是输入类型的蓝牙设备(测试时使用的是蓝牙键盘),故第三个参数profile设为BluetoothProfile.INPUT_DEVICE,但是因为它为@hide,故直接写为对应整型值,为4。

        onServiceConnected内部,代理对象调用getConnectedDevices方法,获取当前所有已连接的设备,然后输出设备名,用蓝牙键盘测试结果如下:

10-27 17:02:52.743 I/System.out( 8375): onServiceConnected
10-27 17:02:52.753 I/System.out( 8375): connected name = Bluetooth Keyboard
        至此,成功检测到了蓝牙键盘。

你可能感兴趣的:(Android,蓝牙)