Android 蓝牙开发 BLE(低功耗) 摩拜单车共享汽车开门实例

1.打开蓝牙

2.蓝牙扫描,列出可用设备

3.关闭蓝牙扫描(不关闭会一直扫描)

4.找到目标蓝牙设备进行连接

5.连接成功,进行通信

6.关闭蓝牙释放资源

接下来我们要根据上面6个步骤进行API的说明,在说明前,我先说明一下

 

BLE 即 Bluetooth Low Energy,蓝牙低功耗技术,是蓝牙4.0引入的新技术,在安卓4.3(API 18)以上为BLE的核心功能提供平台支持和API。与传统的蓝牙相比,BLE更显著的特点是低功耗,所以现在越来越多的智能设备使用了BLE,比如满大街的智能手环,还有体重秤、血压计、心电计等很多BLE设备都使用了BLE与终端设备进行通信。

 

蓝牙BLE4.x

BLE分为三部分:

  • Service

  • Characteristic

  • Descriptor

    这三部分都用UUID作为唯一标识符。UUID为这种格式:0000ffe1-0000-1000-8000-00805f9b34fb。比如有3个Service,那么就有三个不同的UUID与Service对应。这些UUID都写在硬件里,我们通过BLE提供的API可以读取到。

  • 一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

 

与BLE设备相互通信的大致流程

 

扫描并与指定的BLE设备进行连接。

连接成功就能拿到设备的GATT、Service、Characteristic、Descriptor这几个关键的东西(其实能拿到很多东西),并且每个Service、Characteristic都有自己唯一的UUID。

开启数据通道,这里的一条数据通道就相当于一个Characteristic,而具体开启哪一个或哪几个,需要根据BLE设备的工程师给的协议文档,如果工程师给的文档很坑的话,就要自己调试,判断等。

手机就可以向BLE设备发送数据或接受BLE向手机发送的数据。

一般BLE设备向手机发送的数据都16进制的数据报,类似于IP数据报,需要自己解析,具体的解析规则BLE设备的工程师都会给一份协议文档的,看着解析就行了,到这里就完成了与BLE设备的通信了。

 

步骤:

(1)Service蓝牙功能集合,每一个Service都有一个UUID,

(2)Characteristic 在service中也有好多个Characteristic 独立数据项,其中也有独立UUID

上面的两个uuid需要从硬件工程师中获取,这样你才能匹配到你要的。

(3)BluetoothAdapter 蓝牙的打开关闭等基本操作

(4)BluetoothDevice 蓝牙设备,扫描到的

(5)BluetoothGatt 蓝牙连接重连断开连接等操作的类

(6)BluetoothGattCharacteristic 数据通信操作类,读写等操作

 

 

1.打开蓝牙

需要权限

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

BluetoothAdapter 这个类就是蓝牙的基本操作,比如打开关闭等

初始化蓝牙,得到BluetoothAdapter

private void initBlueTooth() {
    BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    if (manager != null) {
        bluetoothAdapter = manager.getAdapter();
        if (bluetoothAdapter != null) {
            //蓝牙没有打开
            if (!bluetoothAdapter.isEnabled()) {
                openBle();
            } else {
                Toast.makeText(MainActivity.this, "蓝牙已打开", Toast.LENGTH_SHORT).show();
                scanLeDevice(true);
            }
        } else {
            openBle();
        }
    }
}

打开蓝牙

   private void openBle() {
   //以下两种方式 第二种方式在onActivityResult处理回调
//        boolean enable = bluetoothAdapter.enable();//打开蓝牙'直接打开,用户不知权,用于定制系统'
//        Toast.makeText(MainActivity.this, "正在打开蓝牙", Toast.LENGTH_SHORT).show();
//        if (enable) {
//            Log.e("open",enable+"");
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    scanLeDevice(true);
//                }
//            },2000);
//
//        }

        //提示用户正在打开蓝牙
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);


    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == REQUEST_ENABLE_BT) {
        scanLeDevice(true);
    }
}

