详解蓝牙BluetoothGattCallback用法之BLE数据接收

来源:互联网 发布:centos6 离线安装mysql 编辑:程序博客网 时间:2024/05/22 10:40

详解蓝牙BluetoothGattCallback用法之BLE数据接收

这篇继续讲解数据接收,想要接收到数据必须先要使能通知

@Override    public void onServicesDiscovered(BluetoothGatt gatt, int status) {//发现服务,在蓝牙连接的时候会调用        List<BluetoothGattService> list = mBluetoothGatt.getServices();        for (BluetoothGattService bluetoothGattService:list){            String str = bluetoothGattService.getUuid().toString();            Log.e("onServicesDisc中中中", " :" + str);            List<BluetoothGattCharacteristic> gattCharacteristics = bluetoothGattService                    .getCharacteristics();            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {                Log.e("onServicesDisc中中中", " :" + gattCharacteristic.getUuid());                if("0000ffe1-0000-1000-8000-00805f9b34fb".equals(gattCharacteristic.getUuid().toString())){                    linkLossService=bluetoothGattService;                    alertLevel=gattCharacteristic;                    Log.e("daole",alertLevel.getUuid().toString());                }            }        }        enableNotification(true,gatt,alertLevel);//必须要有,否则接收不到数据        //Log.e("onServicesDisc中中中", " :" + mBluetoothGatt.getServices().toString());    }

就是 {enableNotification(true,gatt,alertLevel);//必须要有,否则接收不到数据} 这句话,这个必须要有不然接收数据的那个回调函数会毫无反应,不要问为什么,程序主要还是测试,都是泪,为了做一个工具用了好久。

    private void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {    if (gatt == null || characteristic == null)        return; //这一步必须要有 否则收不到通知     gatt.setCharacteristicNotification(characteristic, enable);}

这些都做完了以后,在发送了指令后,如果有数据返回,就会接收到数据

        @Override    public void onCharacteristicChanged(BluetoothGatt gatt,                                        BluetoothGattCharacteristic characteristic) {// Characteristic 改变,数据接收会调用        Log.e("CharacteristicChanged中", "数据接收了哦"+bytesToHexString(characteristic.getValue()));        tx_receive.append(bytesToHexString(characteristic.getValue()) + "\n");    }

tx_receive.append(bytesToHexString(characteristic.getValue()) + “\n”);这个是将接收的数据在textview中显示出来。append是追加显示,有的手机可能不能在第一时间显示出数据,那么你就滑动一下listview啥的,数据就会显示了。

数据写入状态

        @Override    public void onCharacteristicWrite(BluetoothGatt gatt,                                      BluetoothGattCharacteristic characteristic, int status) {//发送数据时调用        Log.e("onCharacteristicWrite中", "数据发送了哦");        Log.e("onCharacteristicWrite中", bytesToHexString(characteristic.getValue()));        if(status == BluetoothGatt.GATT_SUCCESS){//写入成功            Log.e("onCharacteristicWrite中", "写入成功");            tx_display.append("写入成功");        }else if (status == BluetoothGatt.GATT_FAILURE){            Log.e("onCharacteristicWrite中", "写入失败");        }else if (status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){            Log.e("onCharacteristicWrite中", "没权限");        }    }

这个是显示写入状态的回调函数,写入成功后就会显示的,这个
boolean status = mBluetoothGatt.writeCharacteristic(alertLevel);
也可以显示写入成功没有。
关于数据接收与发送的我已经写完了,我所有的地方都没有用到广播和服务,使用我的代码的时候一定要注意。
如果有什么更好的想法,本人能力也有限,如有错误,也欢迎指正。
源码(http://download.csdn.net/download/uu13567/9983668