Java 蓝牙GATT试图调用虚拟方法';void android.content.Context.sendBroadcast(android.content.Intent)和#x27;关于空对象引用
我正在编写我的第一个android应用程序,但已经失败了——我对蓝牙GATT读写的可靠性产生了很大的质疑。30分钟前,我能够将手机中的一个字节写入外设,并看到它被接收。现在我不能。我开始认为这个间歇性错误是导致问题的原因:Java 蓝牙GATT试图调用虚拟方法';void android.content.Context.sendBroadcast(android.content.Intent)和#x27;关于空对象引用,java,android,bluetooth,Java,Android,Bluetooth,我正在编写我的第一个android应用程序,但已经失败了——我对蓝牙GATT读写的可靠性产生了很大的质疑。30分钟前,我能够将手机中的一个字节写入外设,并看到它被接收。现在我不能。我开始认为这个间歇性错误是导致问题的原因: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:376)
at com.znewsham.skiday.adapters.BluetoothLeService.broadcastUpdate(BluetoothLeService.java:152)
at com.znewsham.skiday.adapters.BluetoothLeService.access$100(BluetoothLeService.java:50)
at com.znewsham.skiday.adapters.BluetoothLeService$1.onServicesDiscovered(BluetoothLeService.java:118)
at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:304)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:217)
at android.os.Binder.execTransact(Binder.java:454)
我从android开发人员示例中获得了大部分代码,但根据需要对其进行了修改。如果我在此处删除对broadcastUpdate的调用,或将其包装在一个try块中,则错误将发生在不同的位置(例如OnServicesDiscoveryd),但该错误仅出现一次
这是我绑定服务的地方:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == DEVICE_SCAN){
address = data.getStringExtra("address");
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
startService(gattServiceIntent);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
boolean connected;
do {
bluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
bluetoothLeService.attachBase(ViewSkiDayActivity.this);
if (!bluetoothLeService.initialize()) {
Log.e("", "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
connected = bluetoothLeService.connect(address);
}while(connected == false);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
bluetoothLeService = null;
}
};
这是服务:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == DEVICE_SCAN){
address = data.getStringExtra("address");
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
startService(gattServiceIntent);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
boolean connected;
do {
bluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
bluetoothLeService.attachBase(ViewSkiDayActivity.this);
if (!bluetoothLeService.initialize()) {
Log.e("", "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
connected = bluetoothLeService.connect(address);
}while(connected == false);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
bluetoothLeService = null;
}
};
以下是发生错误的回调处理程序:
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;
try{
broadcastUpdate(intentAction);
}
catch(Exception e){
}
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.");
try{
broadcastUpdate(intentAction);
}
catch(Exception e){
}
}
}
@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, final BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.w("BLARG", "write callback");
}
};
广播更新:
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
我非常困惑,我甚至不确定解决这个问题是否能解决我的问题我不知道这是否能帮助您,但我收到了完全相同的错误消息。关于我的问题的一些上下文:我在我的自定义对象中的活动之外调用“sendBroadcast”。我可以通过替换以下内容来解决此问题:
sendBroadcast(intent);
为此:
context.sendBroadcast(intent);
不用说,当活动第一次实例化所述对象时,通过将上下文传递给构造函数中的对象,我在对象中保留了对上下文的引用:
new MyObject(this);
希望它能帮助您。广播更新在哪里方法代码?如果您只需要应用程序内部广播,请不要使用
sendBroadcast(intent)
。为了提高效率和安全性,请更好地使用LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
我最终修复了它,但我不知道它是如何消失的,我将回顾我的问题并比较源代码,看看是否可以缩小范围。谢谢你的回答,你有没有想过?我有一个类似的问题,我想知道您发现了什么将sendBroadcast(intent)更改为上下文。sendBroadcast(intent)确实解决了问题>>>我在活动之外调用“sendBroadcast”这是一个奇怪的说法。无法从任意对象调用sendBroadcast,因为此方法是上下文类的成员