I am new to Android and I am trying to understand the working of Bluetooth connection. For that I have used used the wiced sense app to understand the working.
Here I want to connect to a particular device with respect to their MAC address. I have managed to store and retrieve the MAC address through Shared preferences. And now I want to connect to device which matches the MAC address without user interaction.
To store the MAC address I do the following:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.findViewById(R.id.device_name) == null) {
convertView = mInflater.inflate(R.layout.devicepicker_listitem, null);
holder = new ViewHolder();
String DeviceName;
holder.device_name = (TextView) convertView.findViewById(R.id.device_name);
// DeviceName= String.valueOf((TextView) convertView.findViewById(R.id.device_name));
holder.device_addr = (TextView) convertView.findViewById(R.id.device_addr);
holder.device_rssi = (ProgressBar) convertView.findViewById(R.id.device_rssi);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DeviceRecord rec = mDevices.get(position);
holder.device_rssi.setProgress(normaliseRssi(rec.rssi));
editor = PreferenceManager.getDefaultSharedPreferences(mContext);
String deviceName = rec.device.getName();
if (deviceName != null && deviceName.length() > 0) {
holder.device_name.setText(rec.device.getName());
holder.device_addr.setText(rec.device.getAddress());
//Log.i(TAG, "Service onStartCommand");
if(deviceName.equals("eVulate")&& !editor.contains("MAC_ID")) {
storeMacAddr(String.valueOf(rec.device.getAddress()));
}
} else {
holder.device_name.setText(rec.device.getAddress());
holder.device_addr.setText(mContext.getResources().getString(R.string.unknown_device));
}
return convertView;
public void storeMacAddr(String MacAddr) {
editor.edit().putString("MAC_ID", MacAddr).commit();
}
}
I retrive the same through the following code :
private void initDevicePicker() {
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if(mSharedPreference.contains("MAC_ID")){
String value=(mSharedPreference.getString("MAC_ID", ""));
}
else
// search for devices
}
I want to start a service after retrieving the MAC address and I don't know where exactly to do that. Any kind of help is appreciated.