您的位置:首页 > 移动开发 > Android开发

Android 蓝牙4.0 BLE

2015-12-09 17:03 471 查看
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返回


  // Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {

@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

}

};


(2)关闭BLE设备的搜索

mBluetoothAdapter.stopLeScan(mLeScanCallback);


(3)开启BLE设备的搜索

mBluetoothAdapter.startLeScan(mLeScanCallback);


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

(4)使用BluetoothGatt 来连接设备

两个设备通过BLE通信,首先需要建立GATT连接。这里我们讲的是Android设备作为client端,连接GATT Server。
连接GATT Server,你需要调用BluetoothDevice的connectGatt()方法。此函数带三个参数:Context、autoConnect(boolean)和BluetoothGattCallback对象。

private BluetoothGatt mBluetoothGatt;


  final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found.  Unable to connect.");
return false;
}
mBluetoothGatt = device.connectGatt(this, true, mGattCallback);


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

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt,
int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);         if (newState == BluetoothProfile.STATE_CONNECTED) {
setState(State.CONNECTED);
gatt.discoverServices();
} else {
setState(State.IDLE);
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt,
   int status) {
if(status == BluetoothGatt.GATT_SUCCESS) {
Log.v(TAG, "onServicesDiscovered: " + status);
}
}
}


连接时会走这个方法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);

public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {

if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}

mBluetoothGatt.writeCharacteristic(characteristic);
}


public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}


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

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


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

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

参考:

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

android 蓝牙4.0多通道

Android Bluetooth Low Energy(官方)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: