概述:实现了BLE扫描、连接、读写、通知接收等操作。
1、扫描方法的回调函数更改
在android5.0(SDK 21)以上时,BluetoothAdapter.startLeScan()和stopLEScan已经过时,所以在5.0以上时需要判定android版本.
扫描代码:
private void scanLeDevice(final boolean enable) {
if (enable) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}, SCAN_PERIOD);
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mLEScanner.startScan(filters, settings, mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}
回调函数:
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("callbackType", String.valueOf(callbackType));
Log.i("result", result.toString());
BluetoothDevice btDevice = result.getDevice();
connectToDevice(btDevice);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult sr : results) {
Log.i("ScanResult - Results", sr.toString());
}
}
@Override
public void onScanFailed(int errorCode) {
Log.e("Scan Failed", "Error Code: " + errorCode);
}
};
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("onLeScan", device.toString());
connectToDevice(device);
}
});
}
};
2、Android6.0权限问题
在手机上编译调试时,由于android6.0系统APP权限改为手动添加,则在调试低版本android BLE程序时要在build.gradle中将targetSdkVersion改为23以下,否则会运行出错。要想完全解决该问题,则需要判断android系统版本,查看android6.0的权限文档对代码进行修改。
例如:
3、低功耗蓝牙读写通信
package com.example.lenovo.ss1003;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import static android.bluetooth.BluetoothAdapter.STATE_DISCONNECTED;
/**
* Created by lenovo on 2016/11/8.
*/
public class BluetoothLeService extends Service {
//预定义
private Handler mHandler;
private final String LIST_NAME = "NAME";
private final String LIST_UUID = "UUID";
private final static String TAG = BluetoothLeService.class.getSimpleName();
private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharateristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();//蓝牙数据描述对象链表
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private String mBluetoothDeviceAddress;
private BluetoothGatt bluetoothGatt;
public BluetoothGattService mBluetoothGattService;
private BluetoothGattCharacteristic mBluetoothGattCharacteristic;
private int mConnectionState = STATE_CONNECTED;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;
static final String ACTION_GATT_CONNECTED = "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
static final String ACTION_GATT_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
private static final String ACTION_GATT_SERVICES_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED = "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_GATT_WRITE="com.example.bluetooth.le.ACTION_WRITE";
static final String ACTION_DATA_AVAILABLE = "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
static final String EXTRA_DATA = "com.example.bluetooth.le.EXTRA_DATA";
public final static UUID UUID_TENS_MEASUREMWNT = UUID.fromString(SampleGattAttributes.TENS_MEASUREMENT);
public final static UUID UUID_TENS_CHARACTERISTIC = UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG);
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
//获取蓝牙的连接状态,gatt客户端,status蓝牙连接操作成功后的连接状态,newState返回新的连接状态
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
//判断蓝牙新的连接状态,更新并发送广播
if (newState == BluetoothProfile.STATE_CONNECTED) {