Android 蓝牙 4.0 ble 基础通讯篇

这篇博客主要讲解 Android 蓝牙 BLE 基础通讯。


一、添加权限

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

不过在 Android 6.0之后 需要动态申请定位权限。

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

 

二、扫描

  • 首先初始化蓝牙适配器 
BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
     BluetoothAdapter  mBluetoothAdapter = bluetoothManager.getAdapter();
  • 根据 BluetoothAdapter  进行开始、停止扫描
 mBluetoothAdapter.startLeScan(mLeScanCallback);//开始扫描
 mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止扫描

 private BluetoothAdapter.LeScanCallback mLeScanCallback =
         new BluetoothAdapter.LeScanCallback() {
         @Override
          public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
                    //这里不能做太多的操作,要抛到另外一个线程中去处理。
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //device:设备对象
                            //rssi 信号值
                            //scanRecord 广播包
                            
                        }
                    });
                }
            };

三、连接

在获取到设备的 MAC 地址之后,就可以进行连接

BluetoothGatt mGatt = device.connectGatt(mContext, false, new BluetoothGattCallback() {

    //连接状态改变时回调
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "连接成功");
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "连接已断开");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

    }
   
    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

});

四、接收和发送

  • 接收

如果蓝牙设备具有 notifiy/indecation 功能,那么我们可以通过 onCharacteristicChanged() 回调获取数据,当然我们也是需要设置的。在蓝牙连接成功时,需要打开通知功能,告诉设备可以发信息给我们。

   //设置为Notifiy
    mGatt.setCharacteristicNotification(mCharacteristic, mEnable);  
    BluetoothGattDescriptor descriptor =             
    mCharacteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mGatt.writeDescriptor(descriptor);
  • 发送
    characteristicWrite.setValue(getAllDataOrder());
    mGatt.writeCharacteristic(characteristicWrite);

这里的 mGatt 是在连接成功之后的对象,需要设置成全局变量。

五、解析数据

    /**
     * byte[] 转 字符串
     * @param bytes
     * @return
     */
    public static String bytesToHexString(byte[] bytes) {
        String result = "";
        for (int i = 0; i < bytes.length; i++) {
            String hexString = Integer.toHexString(bytes[i] & 0xFF);
            if (hexString.length() == 1) {
                hexString = '0' + hexString;
            }
            result += hexString.toUpperCase();
        }
        return result;
    }

好,简单介绍蓝牙 BLE 的扫描、连接、收发数据,就此到一段落。下次介绍 Android 蓝牙协议中 SPP 的开发。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
蓝牙4.0BLE开发完全手册——物联网开发技术实战》是一本专门介绍蓝牙4.0低功耗(Bluetooth Low Energy, BLE)开发技术并应用于物联网的实战指南。 该书从基础概念出发,分析了BLE技术的特点、优势和应用领域。首先介绍了蓝牙技术的发展历程,然后深入研究了BLE的基本原理和通信协议,包括物理层和协议栈的组成、BLE传输数据的方式以及BLE设备之间的连接和广播等内容。同时,还对BLE的安全性和功耗进行了详细的讲解。 随后,本书重点聚焦于BLE开发的实战应用。作者通过实例演示了如何使用iOS和Android平台上的开发工具来搭建一个BLE应用程序,包括手机与BLE设备之间的连接与通信、UUID的使用、服务和特征的定义以及数据的读写等操作。此外,还介绍了如何在物联网场景中使用BLE技术,如智能家居、智能健康等方面的应用。 该书以简明易懂的方式呈现了BLE技术的相关知识,并结合实际案例进行讲解,使读者可以快速上手并实践于物联网开发中。此外,书中还包括了常见问题和解决方案,以帮助读者解决开发过程中可能遇到的困难与挑战。 综上所述,《蓝牙4.0BLE开发完全手册——物联网开发技术实战》是一本全面介绍BLE开发技术的实用指南,是物联网开发者不可或缺的参考工具。无论是对于初学者还是已有一定经验的开发者来说,都能够从中获得宝贵的知识和实战经验,提升物联网开发的技能水平。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值