Hie all, i am working on Bluetooth connections and to do that i have device address and i want send it to a service which handle Bluetooth connections
i want to send string(device address) from activity to service (Android)
Code In ACTIVITY CLASS:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
startService(new Intent(CallBluetooth .this,BluetoothService.class));
Intent myIntent = new Intent(CallBluetooth.this, BluetoothService.class);
Bundle bundle = new Bundle();
CharSequence data1 = "abc";
bundle.putCharSequence("extraData",data1);
myIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getService(CallBluetooth.this, 0, myIntent, 0);
/*BluetoothService s = new BluetoothService();
s.deviceAddress(address);*/
// Attempt to connect to the device
// mChatService.connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
openOptionsMenu();
} else {
// User did not enable Bluetooth or an error occured
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
}
Code In SERVICE CLASS:
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Bundle bundle = intent.getExtras();
System.out.println("*******************"+"InsideOnStart");
if(bundle != null)
{
CharSequence data = (String) bundle.getCharSequence("extraData");
System.out.println("*******************"+data);
}
}