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

Android 蓝牙4.0 BLE

时间:2015-12-09 19:41:04      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用。

BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备。

官方demo:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

官方demo(csdn下载,感谢分享的人吧):http://download.csdn.net/detail/lqw770737185/8116019

由于搜索需要尽量减少功耗,因此在实际使用时需要注意:
1、当找到对应的设备后,立即停止扫描;
2、不要循环搜索设备,为每次搜索设置适合的时间限制。避免设备不在可用范围的时候持续不停扫描,消耗电量。
注意:添加权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
除了蓝牙权限外,如果需要BLE feature则还需要声明uses-feature:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
设置required为true时,则应用只能在支持BLE的Android设备上安装运行;required为false时,Android设备均可正常安装运行,需要在代码运行时判断设备是否支持BLE feature:
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
      //不支持ble
      finish();
}
 
(1)实现 BluetoothAdapter.LeScanCallback接口,BLE设备的搜索结果将通过这个callback返回
 1   // Device scan callback.
 2     private BluetoothAdapter.LeScanCallback mLeScanCallback =
 3             new BluetoothAdapter.LeScanCallback() {
 4 
 6         @Override
 7         public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
 8             
30         }
31 
32     };

(2)关闭BLE设备的搜索

1 mBluetoothAdapter.stopLeScan(mLeScanCallback);

(3)开启BLE设备的搜索

1 mBluetoothAdapter.startLeScan(mLeScanCallback);

*************注意:搜索时,你只能搜索传统蓝牙设备或者BLE设备,两者完全独立,不可同时被搜索。

(4)使用BluetoothGatt 来连接设备

两个设备通过BLE通信,首先需要建立GATT连接。这里我们讲的是Android设备作为client端,连接GATT Server。
连接GATT Server,你需要调用BluetoothDevice的connectGatt()方法。此函数带三个参数:Context、autoConnect(boolean)和BluetoothGattCallback对象。
1 private BluetoothGatt mBluetoothGatt;
 1   final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
 2     if (device == null) {
 3       Log.w(TAG, "Device not found.  Unable to connect.");
 4       return false;
 5     }
 6     mBluetoothGatt = device.connectGatt(this, true, mGattCallback);

(5)使用BluetoothGattCallback 来跟设备进行通信

 1 private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
 2     @Override
 3     public void onConnectionStateChange(BluetoothGatt gatt, 
 4         int status, int newState) {
 5         super.onConnectionStateChange(gatt, status, newState); 9         if (newState == BluetoothProfile.STATE_CONNECTED) {
10             setState(State.CONNECTED);
11             gatt.discoverServices();
12         } else {
13             setState(State.IDLE);
14         }
15     }
16  
17     @Override
18     public void onServicesDiscovered(BluetoothGatt gatt, 
19        int status) {
20         if(status == BluetoothGatt.GATT_SUCCESS) {
21             Log.v(TAG, "onServicesDiscovered: " + status);
22         }
23     }
24 }

连接时会走这个方法onConnectionStateChange,传过来的新状态是连接状态,这时在这个方法中调用一下这句:mBluetoothGatt.discoverServices(),

mBluetoothGatt是连接完成时的对象,还记得吧,调用这句后,会走回调函数的onServicesDiscovered方法。在这个方法中去获取设备的一些服务,蓝牙通道,然后通过这些通道去发送数据给外设。

在蓝牙设备中, 其包含有多个BluetoothGattService, 而每个BluetoothGattService中又包含有多个BluetoothGattCharacteristic。

(1)获取到设备中的服务列表  mBluetoothGatt.getServices(); 或者通过uuid 来获取某一个服务:

public List<BluetoothGattService> getSupportedGattServices() {
        if (mBluetoothGatt == null)
            return null;
        return mBluetoothGatt.getServices();
        // 或者通过uuid来获取某一个服务 mBluetoothGatt.getServices(uuid);
}

(2)通过Gatt这个对像,就是蓝牙连接完成后获取到的对象,通过这个对象设置好指定的通道向设备中写入和读取数据。

服务中, 获得Characteristic 集合则调用 gattService.getCharacteristics():

在 Characteristic中, 可以通过  mBluetoothGatt.readCharacteristic(characteristic); 来读取其里面的数据, 其结果在mGattCallback 回调函数中获取.

写如数据:  characteristic .setValue(data); 

mBluetoothGatt .wirteCharacteristic(mCurrentcharacteristic);

1 public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
2 
3         if (mBluetoothAdapter == null || mBluetoothGatt == null) {
4             Log.w(TAG, "BluetoothAdapter not initialized");
5             return;
6         }
7 
8         mBluetoothGatt.writeCharacteristic(characteristic);
9  }
1 public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
2         if (mBluetoothAdapter == null || mBluetoothGatt == null) {
3             Log.w(TAG, "BluetoothAdapter not initialized");
4             return;
5         }
6         mBluetoothGatt.readCharacteristic(characteristic);
7  }

注意:BluetoothGattCharacteristic这个是指定的通道,蓝牙服务:

1 BluetoothGattCharacteristic characteristic = null;
2 characteristic = mGattCharacteristics.get(4).get(4);

这两个数字就是从指定的服务中找到你要发送数据的那个服务。

如果要进行多个连接,每次连接完成后可以将BluetoothGatt的对象放到一个list里面,获取到的服务也放到一个List里面,然后发送数据的时候调用不同的Gatt发送不同的通道数据即可。

参考:

Android提高之Android手机与BLE终端通信

android 蓝牙4.0多通道

Android Bluetooth Low Energy(官方)

Android 蓝牙4.0 BLE

标签:

原文地址:http://www.cnblogs.com/CharlesGrant/p/5033428.html

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