gpt4 book ai didi

Android BLE,不能真正写特性

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:48 25 4
gpt4 key购买 nike

我正在开发一个 Android 项目,该项目将通过 BLE 链路连接 Nexus 7 和生物传感器。问题是,我可以成功检测并获取传感器的服务和特征列表。当我向特定特征写入一些数据时,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;
}

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

最佳答案

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

OP 写道:

Follow Up:

The problem solved by manually pairing the tablet with the device first in the setting instead of pairing by code.

So only using the code snippet of connecting Gatt provided by Android is not good enough to pair the device. I should add another code I found online to pair the devices if I don't want to pair them manually every time:

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());
}
}

关于Android BLE,不能真正写特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com