我的广播接收器未从本地服务接收广播

荣誉市民

我是android开发的新手,目前正在尝试修改https://developer.android.com/samples/BluetoothLeGatt/index.html上的Bluetooth LE接口的示例代码
该演示项目分为两个活动,但我想将其简化为一个活动。将DeviceControlActivity更改为“常规” Java类。经过几次尝试,我使BluetoothLeService与我的Device Control类的构造函数一起运行:

public DeviceControl (BluetoothDevice device, Context context) {
    mDeviceName = device.getName();
    mDeviceAddress = device.getAddress();
    this.context = context;

    Log.d(TAG, "DeviceControl construktor");
    Log.d(TAG, "Context = " + context);

    Intent gattServiceIntent = new Intent(context, BluetoothLeService.class);
    context.bindService(gattServiceIntent, mServiceConnection, context.BIND_AUTO_CREATE);

}

BluetoothLeService类已与Log.d()一起使用,并且似乎可以正常工作。但是,它在此处发出的广播是:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            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) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
};

private void broadcastUpdate(final String action) {
    Log.d(TAG, "Methode broadcastUpdate aufgerufen. Action = " + action);
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

但是,它们从未到达我最初将服务绑定到的DeviceControl类:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Broadcast received");
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {

        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {

        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            System.out.println("GATT-services discovered");
            displayGattServices(mBluetoothLeService.getSupportedGattServices());
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
           displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }
    }
};

怎么了

罗布

我还不能发表评论,但已注册吗?

您必须使用registerReceiver方法注册广播接收器

PS:如果您使用的是AppCompat库,并且不需要广播就可以离开您的应用程序,请同时查看LocalBroadcastManager

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

广播接收器或服务?

来自分类Dev

广播接收器

来自分类Dev

从广播接收器启动服务

来自分类Dev

片段中的广播接收器无法从服务接收意图

来自分类Dev

广播接收器vs唤醒广播接收器

来自分类Dev

我的广播接收器未接收到

来自分类Dev

接收并注册广播接收器

来自分类Dev

我们可以使用本地广播接收器在活动和服务之间进行通信吗?

来自分类Dev

广播接收器未从Google SMS检索器API接收SMS

来自分类Dev

Android 广播接收器未接收显式广播?

来自分类Dev

Android-广播接收器和服务之间的区别

来自分类Dev

检查服务是否正在从广播接收器运行

来自分类Dev

如何在服务中使用广播接收器

来自分类Dev

服务广播接收器后无响应

来自分类Dev

如何管理服务中的广播接收器

来自分类Dev

如何在服务中使用广播接收器

来自分类Dev

如何在广播接收器中启动服务?

来自分类Dev

服务和广播接收器,以检查网络是否可用

来自分类Dev

向广播接收器发送未绑定的服务消息

来自分类Dev

为什么广播接收器不调用服务

来自分类Dev

对象内部或服务内部的广播接收器?

来自分类Dev

我怎么知道何时从广播接收器与Android调用我的服务?

来自分类Dev

广播接收器未发布我的数据

来自分类Dev

我的通知无法播放广播接收器

来自分类Dev

广播接收器未发布我的数据

来自分类Dev

我们可以通过广播接收器绑定活页夹服务吗?

来自分类Dev

我应该如何订阅广播接收器来监听位置服务的变化

来自分类Dev

服务停止后,服务中的广播接收器仍在广播

来自分类Dev

使用清单注册广播接收器