首页 > IOS蓝牙读取数据缓存这一块,有安卓代码,我们IOS的要怎么写

IOS蓝牙读取数据缓存这一块,有安卓代码,我们IOS的要怎么写

RT,蓝牙折腾到数据收发这一块了,已经可以发送数据,就是读取蓝牙数据的这一块,要做个缓存,让收到数据的最大不能超过最大字节长度,IOS接收得到的是NSData,实在想不出要如何进行判断,上安卓代码求大神们帮忙看看

/** 一次能读的最大字节长度 **/
    private static final int MAX_READLEN = 4096;
    /** 读取数据的字节长度 **/
    private int mReadCount = 0;
    /** 每次缓存数据的字节数组 **/
    private byte[] mReadBuffer = new byte[MAX_READLEN];   
public void onCharacteristicRead(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                byte[] buffer = new byte[1024];
                int bytes;
                buffer = characteristic.getValue();
                bytes=buffer.length;
                synchronized (BluetoothLeService.this) {
                    if (mReadCount + bytes < MAX_READLEN) {
                        System.arraycopy(buffer, 0, mReadBuffer, mReadCount, bytes);
                        mReadCount += bytes;
                    } else if (mReadCount < MAX_READLEN) {
                        int count = MAX_READLEN - mReadCount;
                        System.arraycopy(buffer, 0, mReadBuffer, mReadCount, count);
                        mReadCount += count;
                    }
                }

                // TODO:读取数据
//                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }
        }

网上有类似代码啊 参阅 你懂的

【热门文章】
【热门文章】