第一步 声明蓝牙权限

一、要在应用程序中使用蓝牙功能,必须声明蓝牙的使用权限:BLUETOOTH和BLUETOOTH_ADMIN

1、BLUETOOTH

必须请求BLUETOOTH权限才能够使用蓝牙通信,进而请求连接、接收连接、传输数据

2、BLUETOOTH_ADMIN

必须请求BLUETOOTH_ADMIN才能够初始化device discovery或者管理蓝牙设置(Bluetooth settings)。

大多数应用程序必须具有这个权限才能够发现本地蓝牙设备,这个权限保护的其他能力(除了发现本地设备)不应该被使用,除非你的应用程序是在用户请求的时候能够修改蓝牙设置的管理者。


二、注意:如果你想要使用BLUETOOTH_ADMIN权限,那么你首先必须有BLUETOOTH权限。


三、在AndroidManifest文件中声明程序的蓝牙权限:

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

第二步 获取本地的蓝牙设备-BluetoothAdapter

任何蓝牙activity都需要BluetoothAdapter类。使用静态方法getDefaultAdapter()获得一个BluetoothAdapter的实例,这代表了设备本身的蓝牙适配器(the Bluetooth radio)。整个系统只有一个蓝牙适配器,你的程序可以通过获取到BluetoothAdapter实例与之交互。如果getDefaultAdapter()方法返回null则说明你的设备不支持蓝牙。

实例代码如下:

BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();//判断BluetoothAdapter对象是否为空,如果为空,则表明本机没有蓝牙设备if(adapter !=null){System.out.println("本机拥有蓝牙设备");            }            else{                System.out.println("没有蓝牙设备");            }
第三步 调用isEnabled()方法,打开蓝牙

接下来,你必须确保用户启动了蓝牙。调用isEnabled()方法来检查当前蓝牙是否启动。如果该方法返回false,那么说明蓝牙没有启动。这时需要使用“ACTION_REQUEST_ENABLE”action Intent作为参数,调用startActivityForResult()方法来请求启动蓝牙。这将通过系统设备来发出启动蓝牙的请求(不会停止你的程序)。

执行如上的代码将会弹出一个对话框,请求启动蓝牙的用户权限。如果用户点击“Yes”按钮,那么系统将开始启动蓝牙,启动蓝牙(有可能失败)之后你的程序将重新获得焦点。

实例代码如下:

<span style="font-size:14px;">   //调用isEnabled()方法,判断当前蓝牙设备是否可用if(!adapter.isEnabled()){//创建一个intent对象,该对象用于启动一个Activity,提示用户开启蓝牙设备Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivity(intent);        //不做提示,强行打开        // mAdapter.enable();}</span>
第四步 查询手机上已配对的蓝牙设备
在执行device discovery之前,最好在已配对的设备列表中查看所要发现的设备是否已经存在。通过调用getBondedDevices()函数可以获得代表已经配对的设备的BluetoothDevice集合。例如,你可以查询所有已经配对的设备,然后通过一个ArrayAdapter添加和显示每个设备的名字给用户:

//得到所有已配对的蓝牙适配器对象Set<BluetoothDevice> devices = adapter.getBondedDevices();if(devices.size()>0){for(Iterator iterator = devices.iterator();iterator.hasNext();){BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();//得到远程已配对蓝牙设备的mac地址System.out.println(bluetoothDevice.getAddress());}
为了建立一个连接,需要才能够BluetoothDevice对象中获取的是MAC地址。在这个例子中,MAC地址作为显示给用户的ArrayAdapter的一部分存储。只要有需要,可以把MAC地址提取出来。

第五步 搜索可用的蓝牙设备

1)刚才说过了mAdapter.startDiscovery()

是第一步,可以你会发现没有返回的蓝牙设备,怎么知道查找到了呢?向下看,不要急

2)定义BroadcastReceiver,关于BroadcastReceiver不多讲了,不是今天的讨论内容,代码如下


BroadcastReceiver mReceiver = new BroadcastReceiver() {<span style="white-space:pre"></span>public void onReceive(Context context, Intent intent) {<span style="white-space:pre"></span>String action = intent.getAction();                   //找到设备<span style="white-space:pre"></span>if (BluetoothDevice.ACTION_FOUND.equals(action)) {<span style="white-space:pre"></span>BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);<span style="white-space:pre"></span>if (device.getBondState() != BluetoothDevice.BOND_BONDED) {<span style="white-space:pre"></span>Log.v(TAG, "find device:" + device.getName()+ device.getAddress());<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}         //搜索完成        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {<span style="white-space:pre"></span>setTitle("搜索完成");<span style="white-space:pre"></span>if (mNewDevicesAdapter.getCount() == 0) {<span style="white-space:pre"></span>Log.v(TAG,"find over");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//执行更新列表的代码<span style="white-space:pre"></span>}<span style="white-space:pre"></span>};

这样,没当查找到新设备或是搜索完成,相应的操作都在上段代码的两个if里执行了,不过前提是你要先注册

BroadcastReceiver,具体代码如下

<span style="font-size:14px;"><span style="white-space:pre"></span>IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);<span style="white-space:pre"></span>registerReceiver(mReceiver, filter);<span style="white-space:pre"></span>filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);<span style="white-space:pre"></span>registerReceiver(mReceiver, filter);</span>
(这段代码,一般写在onCreate()里..)




更多相关文章

  1. Android(安卓)中的显示单位
  2. Adobe AIR for Android开发记录
  3. Android存储设备(U盘,SD卡)状态监测
  4. Android(安卓)USB使用
  5. Android(安卓)“adb forward”端口映射
  6. 有空待研究的几篇不错的surfaceflinger文章
  7. Android(安卓)6.0蓝牙读写和扫描权限问题
  8. 为Android应用程序读取/dev下设备而提权
  9. android 蓝牙模块相关的一些知识了解

随机推荐

  1. 享受Android应用程序的Java技术盛宴
  2. Android中xml的部分属性
  3. 如何一个android工程作为另外一个android
  4. Android Studio常用快捷键、Android Stud
  5. 多款Android播放器源码集锦(附开发教程)
  6. 对android夜间模式实现的探讨
  7. Android创建和使用数据库详细指南
  8. 创建一个ArcGIS for Android 新项目并显
  9. android:stretchColumns=”0″
  10. Android高手进阶教程(二)之----Android(