Android BLE低功耗蓝牙开发极简系列(二)之读写操作

来源:互联网 发布:windows是那个公司 编辑:程序博客网 时间:2024/06/05 02:03

简书地址:Android BLE低功耗蓝牙开发极简系列(二)之读写操作
这是Ble极简系列的第二篇文章,上一篇Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接主要是扫描连接,这一篇主要是读写操作。

发现服务

在连接成功后,可以通过Gatt进行discoverServices()。

     if (newState == BluetoothProfile.STATE_CONNECTED) {//当蓝牙设备已经连接  //获取ble设备上面的服务          Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();            Log.i("haha", "Attempting to start service discovery:" +                    mBluetoothGatt.discoverServices());            Log.d("haha", "onConnectionStateChange: " + "连接成功")        }

在mGattCallback 回调添加Servicest的相关回调

  //发现服务回调。    public void onServicesDiscovered(BluetoothGatt gatt, int status) {        Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status);        if (status == BluetoothGatt.GATT_SUCCESS) {                 //成功            }        }

读写开关

当返回的status == BluetoothGatt.GATT_SUCCESS时,进行读写以及通知相关的操作, 调用writeDescriptor(),注意设置setValue为ENABLE_INDICATION_VALUE,否则可能后续读取不到数据。

        if (status == BluetoothGatt.GATT_SUCCESS) {                 //成功             isServiceConnected = true;            boolean serviceFound;            Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status)            if (mBluetoothGatt != null && isServiceConnected) {                BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);                BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_NOTIFICATION);                boolean b = mBluetoothGatt.setCharacteristicNotification(characteristic, true);                if (b) {                    List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();                    for (BluetoothGattDescriptor descriptor : descriptors) {                        boolean b1 = descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);                        if (b1) {                            mBluetoothGatt.writeDescriptor(descriptor);                            Log.d(TAG, "startRead: " + "监听收数据");                        }                    }                }            }

设置成功,会在onDescriptorWrite方法进行回调,注意UUID_SERVICE,UUID_NOTIFICATION特征值UUID,可以询问公司固件端的开发人员,和开发人员配合修改。

     @Override    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {        super.onDescriptorWrite(gatt, descriptor, status);        Log.d(TAG, "onDescriptorWrite: " + "设置成功");    }

发送数据

 public void startSend(View view) {    if (mBluetoothGatt != null && isServiceConnected) {        BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);        BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_WRITE);        byte[] bytes = new byte[2];        bytes[0] = 04;        bytes[1] = 01;        characteristic.setValue(bytes);        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);        mBluetoothGatt.writeCharacteristic(characteristic);    }}

读取数据

读取数据在onCharacteristicChanged方法中,注意进制间的转换。

    @Override    public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {        byte[] value = characteristic.getValue();        Log.d(TAG, "onCharacteristicChanged: " + value);        String s0 = Integer.toHexString(value[0] & 0xFF);        String s = Integer.toHexString(value[1] & 0xFF);        Log.d(TAG, "onCharacteristicChanged: " + s0 + "、" + s);        for (byte b : value) {            Log.d(TAG, "onCharacteristicChanged: " + b);        }    }

断开操作

  if (mBluetoothGatt != null) {        mBluetoothGatt.close();    }

注意事项

一定要进行读写开关操作,注意descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE),否则可能读取不到数据。

Github

BleDemo

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 没网steam要登录怎么办 电脑有gta还需要安装怎么办 孤岛惊魂5卡顿怎么办 酷匠密码忘记了怎么办 战地1有时候卡死怎么办 战地一fps太低怎么办 Dnf与系统不兼容怎么办 使命召唤7显示w怎么办 战地3王者太卡怎么办 合金装备5消音器没了怎么办 红警基地没了怎么办 玩战地1帧数太低怎么办 战地1点游戏不开怎么办 ios耳机孔坏了怎么办? 吃泻药都不排便怎么办 上司离职了我该怎么办 我上司要辞职我怎么办 一方坚决不同意离婚我该怎么办 模拟农场车翻了怎么办 手机退出键坏了怎么办 dnf邮件发错了怎么办 手机提示sd卡已损坏怎么办 解压包文件数据损坏该怎么办 电脑被压缩后电脑打不开怎么办 眼睛里进了飞虫怎么办 虫子飞到眼睛里怎么办 云电脑pc版双鼠标怎么办 电脑蓝屏代码7f怎么办 笔记本电脑蓝屏开不了机怎么办 装xp系统后蓝屏怎么办 一键ghost断电了怎么办 如何防止cpu降频怎么办 win7 64位系统不兼容怎么办 网页无法加载打印机插件怎么办 微信提示安装了插件怎么办 电脑上不了网怎么办 win10 win10电脑突然没网了怎么办 笔记本玩战争前线发热严重怎么办 玩战争前线总是闪退怎么办 uu加速器卡在29怎么办 新ipad下载不了东西怎么办