2.3.打开或者停止扫描,放在同一个方法中

/**
 * 打开或者停止扫描
 *
 * @param enable
 */
private void scanLeDevice(final boolean enable) {

    if (enable) {
        mScanning = true;
        // 定义一个回调接口供扫描结束处理
        bluetoothAdapter.startLeScan(mLeScanCallback);
        // 预先定义停止蓝牙扫描的时间(因为蓝牙扫描需要消耗较多的电量)
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                bluetoothAdapter.stopLeScan(mLeScanCallback);
            }
        }, SCAN_PERIOD);

    } else {
        mScanning = false;
        bluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

扫描回调,回调之后得到 BluetoothDevice 的集合,可以放到列表中去

/**
 * 扫描回调
 */
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
        if (bluetoothDevice.getName() != null) {
            if (!bluetoothDeviceArrayList.contains(bluetoothDevice)) {//去下重
                bluetoothDeviceArrayList.add(bluetoothDevice);
            }
            Log.e(TAG, "scan--" + bluetoothDevice.getName());
        }
    }
};

4.5.进行蓝牙连接

/**
 * 连接蓝牙 参数为目标设备
/
public void connectBle(BluetoothDevice bluetoothDevice) {
    mBluetoothDevice = bluetoothDevice;
    if (bluetoothDevice != null) {
        //第二个参数 是否重连
        mBluetoothGatt = bluetoothDevice.connectGatt(MainActivity.this, false, bluetoothGattCallback);
    }

}

连接回调

  /**
     * 蓝牙连接成功回调
     */

    private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
        }

        //不要执行耗时操作
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            if (newState == BluetoothProfile.STATE_CONNECTED) {//连接成功
                Log.e(TAG, "onConnectionStateChange 蓝牙连接");
                //这里要执行以下方法,会在onServicesDiscovered这个方法中回调,如果在                        //onServicesDiscovered方法中回调成功,设备才真正连接起来,正常通信
                gatt.discoverServices();
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.e(TAG, "onConnectionStateChange 蓝牙断连");
                if (mBluetoothDevice != null) {
                    //关闭当前新的连接
                    gatt.close();
                    characteristic = null;
                 
                }

            }

        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            //回调之后,设备之间才真正通信连接起来
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.e(TAG, "onServicesDiscovered 蓝牙连接正常");
                BluetoothGattService service = gatt.getService(UUID.fromString(BleConstantValue.serverUuid));//uuid从硬件工程师获取
                characteristic = service.getCharacteristic(UUID.fromString(BleConstantValue.charaUuid));
                gatt.readCharacteristic(characteristic);//执行之后,会执行下面的                onCharacteristicRead的回调方法
                //设置通知,一般设备给手机发送数据,需要以下监听
                setCharacteristicNotification(characteristic, true);
                //耗时操作,如果有ui操作,需要用到handler
                adapterFreshHandler.sendEmptyMessage(0);
            } else {
                Log.e(TAG, "onServicesDiscovered 蓝牙连接失败");
            }

        }

        //这个方法一般用不到
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.e(TAG, "callback characteristic read status " + status
                    + " in thread " + Thread.currentThread());
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.e(TAG, "read value: " + characteristic.getValue());
            }


        }

        //这个方法是写入数据时的回调,可以和你写入的数据做对比
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.e(TAG, "write value: " + FormatUtil.bytesToHexString(characteristic.getValue()));
        }

        //设备发出通知时会调用到该接口,蓝牙设备给手机发送数据,在这个方法接收
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.e(TAG, "接收:" + FormatUtil.bytesToHexString(characteristic.getValue()));//byte[]转为16进制字符串
            bleWriteReceiveCallback();
        }
    };

/**
* 设置通知
/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
        if (bluetoothAdapter == null || mBluetoothGatt == null) {
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    }

参考写入指令

/**
 * 写入命令
 */
