-1

i have two problem.I want connect my ble service .I can connect but i cant disconnect serive when i use mService.disconnect(); my app will crash.and other problem is i cant send value to RXchar...... All problem are dispaly on a null object reference this is disconnect error enter image description here

RxChar error

enter image description here

my code _ble_fragment

 switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            try {
                //send data to service
                if (isChecked) {
                    message = "1";
                } else {
                    message = "0";
                }
                value = message.getBytes("UTF-8");
                sendText.setText(message);
                mService.writeRXCharacteristic(value);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
 scanButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("click", "按下去了");
            if (!mBtAdapter.isEnabled()) {
                Log.i(TAG, "onClick - BT not enabled yet");
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            } else {
                if (scanButton.getText().equals("Connect")) {
                    Log.i("scan", "open device");
                    switch1.setEnabled(true);
                    //Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices
                    Intent newIntent = new Intent(getActivity(), DeviceListActivity.class);
                    startActivity(newIntent);
                } else {
                    //Disconnect button pressed

                       mService.disconnect();
                        Log.i("Diconnect", "Disconnect Ble");
                        // mHandler.removeCallbacks(runnable);

                }
            }
        }
    });

DeviceList & UartService code:

public void writeGatt(BluetoothDevice dev){
    byte[] msgBuffer;  //bluetooth send alway byte
    String message = "1"; //ON
    msgBuffer = message.getBytes();


    mBtGatt = dev.connectGatt(this,false,GattCallback);//call Gattcallback

}

private final BluetoothGattCallback GattCallback = new BluetoothGattCallback() { //
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            broadcastUpdate(intentAction);

            Log.i(TAG, "Connected to GATT server.");
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBtGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Disconnected from GATT server.");
        }

    }
    @Override
    // New services discovered
    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
    // Result of a characteristic read operation
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            //broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }
};

public void disconnect() {  //deisconnect
    if (mBtGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");

        return;
    }
   mBtGatt.close();
    mBtGatt = null;
    // mBluetoothGatt.close();
}
private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    Log.i("broadcastUpdate", "run broadcastUpdate send " + intent);
}

public void close() {
    if (mBtGatt == null) {
        return;
    }
    Log.w(TAG, "mBluetoothGatt closed");
    ;
    mBtGatt.close();
    mBtGatt = null;
}

public void writeRXCharacteristic(byte[] value)
{

   /* BluetoothGattService RxService = mBtGatt.getService(RX_SERVICE_UUID);
    //showMessage("mBluetoothGatt null"+ mBluetoothGatt);
    if (RxService == null) {
        //showMessage("Rx service not found!");
       // broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        Log.i("RxService is null!!","RxService is null!!");
        return;
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
       // showMessage("Rx charateristic not found!");
        //broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        Log.i("RxCHAR is null!!","RxCHAR is null!!");
        return;
    }

        RxChar.setValue(value);
        boolean status = mBtGatt.writeCharacteristic(RxChar);
        Log.d(TAG, "write TXchar - status=" + status);*/
    BluetoothGattService RxService=null;
    while( RxService==null)  {
        try{
            RxService = mBtGatt.getService(RX_SERVICE_UUID);}
        catch(Exception e){
            Log.d(TAG, e.toString());}
    }
    if (RxService == null) {
        Log.d(TAG, "1");
        return;
    }BluetoothGattCharacteristic RxChar=null;
    while( RxChar ==null){
        try{
            RxChar = RxService.getCharacteristic(RX_CHAR_UUID);}
        catch(Exception e)
        {Log.d(TAG, e.toString());}
    }
    if (RxChar == null) {
        Log.d(TAG, "2");
        return;
    }
    RxChar.setValue(value);
    mBtGatt.writeCharacteristic(RxChar);



}

what can i do?

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
陳家麒
  • 1
  • 3
  • 1
    your `mService` is not **initialized properly**, please post the code where you initialize mService...and please dont post your stacktrace as an image. – Gueorgui Obregon Mar 13 '16 at 06:38
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Gueorgui Obregon Mar 13 '16 at 06:48
  • sorry I will be more careful next time.mService is `private DeviceListActivity mService= null;` is in DeviceList & UartService code: – 陳家麒 Mar 13 '16 at 08:04

1 Answers1

0

you can try this:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.isEnabled()){
     bluetoothAdapter.disable();
}

you need add two permission bellow:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
GiapLee
  • 436
  • 2
  • 8
  • HI,my AndroidManifest.xml already add ` `but this problem cant reslove – 陳家麒 Mar 13 '16 at 05:56
  • so you can try my code above. and if you only disconnect from background service but do not turn off ble then you only close ble socket connection which using to read/write data from/to other ble devices. – GiapLee Mar 13 '16 at 06:00
  • thx GiapLee but my project want discoonect button not disable to bluetoothAdapter – 陳家麒 Mar 13 '16 at 06:06