【问题标题】:Android BLE, can not really write characteristicAndroid BLE,不能真正写特性
【发布时间】:2023-03-14 02:07:01
【问题描述】:

我正在开发一个通过 BLE 链接连接 Nexus 7 和生物传感器的 Android 项目。问题是,我可以成功检测并获取传感器的服务和特性列表。当我向特定特征写入一些数据时,会自动调用onCharacteristicWrite 并显示写入操作成功。但是,传感器永远不会收到来自平板电脑的任何信息。如果我在 iPhone 上使用类似的应用程序,一切正常。所以设备没有问题。 有人知道这个问题吗?

这是我的写代码:

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnected = true;
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnected = false;
            Log.i(TAG, "Disconnected from GATT server.");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {

         //Once detected services, write to characteristic for 6 times.
          int count =6;
            while(count>0){

              writeCharacteristic();

                count--;

            }

        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

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

        if (status == BluetoothGatt.GATT_SUCCESS){

            Log.d(TAG,"Write to Characteristic Success! !");
        }

    }
};

public boolean writeCharacteristic(){

    //check mBluetoothGatt is available
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }

    BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
    if (Service == null) {
        Log.e(TAG, "service not found!");
        return false;
    }
    BluetoothGattCharacteristic characteristic = Service
            .getCharacteristic(UUID_MY_CHARACTERISTIC);
    if (characteristic == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] value = {(byte)300,(byte)100,(byte)100};
    characteristic.setValue(value);

    boolean status = mBluetoothGatt.writeCharacteristic(characteristic);

    return status;
}

输出显示“写入特征成功!!”六次,写操作成功。但是,设备显示没有从平板电脑收到任何信息。我还尝试一次写入一个字节,或者添加一个计时器让平板电脑每 2 秒写入一次传感器。但他们都没有工作。有任何想法吗?

【问题讨论】:

  • 您是否尝试过开启 HCI snoop(手机上的开发者设置)并查看发送和接收的内容?
  • 嗨,我刚做了。但是,我不知道如何正确打开它。你能告诉我如何用logcat打开它吗?我尝试用文本或崇高打开它,但结果不可读。这个日志文件和我在运行程序时从 Android Studio Logcat 得到的信息有什么区别?
  • 相信你需要使用Wireshark来阅读日志。我没有尝试过,只是想在这种情况下它可能会对你有所帮助。
  • 您好僵尸,感谢您的回复。该问题已通过手动将平板电脑与设备配对解决。
  • 我无法一次将多个字节写入一个特征。这段代码对你有用吗?字节[] 值 = {(byte)300,(byte)100,(byte)100};特征.setValue(值); boolean status = mBluetoothGatt.writeCharacteristic(characteristic);

标签: android bluetooth-lowenergy characteristics


【解决方案1】:

(由问题编辑回答。转换为社区 wiki 答案。见What is the appropriate action when the answer to a question is added to the question itself?

OP 写道:

跟进:

通过在设置中先手动将平板与设备配对而不是通过代码配对解决了问题。

所以只使用Android提供的连接Gatt的代码sn-p是不够好的配对设备的。如果我不想每次都手动配对设备,我应该添加我在网上找到的另一个代码来配对设备:

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass()
                .getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 2014-12-10
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多