BLE外围设备和BLE中心

阿伦

我正在使用BLE Android到iOS开发聊天应用程序,现在我正在使用以下两个库作为参考

https://github.com/izumin5210/Bletia

https://github.com/captain-miao/bleYan

我面临的问题是外围设备写入任何特征时,不会调用ble中央设备(Android)中的BLECallback。

我的回电码

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        BleLog.i(TAG, "onConnectionStateChange: State = " + BleUtils.getBleConnectStatus(status)
                + " newState = " + BleUtils.getBleConnectStatus(newState));

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            updateState(BleConnectState.CONNECTED);
            //开始发现服务
            BleLog.i(TAG, "gatt.discoverServices()");
            gatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_CONNECTING) {
            updateState(BleConnectState.CONNECTING);
        } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
            updateState(BleConnectState.DISCONNECTING);
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            //断开了,需要做什么处理?
            sIsWriting = false;
            sWriteQueue.clear();
            updateState(BleConnectState.DISCONNECTED);
        }
    }

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

        if (status == BluetoothGatt.GATT_SUCCESS) {
            onDiscoverServices(gatt);
            //需要返回 gatt
            updateState(BleConnectState.SERVICE_IS_DISCOVERED);
        } else {
            BleUtils.refreshDeviceCache(mGatt);
            //失败 需要做何处理 129
            if(mState != BleConnectState.SERVICE_IS_NOT_DISCOVERED) {
                updateState(mState);
            }
        }

        //MSG_BLE_ID_SERVICES_DISCOVERED
        Message msg = Message.obtain();
        msg.what = BleConstants.MSG_BLE_ID_SERVICES_DISCOVERED;
        msg.arg1 = status;
        msg.obj = gatt;
        notifyAllBleClients(msg);
        BleLog.i(TAG, "onServicesDiscovered: " + BleUtils.getGattStatus(status));
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic, int status) {
        BleLog.i(TAG, "onCharacteristicWrite: " + BleUtils.getGattStatus(status));
        UUID uuid = characteristic.getUuid();
        sendBleMessage(BleConstants.MSG_BLE_ID_CHARACTERISTIC_WRITE, status, uuid);
        onNextWrite();
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt,
            BluetoothGattDescriptor descriptor, int status) {
        BleLog.i(TAG, "onDescriptorWrite: " + BleUtils.getGattStatus(status));
        UUID uuid = descriptor.getUuid();

        sendBleMessage(BleConstants.MSG_BLE_ID_DESCRIPTOR_WRITE, status, uuid);
        onNextWrite();
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        final byte[] data = characteristic.getValue();
        BleLog.i(TAG, "onCharacteristicChanged: " + HexUtil.encodeHexStr(data));
        UUID uuid = characteristic.getUuid();

        sendBleMessage(BleConstants.MSG_BLE_ID_CHARACTERISTIC_NOTIFICATION, BluetoothGatt.GATT_SUCCESS, data, uuid);
        onNextWrite();
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        final byte[] data = characteristic.getValue();
        System.out.println(TAG +" onCharacteristicRead: " + data);

        if(data != null) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for (byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));

            final String values = stringBuilder.toString();

            BleLog.i(TAG, "onCharacteristicRead: " + new String(data));
        }else{
            BleLog.i(TAG, " onCharacteristicRead: " + "NULL");
        }

        UUID uuid = characteristic.getUuid();

        sendBleMessage(BleConstants.MSG_BLE_ID_CHARACTERISTIC_READ, status, data, uuid);
        onNextWrite();
    }

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        final byte[] data = descriptor.getValue();

        if(data != null) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for (byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));

            final String values = stringBuilder.toString();

            BleLog.i(TAG, "onDescriptorRead: " + new String(data, Charset.defaultCharset()));
        }else{
            BleLog.i(TAG, " onDescriptorRead: " + "NULL");
        }
        UUID uuid = descriptor.getUuid();

        sendBleMessage(BleConstants.MSG_BLE_ID_DESCRIPTOR_READ, status, data, uuid);
        onNextWrite();
    }

    @Override
    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
        BleLog.i(TAG, "onReliableWriteCompleted: " + BleUtils.getGattStatus(status));

        Message msg = Message.obtain();
        msg.what = BleConstants.MSG_BLE_ID_RELIABLE_WRITE_COMPLETED;
        msg.arg1 = status;
        notifyAllBleClients(msg);
        onNextWrite();
    }

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        BleLog.i(TAG, "onReadRemoteRssi: " + rssi + " status:" + BleUtils.getGattStatus(status));

        Message msg = Message.obtain();
        msg.what = BleConstants.MSG_BLE_ID_READ_REMOTE_RSSI;
        msg.arg1 = status;
        msg.arg2 = rssi;
        notifyAllBleClients(msg);
        onNextWrite();
    }

    @Override
    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
        BleLog.i(TAG, "onMtuChanged: " + BleUtils.getGattStatus(status));

        Message msg = Message.obtain();
        msg.what = BleConstants.MSG_BLE_ID_MTU_CHANGED;
        msg.arg1 = status;
        msg.arg2 = mtu;
        notifyAllBleClients(msg);
        onNextWrite();
    }
};

