Android-低功耗蓝牙BLE(Bluetooth Low Energy)开发

1.前言

1、Android 4.3 (API Level 18)才开始提供低功耗蓝牙开发API;
2、根据API可以 发现设备、查询服务、读写characteristics;
3、顾名思义,BLE出现旨在显著地降低能耗,这就使得app可以和一些低功耗设备交互,例如:接近传感器、心率监视器,健身设备,等等。

2.关键术语和概念

  1. Generic Attribute Profile (GATT):GATT协议定义了一个通用的规范,这个规范用于在BLE 连接上发送和接收一些短消息;
  2. Attribute Protocol (ATT):GATT 建立在ATT上面,ATT是一种最优化的协议对于BLE设备;
  3. Characteristic:这个东西可以被看成一个类来描述一些交互的信息;
  4. Descriptor:描述符,定义一些属性来描述Characteristic的值;
  5. Service:用来搜集Characteristic的服务,例如:心率监测器搜集一些心率测量相关的Characteristic数据;

3.manifest 权限配置
声明权限

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

静态声明以下语句表示app是否支持BLE,required=”true”表支持,反之则不支持;

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

之后在代码检查设备是否支持BLE

// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    //不支持 todo...
}else{
    //支持 todo...
}

4. 配置BLE

  1. 前提是设备支持BLE;
  2. 如果可能出现为false的情况下,我们有必要在代码中动态检查是否支持BLE;
//初始化蓝牙适配器,核心类
final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
private BluetoothAdapter mBluetoothAdapter;
...
//如果蓝牙适配器==null 或者 蓝牙没有打开,代码请求打开蓝牙
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

5.搜索设备

  1. 因为扫描设备比较耗电,所以一旦你发现了想要的设备,立即停止扫描;
  2. 千万不要不停地循环扫描,而是设置扫描的时间,因为之前可用的设备可能离开BLE的范围,这时候还在循环扫描比较浪费电;
  3. 注意:我们不能同时扫描 低功耗蓝牙设备(BLE)传统蓝牙设备
//startScan有很多个重载的方法 
mBluetoothAdapter.startScan(BluetoothAdapter.LeScanCallback);

mBluetoothAdapter.startScan (List<ScanFilter> filters, ScanSettings settings, ScanCallback callback);

...
/**
 * Activity 示例:扫描和展示可用BLE设备
 */
public class DeviceScanActivity extends ListActivity {

    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private Handler mHandler;

    // 扫描时间 10秒
    private static final long SCAN_PERIOD = 10000;
    ...
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // 通过Handler延迟任务,取消扫描
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

            mScanning = true;
            //开始扫描
            mBluetoothAdapter.startLeScan(mLeScanCallback);
            //扫描指定UUID的设备使用面这个方法
            startLeScan(UUID[], BluetoothAdapter.LeScanCallback)
        } else {
            mScanning = false;
            //停止扫描
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
        ...
    }
...
}
//BluetoothAdapter.LeScanCallback实现
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// 扫描设备后回调接口
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi,
            byte[] scanRecord) {
       //device 扫描到的设备
   }
};

6. 连接到服务(GATT Server)

//参数一 Context
//参数二 Boolean 是否尝试自动连接当BLE设备变得可用的时候
//参数三 BluetoothGattCallbac 回调接口
mBluetoothGatt = device.connectGatt(Context, Boolean, BluetoothGattCallback);
// A service that interacts with the BLE device via the Android BLE API.
//开启服务,通过Android BLE API 实现和BLE设备的交互
public class BluetoothLeService extends Service {
    private final static String TAG = BluetoothLeService.class.getSimpleName();

    private BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private String mBluetoothDeviceAddress;
    private BluetoothGatt mBluetoothGatt;
    private int mConnectionState = STATE_DISCONNECTED;

    private static final int STATE_DISCONNECTED = 0;
    private static final int STATE_CONNECTING = 1;
    private static final int STATE_CONNECTED = 2;

    public final static String ACTION_GATT_CONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
    public final static String ACTION_GATT_DISCONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
    public final static String ACTION_GATT_SERVICES_DISCOVERED =
            "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
    public final static String ACTION_DATA_AVAILABLE =
            "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
    public final static String EXTRA_DATA =
            "com.example.bluetooth.le.EXTRA_DATA";

    public final static UUID UUID_HEART_RATE_MEASUREMENT =
            UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);

    // Various callback methods defined by the BLE API.
    //BLE API 定义了多种回调方法
    private final BluetoothGattCallback mGattCallback =
            new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status,
                int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) { //连上GATT服务
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" +
                        mBluetoothGatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {//断开服务连接
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            }
        }

        @Override
        // New services discovered
        //新的服务被发现
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        // Result of a characteristic read operation
        //
        public void onCharacteristicRead(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic,
                int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }
        }
     ...
    };
