gpt4 book ai didi

android - 我的广播接收器不接收来自本地服务的广播

转载 作者:行者123 更新时间:2023-11-30 00:43:59 25 4
gpt4 key购买 nike

我是 android 开发的新手,目前我正在尝试修改此处找到的蓝牙 LE 接口(interface)的示例代码 https://developer.android.com/samples/BluetoothLeGatt/index.html .
该演示项目分为两个 Activity ,但我想将其缩减为一个 Activity ; DeviceControlActivity 更改为“常规”Java 类。几次尝试后,我使用我的设备控制类的构造函数运行了 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);
}

但是,它们从未到达我最初将服务绑定(bind)到的 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 注册广播接收器。方法。

附言如果您正在使用 AppCompat 库并且不需要广播离开您的应用程序,也可以查看 LocalBroadcastManager .

关于android - 我的广播接收器不接收来自本地服务的广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42091621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com