我正在尝试从服务向我的主要活动发送广播。由于某些原因,当我致电sendBroadcast时,会收到以下警告。

02-26 14:55:40.615  11079-11090/com.example.sdp11.wmd W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:372)
        at com.example.sdp11.wmd.BluetoothLEService.broadcastUpdate(BluetoothLEService.java:148)
        at com.example.sdp11.wmd.BluetoothLEService.access$100(BluetoothLEService.java:28)
        at com.example.sdp11.wmd.BluetoothLEService$1.onServicesDiscovered(BluetoothLEService.java:94)
        at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:301)
        at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:215)
        at android.os.Binder.execTransact(Binder.java:412)
        at dalvik.system.NativeStart.run(Native Method)


我用来调用sendBroadcast函数的方法如下。

private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}


在我的蓝牙回调中调用了上述方法:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        //Connection established
        if (status == BluetoothGatt.GATT_SUCCESS
                && newState == BluetoothProfile.STATE_CONNECTED) {

            Log.e(TAG, "Connected Successfully");
            //Discover services
            gatt.discoverServices();

        } else if (status == BluetoothGatt.GATT_SUCCESS
                && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.e(TAG, "Disconnected");
            //Handle a disconnect event
        }

        else {
            Log.e(TAG, "Connection state changed.  New state: " + newState);
        }
    }

    @Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.e(TAG, "Services discovered");
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.e(TAG, "Error, onServicesDiscovered received status: " + status);
        }
    }
}


任何帮助将不胜感激。

编辑

这是在我的主要活动的onCreate方法中:

public static BluetoothLEService mBluetoothLEService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buildGoogleApiClient();
    createLocationRequest();

    mBluetoothLEService = new BluetoothLEService();
    Intent gattServiceIntent = new Intent(this, BluetoothLEService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLEService = ((BluetoothLEService.LocalBinder) service).getService();
        if (!mBluetoothLEService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        // Automatically connects to the device upon successful start-up initialization.
        //mBluetoothLEService.connect(mDeviceAddress);
        Log.e(TAG, "Service Connected");
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBluetoothLEService = null;
    }
};

最佳答案

我终于找到了问题。我在视图分页器的选项卡中使用片段。我从片段的onCreate方法的主要活动中获取mBluetoothLEService的实例。问题在于,此mBluetoothLEService此时仍为null。我最终只使用了片段中的MainActivity.mBluetoothLEService.broadcastUpdate()。

10-04 18:07