BLE's connectGatt () method doesn't report anything to BluetoothGattCallback ....?

Can someone please check the problem with this code. I have implemented the service and passed an object to it BluetoothDevice

(in the object BluetoothDevice

passing through the intent) no error / problem. Then in onStartCommand()

I call deviceToConnect.connectGatt(this,false,mGattCallback)

. But mine BluetoothGattCallback()

doesn't work (don't print anything). The code is simple and straightforward. Can someone help me debug it.

EDIT: I am using Le device Scan in MainActivity()

and passing the device object to the service connected to the device.

public class PairedBleService extends Service
{
    private BluetoothGatt mConnectedGatt;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);

        BluetoothDevice deviceToConnect = (BluetoothDevice) intent.getParcelableExtra(DEVICE_TO_CONNECT);
        mConnectedGatt = deviceToConnect.connectGatt(this, false, mGattCallback);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service End", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral connected", Toast.LENGTH_LONG).show();
        } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral Disconnected", Toast.LENGTH_LONG).show();
        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            gatt.disconnect();
        }
    }
}

      

Edit: I tried to connect my beacon (Peripheral) with a standard Android phone and / or wherever I can establish a connection. But there it asks for a pairing pin and as soon as it sets the pin it is connected and shown in the bluetooth device. Is there any method, for example connectGatt

, where we can ask "pairing contacts" with a user ... I can't figure out what's missing.

+3


source to share


2 answers


Your onBind method is returning null and hence your main activity cannot communicate with your service.

Your code should be listed below,

@PairedBleService



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

@Override
public IBinder onBind(Intent intent) 
{
    // TODO Auto-generated method stub
    return mBinder;
};

private final IBinder mBinder = new LocalBinder();

      

@Primary activity

//put this inside onServiceConnected

PairedBleService bleService = ((PairedBleService.LocalBinder) service).getService();

// Your code for connecting with BLE device
....................................
....................................

      

+1


source


Editing really helps. You want to pair from the app ...

BluetoothDevice

has a method createBond()

since API level 19 (4.4), so this excludes 4.3, which was the first version of BLE for Android, but there aren't many left.



To do this, open the PIN-code dialog for the user. After successfully connecting the device, you can connect. To find out if the stapling is completed or not, you need to register BroadcastReceiver

for listening ACTION_BOND_STATE_CHANGED

. The incoming Intent

has EXTRA_BOND_STATE

, which can be BOND_BONDED

, BOND_BONDING_

or BOND_NONE

.

So, in onStartCommand

you want getBondState()

, and if it is BOND_NON

, you want createBond()

. If it is BOND_BONDING

, you need to wait for the connection with BroadcastReceiver

and connect after connecting it. If it is BOND_BONDED

, you can connect.

+1


source







All Articles