I was learning about Bluetooth in android. I came across Bluetooth LE I tried to implement it by following the Android docs it does not seem to work.
I am actually confused about Bluetooth LE. How is it different from the normal Bluetooth. I did read in the docs which says it is used to connect to fitness devices etc does this mean the normal bluetooth implemetation does not allow us to connect to fitness devices ?
Can some one please explain this ?. Thank in advance :)
Here is the code which I have written for Bluetooth LE
MainActivity.java
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothManager mBluetoothManager;
private LeDeviceListAdapter mLeDeviceListAdapter;
private boolean mScanning, mBluetoothEnable;
private Handler mHandler = new Handler();
private ListView mListView;
//Request code to enable Bluetooth
private static final int REQUEST_BT_ENABLE = 9990;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), mLeDeviceListAdapter.getNumberOfDevicesFound(), 0).show();
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
mListView = (ListView) findViewById(R.id.listview);
mListView.setAdapter(mLeDeviceListAdapter);
//Check if the device supports BLE Devices
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_supported, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
}
//BluetoothManager is supported from Android 4.3 (API Level 18) and above ONLY.
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//Get the bluetooth adapter.
mBluetoothAdapter = mBluetoothManager.getAdapter();
}
@Override
protected void onResume() {
super.onResume();
if(!mBluetoothAdapter.isEnabled()){//bluetooth not enabled
mBluetoothEnable = false;
// Ensures Bluetooth is available on the device and it is enabled. If not, displays a dialog requesting user permission to enable Bluetooth.
makeSureBluetoothIsEnabled();
}else{//bluetooth is enabled
mBluetoothEnable = true;
}
scanLeDevices(mBluetoothEnable);
}
private void makeSureBluetoothIsEnabled(){
//If bluettoth is not enabled, enable it
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_BT_ENABLE);
}
private void scanLeDevices(boolean enable){
if(enable){
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
Toast.makeText(getApplicationContext(), "Stopped: "+mLeDeviceListAdapter.getNumberOfDevicesFound(), 0).show();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){ ///SUCCESSFULL
switch (requestCode) {
case REQUEST_BT_ENABLE:
mBluetoothEnable = true;
Toast.makeText(this, R.string.bt_enable, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
LeDeviceListAdapter.java
public class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter(Activity activity) {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = activity.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public int getNumberOfDevicesFound(){
return mLeDevices.size();
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(position);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
static class ViewHolder{
TextView deviceAddress;
TextView deviceName;
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le"
android:required="false"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>