Android OnServiceConnected从不执行

Android OnServiceConnected从不执行,android,Android,我得到了下一个异常,当我调用服务中的方法时,当调试时,我看到onServiceConnected从不执行。我试着把日志放在里面,但它从来没有执行过,我已经厌倦了等待它 05-18 19:31:13.998 7031-7031/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.geochildfragment.app, PID: 7031 java.lang.NullPointerException at com.g

我得到了下一个异常,当我调用服务中的方法时,当调试时,我看到onServiceConnected从不执行。我试着把日志放在里面,但它从来没有执行过,我已经厌倦了等待它

05-18 19:31:13.998    7031-7031/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.geochildfragment.app, PID: 7031
java.lang.NullPointerException
        at com.geochildfragment.app.ActivityMain.sendPin(ActivityMain.java:249)
        at com.geochildfragment.app.FragmentLinkDevice$4.onClick(FragmentLinkDevice.java:224)
        at android.view.View.performClick(View.java:4626)
        at android.view.View$PerformClick.run(View.java:19293)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
这是我的活动,我调用sendPin方法,它从一个片段引发异常:

int bStatus;
Boolean connected = false;
BluetoothDevice bDevice;
private BLEService bleService;
BluetoothGattService gattService;
public static final String EXTRA_BLUETOOTH_DEVICE = "BT_DEVICE";
BluetoothAdapter bAdapter;
Context context;


private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        bleService = ((BLEService.LocalBinder) service).getService();
        if (!bleService.initialize()){
            finish();
        }
        bleService.context = context;
    }

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


private final BroadcastReceiver serviceUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BLEService.ACTION_GATT_CONNECTED.equals(action)) {
            connected = true;
            //invalidateOptionsMenu();
        } else if (BLEService.ACTION_GATT_DISCONNECTED.equals(action)) {
            connected = false;
            //invalidateOptionsMenu();
        } else if (BLEService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            List<BluetoothGattService> servicesList;
            servicesList = bleService.getSupportedGattServices();
            Iterator<BluetoothGattService> iter = servicesList.iterator();
            while (iter.hasNext()) {
                BluetoothGattService bService = (BluetoothGattService) iter.next();
                if (bService.getUuid().toString().equals(BLEUUID.SERVICE)){
                    gattService = bService;
                }
            }
        } else if (BLEService.ACTION_DATA_AVAILABLE.equals(action)) {
            ........

        }
    }
};

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

    Bundle extra = this.getIntent().getExtras();
    getConnectedDevices(extra);

    if (mServiceConnection == null){
        Log.v("NULL", "mServiceConnection NULL");
    }

    Intent gattServiceIntent = new Intent(this, BLEService.class);
    if (gattServiceIntent==null){
        Log.v("NULL", "mServiceConnection NULL");
    }
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    bStatus = BluetoothProfile.STATE_DISCONNECTED;
}

@Override
public void onDestroy() {
    super.onDestroy();
    unbindService(mServiceConnection);
    bleService = null;
}

@Override
public void onResume() {
    super.onResume();
    registerReceiver(serviceUpdateReceiver, makeGattUpdateIntentFilter());
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(serviceUpdateReceiver);
}

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

@Override
public void sendPin(BluetoothDevice bDevice, String pin) {
    String deviceAddress = bDevice.getAddress();
    bleService.connect(deviceAddress);
    bleService.sendPINCharacteristic(pin, bDevice);
}