private void write(byte[] cmd) {
    if (characteristic != null) {
        // 发出数据
        characteristic.setValue(cmd);
        if (mBluetoothGatt.writeCharacteristic(characteristic)) {
            Log.e(TAG, "写入成功");
        } else {
            Log.e(TAG, "写入失败");
        }
    } else {
        Toast.makeText(MainActivity.this, "蓝牙未连接", Toast.LENGTH_SHORT).show();
    }
}
 
发送数据
 
给设备发送数据,就是发送指令,文档上会写可以给设备发送指令,也就是功能,然后还会说明指令格式等等,指令最终也还是字节数组。 
如下代码,就是一条指令
 
    private byte[] getAllDataOrder() {
        Log.i(TAG, "获取全部数据指令");
        byte[] data = new byte[7];
        data[0] = (byte) 0x93;
        data[1] = (byte) 0x8e;
        data[2] = (byte) 0x04;
        data[3] = (byte) 0x00;
        data[4] = (byte) 0x08;
        data[5] = (byte) 0x05;
        data[6] = (byte) 0x11;
        return data;
    }
 
然后调用下面这2个方法写入指令,设备成功收到的话会触发onCharacteristicChanged回调,并收到数据。这里的characteristicWrite是我们前面拿到的那个。
 
    characteristicWrite.setValue(getAllDataOrder());
    gatt.writeCharacteristic(characteristicWrite);
--------------------- 
 

6.断开连接 释放资源

/**
 * 断开蓝牙设备
 */
public void bleDisConnectDevice(BluetoothDevice device) {
    if (mBluetoothGatt != null) {
        mBluetoothGatt.disconnect();
    }
}

 /**
     * 释放资源 
     */

    private void releaseResource() {
        Log.e(TAG, "断开蓝牙连接,释放资源");
        if (mBluetoothGatt != null) {
            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();
        }
    }

最后别忘了蓝牙广播:

 /**
     * 注册蓝牙监听广播
     */
    private void registerBleListenerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(bleListenerReceiver, intentFilter);
    }
 /**
     * 蓝牙监听广播接受者
     */
    private BroadcastReceiver bleListenerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        //连接的设备信息
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.e(TAG, "蓝牙广播" + action);

        if (mBluetoothDevice != null && mBluetoothDevice.equals(device)) {
            Log.e(TAG, "收到广播-->是当前连接的蓝牙设备");

            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                Log.e(TAG,"广播 蓝牙已经连接");

            } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
                Log.e(TAG,"广播 蓝牙断开连接");
            }
        } else {
            Log.e(TAG, "收到广播-->不是当前连接的蓝牙设备");
        }

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    Log.e(TAG, "STATE_OFF 蓝牙关闭");
                    adapter.clear();
                    releaseResource();
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Log.e(TAG, "STATE_TURNING_OFF 蓝牙正在关闭");
                    //停止蓝牙扫描
                    scanLeDevice(false);
                    break;
                case BluetoothAdapter.STATE_ON:
                    Log.d(TAG, "STATE_ON 蓝牙开启");
                    //扫描蓝牙设备
                    scanLeDevice(true);
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    Log.e(TAG, "STATE_TURNING_ON 蓝牙正在开启");
                    break;
            }
        }
        }
    };

所有完成,基本的蓝牙操作及通信功能,通信协议需要和蓝牙硬件厂商工程师获取。

3.ble

蓝牙开发小技巧

蓝牙4.0入门开发总结

1.有什么调试工具吗

nRF,网上下一个
https://jingyan.baidu.com/article/ab0b5630620287c15afa7df4.html

2.搜索出来很多蓝牙,怎么过滤掉不是我们的设备?

叫嵌入式那边改变蓝牙的名称,然后根据蓝牙名称过滤

3.链接ble设备,有时候一下子就能连接,有时候又链接不上这么办?

如果链接失败可以在代码里面重试链接,可以大大提高成功率

