Android可恢复启用通知

Android可恢复启用通知,android,bluetooth,Android,Bluetooth,我正在进行一个项目,其中包括与一个BLE按钮的交互,如下所示: 我的问题是,一旦用户按下ble按钮,我不知道如何启用通知。在这一刻,此方法onCharacteristicChanged从未触发 @Override public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { if (status == BluetoothGatt.GATT_SUCCESS) {

我正在进行一个项目,其中包括与一个BLE按钮的交互,如下所示:

我的问题是,一旦用户按下ble按钮,我不知道如何启用通知。在这一刻,此方法onCharacteristicChanged从未触发

    @Override
    public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        //read the characteristic data
        byte[] data = characteristic.getValue();
        Intent intent = new Intent(getActivity(), MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("ISFROMBLE", true);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
你能帮帮我吗?
谢谢

您可能已经获得了beacon sdk或jar。将其包含到您的android项目中。现在在eclipse项目中-

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

public class MainActivity extends Activity implements BeaconConsumer,
RangeNotifier {
//Add all unimplemented methods
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);
 } 

@Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
            final Region region) {
     Beacon beacon = (Beacon) iterator.next();

                        uuid = beacon.getId1().toUuidString();
                        major = beacon.getId2().toInt();
                         //get your id and match with your ble button. If it matches, then you can call your method or send a notification
}
}
导入org.altbeacon.beacon.BeaconManager;
导入org.altbeacon.beacon.Identifier;
导入org.altbeacon.beacon.RangeNotifier;
导入org.altbeacon.beacon.Region;
导入org.altbeacon.beacon.beacon;
导入org.altbeacon.beacon.Beaconsumer;
导入org.altbeacon.beacon.BeaconManager;
导入org.altbeacon.beacon.BeaconParser;
导入org.altbeacon.beacon.MonitorNotifier;
导入org.altbeacon.beacon.RangeNotifier;
导入org.altbeacon.beacon.Region;
公共类MainActivity扩展活动实现BeaconConsumer,
测距器{
//添加所有未实现的方法
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager=beaconManager.getInstanceForApplication(此应用程序);
beaconManager.bind(这个);
} 
@凌驾
公共范围信标区域(收集信标,
最终区域(区域){
信标=(信标)迭代器。下一步();
uuid=beacon.getId1().touidstring();
major=beacon.getId2().toInt();
//获取您的id并与ble按钮匹配。如果匹配,则可以调用您的方法或发送通知
}
}

在正确连接到可扩展设备时,您跳过了很多步骤

假设您已正确设置连接并调用OnServicesDiscovery,则在发现服务后,您需要启用所需特性的通知:

characteristic = gatt.getService(UUID.fromString(SERVICE_UUID)).getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID)); //Find you characteristic
mGatt.setCharacteristicNotification(characteristic, true); //Tell you gatt client that you want to listen to that characteristic
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors(); //find the descriptors on the characteristic
BluetoothGattDescriptor descriptor = descriptors.get(1); //get the right descriptor for setting notifications
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(descriptor); //apply these changes to the ble chip to tell it we are ready for the data
characteristic=gatt.getService(UUID.fromString(SERVICE_UUID)).getCharacteristic(UUID.fromString(characteristic_UUID))//发现你的特点
mGatt.setCharacteristicNotification(特征,真)//告诉你的关贸总协定客户,你想听听这个特点
列表描述符=characteristic.getDescriptors()//查找特征上的描述符
BluetoothGattDescriptor描述符=描述符.get(1)//获取用于设置通知的正确描述符
描述符.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(描述符)//将这些更改应用于ble芯片,告诉它我们已准备好接收数据

然后,每当特征发生变化时,就会调用onCharacteristicChanged。

您需要发现您的信标,然后检查此BLE按钮的UUID,如果匹配,则触发onCharacteristicChanged()或发送通知。感谢您的快速重播,但我不明白如何才能做到这一点。我能够连接设备ble,但是在我找不到一种方法在描述符中获取这个ble的UUID之后。get()您应该得到数字0而不是1,因为可能只有1个描述符。