Android蓝牙设备的检测
来源:互联网 发布:淘宝网皮书包 编辑:程序博客网 时间:2024/06/05 15:13
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<BluetoothDevice> 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): onServiceConnected10-27 17:02:52.753 I/System.out( 8375): connected name = Bluetooth Keyboard至此,成功检测到了蓝牙键盘。
- Android蓝牙设备的检测
- android之蓝牙通信(一)(检测蓝牙、开启蓝牙、扫描附近蓝牙设备)
- Android设备的蓝牙通信
- Android移动开发-蓝牙(BlueTooth)设备检测连接的实现
- Android Tips: 蓝牙和GPS的检测
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- android蓝牙开发 蓝牙设备的查找和连接
- Android 扫描附近的蓝牙设备并连接蓝牙音响
- android蓝牙开发:无法发现蓝牙设备的问题
- Android中蓝牙设备按键的监听
- Android各种蓝牙设备的UUID
- Mezzanine入门(一)
- synchronized详解。
- 搭建期间未解决问题
- 批处理bat schtasks 启动远程应用
- Android抽奖(大转盘,随机)
- Android蓝牙设备的检测
- android scroller overscroller用法
- 更改归档日志路径
- glibc-2.15 : fopen 源码
- 目标检测“DPMs are CNNs”
- PASSION之Linux命令详解
- 小巧而又强大的翻译神器--QTranslate
- Java嵌套类
- 基于HashHeap的LFU实现