4.在for循环里面发包有时候设备会收不到怎么办

手机发太快设备处理不过来,所以发完一个包后要休眠50ms以上

5.ble只能一次发20个byte吗?能不能扩容?

可以扩容,除了手机端设置之外,还要嵌入式端修改代码,缺一不可
参考
https://blog.csdn.net/leconiot/article/details/76814107

 

应用地方:

1.摩拜

2.身份证

3.智能手环

4.温度计,

5.汗液仪,

6.心电图,

7.血压计等

 

蓝牙的选用

 

      既然有经典蓝牙和低功耗蓝牙之分,我们在设计物联网产品和智能硬件产品的时候,如何选择呢?

 

 经典蓝牙:蓝牙最初的设计意图,是打电话放音乐。3.0版本以下的蓝牙,都称为“经典蓝牙”。功耗高、传输数据量大、传输距离只有10米。

 

      低功耗蓝牙:就是BLE,通常说的蓝牙4.0(及以上版本)。低功耗,数据量小,距离50米左右。

 

传声音的,用经典蓝牙:

 

 如蓝牙耳机、蓝牙音箱。蓝牙设计的时候就是为了传声音的,所以是近距离的音频传输的不二选择。

 

电池供电、连手机APP的,用BLE:

 如共享单车锁、蓝牙智能锁、蓝牙防丢器、蓝牙室内定位,是目前手机和智能硬件通信的性价比最高的手段。直线距离约50米,一节5号电池能用一年,传输模组成本10块钱,远比WIFI、4G等大数据量的通信协议更实用。

 

又要声音又要数据的,用双模蓝牙: 双模蓝牙,就是同时支持经典蓝牙音频和低功耗蓝牙。

 

如智能电视遥控器、降噪耳机等。很多智能电视配的遥控器带有语音识别,需要用经典蓝牙才能传输声音

 

传大数据量的,用经典蓝牙: 如某些工控场景,使用Android或Linux主控,外挂蓝牙遥控设备的,可以使用经典蓝牙里的SPP协议,当作一个无线串口使用。速度比BLE传输快多了。

远距离的,不用蓝牙。 固定供电的、不考虑功耗的、要传超过几十米距离的、要传高速数据的,这些都不适合蓝牙。远距离的可以用2G、4G、NB-IOT,大数据量的可以用WIFI。

--------------------- 

 

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
BLE低功耗蓝牙)是一种通过蓝牙无线技术进行低功耗通信的协议。它是在传统蓝牙(Classic Bluetooth)的基础上发展而来,主要用于物联网、智能家居和健康追踪等领域。 BLE主要特点有以下几个方面: 1. 低功耗BLE采用了一种优化的通信方式,使设备在通信过程中的功耗大大降低,从而延长了设备的电池寿命,这对于需要长时间运行的设备非常重要。 2. 简化传输:BLE使用了一种称为GATT(通用属性)的协议,将数据分为服务和特征,通过读、写或订阅操作来传输数据,这种简化了传输过程,减少了额外的开销。 3. 快速连接:BLE的连接速度比传统蓝牙更快,可以在几十毫秒内建立连接,这对于移动设备和传感器等需要快速响应的设备非常重要。 4. 多设备连接:BLE支持同时连接多个设备,可以通过同一个移动设备与多个BLE设备进行通信,提高了系统的灵活性和可扩展性。 Android提供了一套完整的BLE开发API,开发者可以使用这些API来实现BLE通信功能。在Android中,开发BLE应用涉及到四个主要组件:BLE设备扫描、设备连接、数据传输和GATT服务管理。 开发者可以使用Android的BluetoothAdapter类来进行设备扫描和连接操作,可以通过BluetoothGatt类来进行GATT服务的操作,包括读、写、订阅等。 总之,BLE作为一种低功耗蓝牙通信协议,在物联网和智能设备领域应用广泛。在Android平台上进行BLE开发,可以借助Android提供的API,快速实现BLE通信功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值