我的广播接收器不接收本地服务的广播

时间:2017-02-07 13:53:43

标签: android bluetooth-lowenergy android-broadcast android-service-binding

我是Android开发新手,目前我正在尝试修改此处https://developer.android.com/samples/BluetoothLeGatt/index.html找到的蓝牙LE接口的示例代码。
这个演示项目分为两个活动,但我想将其缩减为一个活动; DeviceControlActivity更改为“常规”Java类。 经过几次尝试后,我使用Device Control类的构造函数运行了BluetoothLeService:

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));
        }
    }
};

这里有什么问题?

2 个答案:

答案 0 :(得分:1)

我还不能评论,但它是否已注册?

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

P.S。如果您正在使用AppCompat库并且不需要广播来离开您的应用,请同时查看LocalBroadcastManager

答案 1 :(得分:0)

谢谢RobK,
实际上我没有注册广播。注册代码不在示例项目活动的onCreate()方法中,我在这里考虑了设置服务所需的一切,但是在onResume()方法中。

添加

registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null) {
        final boolean result = mBluetoothLeService.connect(mDeviceAddress);
        Log.d(TAG, "Connect request result=" + result);
    }

到构造函数和一个额外的自定义意图过滤器

    private static IntentFilter makeGattUpdateIntentFilter() {
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
    intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
    return intentFilter;
}

为我解决了。