iOS通知码

    if (myPeripheral.connectedToPeer)
    {
        CBCharacteristic *peerAvailable = [myPeripheral findCharacteristicFromUUID:myPeripheral.notifyPeerAvailableUUID];
        if (peerAvailable && !peerAvailable.isNotifying) {
            [myPeripheral.cbPeripheral setNotifyValue:YES forCharacteristic:peerAvailable];
        }

        CBCharacteristic *peerReceiveMessage = [myPeripheral findCharacteristicFromUUID:myPeripheral.peerSendToCentralFromPeripheralUUID];
        if (peerReceiveMessage && !peerReceiveMessage.isNotifying) {
            [myPeripheral.cbPeripheral setNotifyValue:YES forCharacteristic:peerReceiveMessage];
        }

    }
埃拉克斯

外围设备应通过使用以下命令通知特性已更改 bluetoothGattServer.notifyCharacteristicChanged

Android中央设备应订阅该通知。

if (characteristic != null) {
                        //SetNotification
                        Log.i(TAG, "SetNotification");
                        gatt.setCharacteristicNotification(characteristic, true);
                        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            gatt.writeDescriptor(descriptor);
                        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android BLE外围设备数据接收

来自分类Dev

如何查看外围设备的BLE服务?

来自分类Dev

如何在不从BLE中心设备连接到BLE外围设备的情况下连续获取RSSI?

来自分类Dev

BLE外围设备:连接时进行扫描

来自分类Dev

BLE外围设备支持Android-L的示例

来自分类Dev

在Windows 10上连接到BLE外围设备

来自分类Dev

Ionic BLE Android Ble正在检索与IOS Ble不同的外围设备的MAC地址

来自分类Dev

从外围设备传入BLE连接时,iOS停止唤醒应用程序

来自分类Dev

在英特尔爱迪生上使用C开发BLE外围设备

来自分类Dev

取消BLE外围设备连接后无法重新连接iOS

来自分类Dev

BLE外围设备可以连接到Android手机上的后台应用程序吗?

来自分类Dev

在英特尔爱迪生上使用C开发BLE外围设备

来自分类Dev

Android Lollipop BLE外围设备:调用BluetoothLeAdvertiser.stopAdvertising(AdvertiseCallback)与已连接的中央设备断开连接

来自分类Dev

集成iBeacon和coreBluetooth外围设备

来自分类Dev

iOS中心如何使用BTLE将来电和SMS通知发送到外围设备?

来自分类Dev

CMSIS和外围设备驱动程序

来自分类Dev

从外围设备更改CBAdvertisementDataManufacturerDataKey

来自分类Dev

USB外围设备之间的通讯

来自分类Dev

CoreBluetooth Central->外围设备

来自分类Dev

外围设备未连接-Swift

来自分类Dev

核心蓝牙无法发现外围设备

来自分类Dev

CBCentral Manager未发现外围设备

来自分类Dev

USB集线器无法熄灭外围设备

来自分类Dev

USB外围设备之间的通讯

来自分类Dev

iOS CoreBluetooth CBP外围设备didReceiveReadRequest

来自分类Dev

USB 外围设备的 bIntervals 是如何执行的?

来自分类Dev

PiZero W连接到两个外围设备(GPIO和USB):如何同时连续从两个外围设备读取数据?

来自分类Dev

Android 设备作为 BLE 中心和读/写操作,无需任何外设触发

来自分类Dev

关于ARM(特别是移动)外围设备寻址和总线体系结构的解释?

Related 相关文章

  1. 1

    Android BLE外围设备数据接收

  2. 2

    如何查看外围设备的BLE服务?

  3. 3

    如何在不从BLE中心设备连接到BLE外围设备的情况下连续获取RSSI?

  4. 4

    BLE外围设备:连接时进行扫描

  5. 5

    BLE外围设备支持Android-L的示例

  6. 6

    在Windows 10上连接到BLE外围设备

  7. 7

    Ionic BLE Android Ble正在检索与IOS Ble不同的外围设备的MAC地址

  8. 8

    从外围设备传入BLE连接时,iOS停止唤醒应用程序

  9. 9

    在英特尔爱迪生上使用C开发BLE外围设备

  10. 10

    取消BLE外围设备连接后无法重新连接iOS

  11. 11

    BLE外围设备可以连接到Android手机上的后台应用程序吗?

  12. 12

    在英特尔爱迪生上使用C开发BLE外围设备

  13. 13

    Android Lollipop BLE外围设备:调用BluetoothLeAdvertiser.stopAdvertising(AdvertiseCallback)与已连接的中央设备断开连接

  14. 14

    集成iBeacon和coreBluetooth外围设备

  15. 15

    iOS中心如何使用BTLE将来电和SMS通知发送到外围设备?

  16. 16

    CMSIS和外围设备驱动程序

  17. 17

    从外围设备更改CBAdvertisementDataManufacturerDataKey

  18. 18

    USB外围设备之间的通讯

  19. 19

    CoreBluetooth Central->外围设备

  20. 20

    外围设备未连接-Swift

  21. 21

    核心蓝牙无法发现外围设备

  22. 22

    CBCentral Manager未发现外围设备

  23. 23

    USB集线器无法熄灭外围设备

  24. 24

    USB外围设备之间的通讯

  25. 25

    iOS CoreBluetooth CBP外围设备didReceiveReadRequest

  26. 26

    USB 外围设备的 bIntervals 是如何执行的?

  27. 27

    PiZero W连接到两个外围设备(GPIO和USB):如何同时连续从两个外围设备读取数据?

  28. 28

    Android 设备作为 BLE 中心和读/写操作,无需任何外设触发

  29. 29

    关于ARM(特别是移动)外围设备寻址和总线体系结构的解释?

热门标签

归档