gpt4 book ai didi

android - onCharacteristicChanged() 被多次调用

转载 作者:行者123 更新时间:2023-11-29 16:42:00 26 4
gpt4 key购买 nike

我正在我的应用程序中使用 BLE 连接。我有一个用于蓝牙功能的类,我正在传递来自不同 fragment 类的命令以写入任何值。

因此根据场景,在 fragment 内,单击按钮将向蓝牙类发送写入命令。它第一次运行良好,我收到了回复。但是当第二次点击按钮时, onCharacteristicChanged() 被调用两次,第三次点击使其被调用三次,依此类推。我真的想不通。我将在下面发布我的代码。请看一看。如有任何疑问,请告诉我。提前致谢。

我正在 OnDescriptorWrite() 中写入数据,其中接收数据onCharacteristicChanged()。

fragment 内部:

     rvw_availableList.addOnItemTouchListener((
new RecyclerItemClickListener(myContext, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position)
{
BluetoothDevice bl_device = al_deviceList.get(position);
bleUtil.writeData(bl_device,"3AxxxxxxD");
}
})
));

现在在 BleUtil 类的 writeData() 中:

    public void writeData(BluetoothDevice bluetoothDevice, final String value)
{
BluetoothGattCallback gattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
gatt.discoverServices();

}

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


activity.runOnUiThread(new Runnable() {
public void run() {

// prgd_progress.HideProgressDialog();
Log.d("onServicesDiscovered", "Service uuid ");

List<BluetoothGattService> gattServices = gatt.getServices();


Log.d("onServicesDiscovered", "Services count: "+gattServices.size());

for (BluetoothGattService gattService: gattServices)
{
Log.d("aniservice",gattService.getUuid().toString());
}

if (status == BluetoothGatt.GATT_SUCCESS) {

ArrayList<String> alst_uuid = new ArrayList<String>();


BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString(SERVICE_ID)).getCharacteristics().get(0);
Log.d("anicheck",characteristic.getUuid().toString());
Log.d("anicheck",characteristic.getDescriptors().get(0).getUuid().toString());
// BluetoothGattCharacteristic characteristic =
// gattServices.get(0).getCharacteristics().get(0);

// Log.d("foundoutchar",gattServices.get(0).getUuid()+" "+gattServices.get(0).getCharacteristics().get(0).getUuid()+"");
gatt.setCharacteristicNotification(characteristic,true);

for (BluetoothGattDescriptor descriptor:characteristic.getDescriptors()){
Log.e("anicheck", "BluetoothGattDescriptor: "+descriptor.getUuid().toString());
}
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

if(descriptor!= null)
{
descriptor.setValue(
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);

}
else
{
Toast.makeText(activity,"nullval", Toast.LENGTH_SHORT).show();
}

// Log.d("foundoutchar", descriptor.getUuid().toString());


}
}
});


}

@Override
public void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
activity.runOnUiThread(new Runnable() {
public void run()
{

}
});
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status)
{
Log.d("onCharacteristicread",characteristic.getValue().toString());
Log.d("onCharacteristicread","called");


}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] charValue = characteristic.getValue();
final String str_result = bytesToHex(charValue);
Log.d("onCharacteristicfullres",str_result);
final Intent intent = new Intent("ble_data"); //FILTER is a string to identify this intent
intent.putExtra("val", "getdeviceinfo");
intent.putExtra("data", str_result);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);




activity.runOnUiThread(new Runnable() {
public void run()
{

// byte[] charValue = characteristic.getValue();
// String str_result = bytesToHex(charValue);
// Log.d("onCharacteristicfullres",str_result);
//Toast.makeText(activity, "On char changed "+str_result, Toast.LENGTH_SHORT).show();


}
});
}
@Override
public void onDescriptorWrite(final BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
{
activity.runOnUiThread(new Runnable() {
public void run()
{
Log.d("oncharadesc","abcd");
// prgd_progress.HideProgressDialog();

BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_ID));

for (BluetoothGattCharacteristic characteristics: service.getCharacteristics())
{
Log.d("getit",characteristics.getUuid().toString());
}

final BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString(SERVICE_ID)).getCharacteristics().get(0);


byte[] byt_arr;

byt_arr = hexStringToByteArray(value);

characteristic.setValue(byt_arr);
gatt.writeCharacteristic(characteristic);


}
});

}


};
BluetoothGatt bluetoothGatt = bluetoothDevice.connectGatt(activity, true, gattCallback);

}

最佳答案

是因为你多次调用了connectGatt。每次调用 connectGatt 时,都会创建一个 GATT 客户端对象来监听通知。因此,按三下后,您将拥有三个 GATT 客户端,它们都处理每个通知。

您应该更改代码,以便在写入数据时使用之前创建的 GATT 客户端。

关于android - onCharacteristicChanged() 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50228728/

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