Can somebody please check the problem with this code. I have implemented a service and passes a BluetoothDevice
object to it (There is no error/Problem in BluetoothDevice
object passing through intent). Then, in the onStartCommand()
, i am calling deviceToConnect.connectGatt(this,false,mGattCallback)
. But my BluetoothGattCallback()
is not working (Not printing anything). Code is simple and straight forward. Can somebody help me to debug it.
EDIT: i am doing Le device Scan in MainActivity()
and passing device object to a service to connect 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 standard android bluetooth s/w and there i am able to make connection. But there it is asking for Pairing pin and once putting the pin it is connected and shown in Paired bluetooth device. Is there any method like connectGatt
where we can ask "pairing pin" to the user... I am unable to understand what i am missing.
Your onBind method is returning null and hence your main activity is not able to communicate with your service.
Your code should be as per the 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();
@ Main Activity
//put this inside onServiceConnected
PairedBleService bleService = ((PairedBleService.LocalBinder) service).getService();
// Your code for connecting with BLE device
....................................
....................................
The edit does help. You want to pair from within the app...
BluetoothDevice
has a createBond()
method as of API level 19 (4.4) so this leaves out 4.3 which was the first BLE Android version, but there aren't many of those left.
That should pop up the PIN dialog for the user. Once the device is successfully bonded you can then connect. To know when bonding has completed or not you need to have a BroadcastReceiver
that is registered to listen to ACTION_BOND_STATE_CHANGED
. The Intent
that comes in has EXTRA_BOND_STATE
which can be BOND_BONDED
, BOND_BONDING_
, or BOND_NONE
.
So in your onStartCommand
you want to getBondState()
and if it is BOND_NON
you want to createBond()
. If it is BOND_BONDING
you need to wait for the bonding with the BroadcastReceiver
and connect once it is bonded. If it is BOND_BONDED
then you can connect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With