android低功耗蓝牙APP开发问题记录

本文档记录了在Android平台上进行低功耗蓝牙(BLE)应用开发时遇到的问题及解决方案,包括针对不同Android版本的扫描方法回调调整、Android 6.0权限管理、蓝牙读写通信、全局变量的使用以及不同分辨率手机的界面适配。通过实例代码展示了如何处理这些问题。
摘要由CSDN通过智能技术生成

概述:实现了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的权限文档对代码进行修改。
例如:
目标版本过高报错
修改targetSdkVersion后运行正常
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) {
        
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值