public void verifyPIN(String data){
    FragmentLinkDevice f = (FragmentLinkDevice) getSupportFragmentManager().findFragmentById(R.id.link_device_fragment);
    if (data.contains(BroadcastIDs.OK)){
        f.launchDeviceConfigActivity()
    } else if (data.contains(BroadcastIDs.FAIL)){
        f.launchAlertDialog();
    }
}
私有最终BluetoothGattCallback mGattCallback=新BluetoothGattCallback(){

@覆盖
连接状态更改的公共无效(蓝牙gatt gatt、int状态、int新闻状态){
串接;
if(newState==BluetoothProfile.STATE\u CONNECTED){
意向=行动与关贸总协定连接;
mConnectionState=连接状态;
广播更新(意向);
Log.i(标记“连接到GATT服务器”);
//尝试在成功连接后发现服务。
Log.i(标记“正在尝试启动服务发现:”+
mBluetoothGatt.discoverServices());
}else if(newState==BluetoothProfile.STATE\u已断开连接){
intentAction=动作与关贸总协定断开;
mConnectionState=状态\断开连接;
Log.i(标签,“与GATT服务器断开连接”);
广播更新(意向);
设备设备=新设备();
设备=设备.FindByDeviceAddress(mBluetoothDeviceAddress);
//显示仪表通知(装置);
}
}
@凌驾
发现服务上的公共无效(Bluetooth gatt,int状态){
如果(状态==蓝牙GATT.GATT\U成功){
for(BluetoothGattService服务:gatt.getServices()){
if((service==null)| |(service.getUuid()==null)){
继续;
}
if(BLEUUID.SERVICE.equalsIgnoreCase(SERVICE.getUuid().toString()){
mService=服务;
}
}
广播更新(发现行动、关税及贸易总协定服务);
}否则{
Log.w(标记“OnServicesDiscovery已接收:+状态);
}
}
@凌驾
特征读取的公共无效(蓝牙gatt、蓝牙gatt特征特征、国际状态){
如果(status==BluetoothGatt.GATT_SUCCESS){
广播更新(动作数据可用,特征);
广播更新(额外数据、特性);
}
}
@凌驾
特征变更后的公共无效(蓝牙gatt、蓝牙gatt特征特征){
阅读特征(特征);
广播更新(动作数据可用,特征);
}
@凌驾
public void onDescriptorWrite(蓝牙gatt、蓝牙GATTDescriptor描述符、int状态){
如果(状态==蓝牙GATT.GATT\U成功){
Toast.makeText(上下文,“onDescriptorWrite:SUCCESS”,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(上下文,“onDescriptorWrite:FAILURE”,Toast.LENGTH_LONG.show();
}
}
};
私有无效广播更新(最终字符串操作){
最终意图=新意图(行动);
发送广播(意图);
}
@TargetApi(Build.VERSION\u code.JELLY\u BEAN\u MR2)
私有无效广播更新(最终字符串操作、最终BluetoothGattCharacteristic特征){
最终意图=新意图(行动);
if(PIN_CHARACTERISTIC.equals(CHARACTERISTIC.getUuid())){
最终字符串引脚=特征。getStringValue(0);
intent.putExtra(EXTRA_数据,BroadcastIDs.PIN+String.valueOf(PIN));
断开连接();
}
发送广播(意图);
}
公共类LocalBinder扩展了Binder{
公共服务getService(){
返回BLEService.this;
}
}
@凌驾
公共IBinder onBind(意向){
返回mBinder;
}
@凌驾
公共布尔onUnbind(意图){
close();
返回super.onUnbind(intent);
}
private final IBinder mBinder=new LocalBinder();
公共布尔初始化(){
if(mBluetoothManager==null){
mBluetoothManager=(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
if(mBluetoothManager==null){
返回false;
}
}
mBluetoothAdapter=mBluetoothManager.getAdapter();
if(mBluetoothAdapter==null){
返回false;
}
返回true;
}
公共布尔连接(最终字符串地址){
if(mBluetoothAdapter==null | | address==null){
返回false;
}
//以前连接的设备。请尝试重新连接。
if(mBluetoothDeviceAddress!=null&&address.equals(mBluetoothDeviceAddress)
&&mBluetoothGatt!=null){
if(mBluetoothGatt.connect()){
mConnectionState=连接状态;
返回true;
}否则{
返回false;
}
}
最终BluetoothDevice=mBluetoothAdapter.getRemoteDevice(地址);
如果(设备==null){
返回false;
}
mbluetothgatt=device.connectGatt(this,false,mGattCallback);
mBluetoothDeviceAddress=地址;
mConnectionState=连接状态;
返回true;
}
公共空间断开连接(){
if(mBluetoothAdapter==null | | mBluetoothGatt==null){
返回;
}
mBluetoothGatt.disconnect();
}
公众假期结束(){
if(mBluetoothGatt==null){
返回;
}
mBluetoothGatt.close();
mBluetoothGatt=null;
}
public void readCharacteristic(蓝牙特征){
if(mBluetoothAdapter==null | | mBluetoothGatt==null){
返回;
}
mbluetothgatt.readCharacteristic(特征);
Log.i(“阅读”,“莱达特色菜”);
}
公共void setCharacteristicNotification(BluetoothGattCharacteristic特征,启用布尔值){
if(mBluetoothAdapter==null | | mBluetoothGatt==null){
返回;
}
MBlueTothGatt.setCharacteristicNotification(特征,已启用);
    <service android:name="Bluetooth.BLEService" android:enabled="true"/>
public class BLEService extends Service {

private static int DELAY = 3000;

private final static String TAG = BLEService.class.getSimpleName();
public Context context;
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private String mBluetoothDeviceAddress;
private BluetoothGatt mBluetoothGatt;
private int mConnectionState = STATE_DISCONNECTED;
BluetoothGattService mService;
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;


public final static String ACTION_GATT_CONNECTED ="com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED ="com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED ="com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE ="com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA ="com.example.bluetooth.le.EXTRA_DATA";


public final static String SERVICE = BLEUUID.SERVICE;
public final static UUID PIN_CHARACTERISTIC = UUID.fromString(BLEUUID.PIN_CHARACTERISTIC_UUID);
public final static UUID PUK_CHARACTERISTIC = UUID.fromString(BLEUUID.PUK_UUID);
public final static UUID INTERVAL_CHARACTERISTIC = UUID.fromString(BLEUUID.INTERVAL_UUID);
public final static UUID ROUTE_INTERVAL_CHARACTERISTIC = UUID.fromString(BLEUUID.ROUTE_INTERVAL_UUID);
public final static UUID ON_OFF_CHARACTERISTIC = UUID.fromString(BLEUUID.ON_OFF_UUID);
public final static UUID GPS1_CHARACTERISTIC= UUID.fromString(BLEUUID.GPS1_UUID);
public final static UUID GPS2_CHARACTERISTIC= UUID.fromString(BLEUUID.GPS2_UUID);
public final static UUID BATTERY_CHARACTERISTIC = UUID.fromString(BLEUUID.BATTERY_LEVEL_UUID);
public final static UUID DEVICE_NAME_CHARACTERISTIC = UUID.fromString(BLEUUID.DEVICE_NAME_UUID);
    @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);
            Devices device = new Devices();
            device = device.FindByDeviceAddress(mBluetoothDeviceAddress);
            //showPerimeterNotification(device);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            for (BluetoothGattService service : gatt.getServices()) {
                if ((service == null) || (service.getUuid() == null)) {
                    continue;
                }
                if (BLEUUID.SERVICE.equalsIgnoreCase(service.getUuid().toString())) {
                    mService = service;
                }
            }
            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);
            broadcastUpdate(EXTRA_DATA, characteristic);

        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
        readCharacteristic(characteristic);
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
        if (status== BluetoothGatt.GATT_SUCCESS){
            Toast.makeText(context, "onDescriptorWrite: SUCCESS", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, "onDescriptorWrite: FAILURE", Toast.LENGTH_LONG).show();
        }
    }
};

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

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void broadcastUpdate(final String action,final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    if (PIN_CHARACTERISTIC.equals(characteristic.getUuid())) {
        final String pin = characteristic.getStringValue(0);
        intent.putExtra(EXTRA_DATA, BroadcastIDs.PIN + String.valueOf(pin));
        disconnect();

    }
    sendBroadcast(intent);
}

public class LocalBinder extends Binder {
    public BLEService getService() {
        return BLEService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
    close();
    return super.onUnbind(intent);
}

private final IBinder mBinder = new LocalBinder();

public boolean initialize() {
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            return false;
        }
    }
    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        return false;
    }
    return true;
}

