我正在使用我的应用程序中的BLE连接。我只有一个用于蓝牙功能的类,并且正在传递来自其他片段类的命令以写入任何值。

因此,根据场景,在片段内,单击按钮将向蓝牙类发送写命令。第一次工作正常,我得到了回应。但是在第二次单击按钮时,两次调用onCharacteristicChanged(),第三次单击使它被调用三次,依此类推。我真的无法弄清楚。我将在下面发布我的代码。请看一看。如有任何疑问,请告诉我。提前致谢。

我在OnDescriptorWrite()里面写数据,在那里接收数据
onCharacteristicChanged()。

片段内部:

     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客户端。

10-07 18:31