蓝牙开发的过程中,想手动实现连接蓝牙的操作,但是总是有报异常
报出异常的是java.lang.NullPointerException:Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String) ' on a null object reference.
异常所在的函数的位置的语句如下:
private String DeviceAddress;
...
protected void onCreate(Bundle savedInstanceState){
...
Intent intent = getIntent();
DeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
}
btn_connect.setOnclickListener(new View.onClickListener(){
@Override
public void onClick(View v){
try{
//DeviceAddress是从一个界面fragment中利用intent传递过来的
//异常就在这一句
final BluetoothDevice bDevice = mBluetoothAdapter.getRemoteDevice(DeviceAddress);
mBluetoothGatt = bDevice.connectGatt(LeDeviceConnect.this,false,mBluetoothService.mGattCallback);
boolean btLeSerive = mBluetoothLeService.connect(DeviceAddress);
...
}catch(Exception e){
e.printStackTrace();
}
}
});
异常每次到这里就会抛出来,为了不让程序闪退,就用try-catch了,结果还不知道怎么去改,
地址传不进去,onClick里面的语句就没有办法继续运行,求大神给个方向