public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        return false;
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        return false;
    }

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

public void disconnect() {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.disconnect();
}

public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;

}

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.readCharacteristic(characteristic);
    Log.i("READ", "CARACTERISTICA LEIDA");
}

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    if (PIN_CHARACTERISTIC.equals(characteristic.getUuid())){
        /*BluetoothGattDescriptor descriptor = characteristic.getDescriptor
                (UUID.nameUUIDFromBytes(BLEUUID.fromHexToString(BLEUUID.PIN_CHARACTERISTIC_CONFIG_DESCRIPTOR)));*/
        BluetoothGattDescriptor descriptor =
                new BluetoothGattDescriptor(UUID.fromString(BLEUUID.CONFIG_UUID),BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);

        //descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

public List<BluetoothGattService> getSupportedGattServices() {
    if (mBluetoothGatt == null) return null;
    return mBluetoothGatt.getServices();
}


public void sendPINCharacteristic(String pin, BluetoothDevice device){

    byte[] pinByte = pin.getBytes();
    int pinInt = Integer.valueOf(pin);

    connect(device.getAddress());

    final BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) mService.getCharacteristic(UUID
            .fromString(BLEUUID.PIN_CHARACTERISTIC_UUID));

    ch.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    ch.setValue(pin);

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    setCharacteristicNotification(ch, true);
    mBluetoothGatt.writeCharacteristic(ch);        
}