...
}
private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile. Data
    // parsing is carried out as per profile specifications.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" +
                    stringBuilder.toString());
        }
    }
    sendBroadcast(intent);
}

通过广播接收者处理这些事件

// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a
// result of read or notification operations.
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
            clearUI();
        } else if (BluetoothLeService.
                ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the
            // user interface.
            displayGattServices(mBluetoothLeService.getSupportedGattServices());
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }
    }
};

7.连上服务后,可以读取数据了

public class DeviceControlActivity extends Activity {
    ...
    // Demonstrates how to iterate through the supported GATT
    // Services/Characteristics.
    // In this sample, we populate the data structure that is bound to the
    // ExpandableListView on the UI.
    private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null) return;
        String uuid = null;
        String unknownServiceString = getResources().
                getString(R.string.unknown_service);
        String unknownCharaString = getResources().
                getString(R.string.unknown_characteristic);
        ArrayList<HashMap<String, String>> gattServiceData =
                new ArrayList<HashMap<String, String>>();
        ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
                = new ArrayList<ArrayList<HashMap<String, String>>>();
        mGattCharacteristics =
                new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

        // Loops through available GATT Services.
        for (BluetoothGattService gattService : gattServices) {
            HashMap<String, String> currentServiceData =
                    new HashMap<String, String>();
            uuid = gattService.getUuid().toString();
            currentServiceData.put(
                    LIST_NAME, SampleGattAttributes.
                            lookup(uuid, unknownServiceString));
            currentServiceData.put(LIST_UUID, uuid);
            gattServiceData.add(currentServiceData);

            ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
                    new ArrayList<HashMap<String, String>>();
            List<BluetoothGattCharacteristic> gattCharacteristics =
                    gattService.getCharacteristics();
            ArrayList<BluetoothGattCharacteristic> charas =
                    new ArrayList<BluetoothGattCharacteristic>();
           // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic :
                    gattCharacteristics) {
                charas.add(gattCharacteristic);
                HashMap<String, String> currentCharaData =
                        new HashMap<String, String>();
                uuid = gattCharacteristic.getUuid().toString();
                currentCharaData.put(
                        LIST_NAME, SampleGattAttributes.lookup(uuid,
                                unknownCharaString));
                currentCharaData.put(LIST_UUID, uuid);
                gattCharacteristicGroupData.add(currentCharaData);
            }
            mGattCharacteristics.add(charas);
            gattCharacteristicData.add(gattCharacteristicGroupData);
         }
    ...
    }
...
}

8.接收GATT通知

private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
        UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

onCharacteristicChanged() 回调用来监听远程设备characteristic changes

@Override
// Characteristic notification
public void onCharacteristicChanged(BluetoothGatt gatt,
        BluetoothGattCharacteristic characteristic) {
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}

8.App和BLE设备交互完成,需要释放资源

public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要开始学习蓝牙低能耗(BLE)技术,首先需要了解BLE的基本原理和特点。BLE是一种低功耗蓝牙通信技术,在物联网和智能设备领域具有广泛的应用。它具有低成本、低功耗和简单易用的特点,适用于需要长时间运行并且电池寿命要求较高的设备。 学习BLE首先需要了解BLE的基本特性,包括广播、连接和数据传输。广播是BLE设备发送广播包来宣传自己的存在和服务信息,连接是建立BLE设备之间的连接通道来进行数据传输,数据传输可以是单向的也可以是双向的,适用于不同的应用场景。 其次,需要熟悉BLE开发工具和平台,例如使用BLE开发板、开发套件或者模块来进行BLE应用的开发。常见的BLE开发平台包括Arduino、Raspberry Pi、nRF52开发板等,可以选择适合自己的开发平台进行学习和实践。 另外,要学会使用BLE开发工具和编程语言,例如使用BLE SDK、BLE API和相应的编程语言(如C、C++、Python等)来编写BLE应用程序。通过学习和实践,可以掌握BLE开发流程、协议栈、服务和特征等相关知识。 最后,需要不断实践和练习,通过搭建BLE通信的demo、开发BLE应用程序、调试和优化BLE应用来提升自己的技能和经验。同时,可以参考相关的教程、文档和社区资源,积极参与BLE开发者社区,与他人交流和分享经验,共同进步。通过持续的学习和实践,可以逐渐掌握BLE技术,为物联网和智能设备领域的开发做出贡献。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值