Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 带Android Studio的蓝牙LE:带按钮的写入特性_Android Studio_Bluetooth Lowenergy - Fatal编程技术网

Android studio 带Android Studio的蓝牙LE:带按钮的写入特性

Android studio 带Android Studio的蓝牙LE:带按钮的写入特性,android-studio,bluetooth-lowenergy,Android Studio,Bluetooth Lowenergy,我对安卓和蓝牙还不熟悉,正为这个问题苦恼 如果触摸两个按钮,我想写一个特定的特征。 如果我按下第一个按钮,0-9之间的数字应为+1。使用另一个按钮,数字应减少为-1。 作为基础,我使用谷歌的BlueTootletegat示例应用程序。 在DeviceControlActivity中,我更改了以下代码: private final ExpandableListView.OnChildClickListener servicesListClickListner = new Expan

我对安卓和蓝牙还不熟悉,正为这个问题苦恼

如果触摸两个按钮,我想写一个特定的特征。 如果我按下第一个按钮,0-9之间的数字应为+1。使用另一个按钮,数字应减少为-1。 作为基础,我使用谷歌的BlueTootletegat示例应用程序。 在DeviceControlActivity中,我更改了以下代码:

private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                if (mGattCharacteristics != null) {
                    final BluetoothGattCharacteristic characteristic =
                            mGattCharacteristics.get(groupPosition).get(childPosition);
                    final int charaProp = characteristic.getProperties();  //The properties contain a bit mask of property flags indicating
                   //the features of this characteristic.
                   if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                        // If there is an active notification on a characteristic, clear
                        // it first so it doesn't update the data field on the user interface.
                        if (mNotifyCharacteristic != null) {
                            mBluetoothLeService.setCharacteristicNotification(
                                    mNotifyCharacteristic, false);
                            mNotifyCharacteristic = null;
                        }
                      mBluetoothLeService.readCharacteristic(characteristic);
                    }
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY ) > 0) {

                        mNotifyCharacteristic = characteristic;
                        mBluetoothLeService.setCharacteristicNotification(
                                characteristic, true);
                    }

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
                        characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);
                        addListenerOnButton();

                    }

                    return true;
                }
                return false;
            }
};
以下是我的addListenerOnButton()用于两个按钮:

public void addListenerOnButton() {

    mArrowUp = (ImageButton) findViewById(R.id.arrow_up);
    mArrowUp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
          mBluetoothLeService.writeCharacteristicUp();

        }

    });

    mArrowDown = (ImageButton) findViewById(R.id.arrow_down);
    mArrowDown.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
        mBluetoothLeService.writeCharacteristicDown();
        }

    });

}
他们将调用BluetoothLeService类中的两个writeCharacteristic方法。例如,我在这里仅发布writeCharacteristicUp()方法:

问题是

boolean status = mBluetoothGatt.writeCharacteristic(charac)  
如果不工作,状态将为false。因此,实际值不会显示在相应文本视图的屏幕上。为什么?
我还发现if研磨不工作,因为charac.setValue(value0)仅在if研磨之外工作?

我建议您按住control键,然后单击代码中的
BluetoothGatt#writeCharacteristic(ch)
函数。然后Android Studio将查看
BluetoothGatt#writeCharacteristic(BluetoothGattCharacteristic特征)
功能:

    public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
        && (characteristic.getProperties() &
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;

    if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
    if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;

    BluetoothGattService service = characteristic.getService();
    if (service == null) return false;

    BluetoothDevice device = service.getDevice();
    if (device == null) return false;

    synchronized(mDeviceBusy) {
        if (mDeviceBusy) return false;
        mDeviceBusy = true;
    }

    try {
        mService.writeCharacteristic(mClientIf, device.getAddress(),
            service.getType(), service.getInstanceId(),
            new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
            new ParcelUuid(characteristic.getUuid()),
            characteristic.getWriteType(), AUTHENTICATION_NONE,
            characteristic.getValue());
    } catch (RemoteException e) {
        Log.e(TAG,"",e);
        mDeviceBusy = false;
        return false;
    }

    return true;
}
在方法中放置断点并进行调试。以下是根据源代码得到
false
的可能原因:

  • 远程BLE设备没有写入或无响应写入访问权限
  • 您的服务为null,或者您设置的特征值为null
  • 您的设备已在使用远程设备执行另一个可恢复进程
  • 在我看来,最有可能的第二项是原因

        public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
            && (characteristic.getProperties() &
                BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
    
        if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
        if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
    
        BluetoothGattService service = characteristic.getService();
        if (service == null) return false;
    
        BluetoothDevice device = service.getDevice();
        if (device == null) return false;
    
        synchronized(mDeviceBusy) {
            if (mDeviceBusy) return false;
            mDeviceBusy = true;
        }
    
        try {
            mService.writeCharacteristic(mClientIf, device.getAddress(),
                service.getType(), service.getInstanceId(),
                new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
                new ParcelUuid(characteristic.getUuid()),
                characteristic.getWriteType(), AUTHENTICATION_NONE,
                characteristic.getValue());
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
            mDeviceBusy = false;
            return false;
        }
    
        return true;
    }