Android Open Source - BLEService Characteristics Activity






From Project

Back to project page BLEService.

License

The source code is released under:

Copyright (c) 2014, Ratio LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project BLEService listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ratio.btdemo;
//w ww.ja  va  2  s  . c o m
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import com.ratio.btdemo.adapters.CharacteristicsListAdapter;
import com.ratio.deviceService.BTCharacteristicProfile;
import com.ratio.deviceService.BTServiceProfile;
import com.ratio.deviceService.DeviceService;
import com.ratio.deviceService.IDeviceCommand;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class CharacteristicsActivity extends Activity {
  protected final static String       TAG = CharacteristicsActivity.class.getSimpleName();
  public static final String        EXTRA_DEVICE = "device";
  public static final String        EXTRA_SERVICE = "service";
    public static final String         PREFS = "BT_PREFS";
  public static final String         RSSI_REFRESH_MSEC = "rssi_refresh_msec";
  public static final int          RSSI_REFRESH_MSEC_DEFAULT = 2000;
  
  protected boolean            mfScanning = false;
  protected IDeviceCommand         mService;            // service for device interface.
  protected DeviceServiceConnection     mConnection;          // connection to the device service.
  protected BluetoothDevice        mDevice;
  protected BTServiceProfile        mServiceProfile;
  protected CharacteristicsListAdapter  mAdapter;
  protected RSSIReceiver          mRSSIReceiver;
  protected ConnectionStateReceiver    mConnectionStateReceiver;
  protected CharactersticsReceiver    mCharacteristicsReceiver;
  protected Timer              mRSSITimer;
  protected TimerTask            mRSSITimerTask;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDevice = getIntent().getParcelableExtra(EXTRA_DEVICE);
    mServiceProfile = getIntent().getParcelableExtra(EXTRA_SERVICE);
    setContentView(R.layout.activity_characteristics);
    initializeDeviceService();
    mRSSITimer = new Timer();
  }  
  
  
  @Override
  public void onContentChanged() {
    TextView tvName = (TextView) findViewById(R.id.peripheral_name);
    tvName.setText(mDevice.getName());
    TextView tvAddress = (TextView) findViewById(R.id.peripheral_address);
    tvAddress.setText(mDevice.getAddress().toString());
       mAdapter = new CharacteristicsListAdapter(CharacteristicsActivity.this, mServiceProfile.getService().getCharacteristics());
       ListView characteristicsListView = (ListView) CharacteristicsActivity.this.findViewById(R.id.characteristics_list);
       characteristicsListView.setAdapter(mAdapter);
       characteristicsListView.setOnItemClickListener(new CharacteristicItemClickListener());
  }
  
  // set up the broadcast receivers for service and device discovery
  @Override
  public void onResume() {
    super.onResume();
    mRSSIReceiver = new RSSIReceiver();
    addReceiver(mRSSIReceiver, DeviceService.ACTION_READ_RSSI);
    mConnectionStateReceiver = new ConnectionStateReceiver();
    addReceiver(mConnectionStateReceiver, DeviceService.ACTION_CONNECTION_STATE);
    mCharacteristicsReceiver = new CharactersticsReceiver();
    addReceiver(mCharacteristicsReceiver, DeviceService.ACTION_GET_CHARACTERISTICS);
    
  }

  @Override
  public void onPause() {
    super.onPause();
    unregisterReceiver(mConnectionStateReceiver);
    unregisterReceiver(mRSSIReceiver);
    unregisterReceiver(mCharacteristicsReceiver);
  }
    
  @Override
  public void onDestroy() {
    super.onDestroy();
    unbindService(mConnection);
  }
  
  protected void addReceiver(BroadcastReceiver receiver, String action) {
    IntentFilter filter = new IntentFilter();
    filter.addAction(action);
    registerReceiver(receiver, filter);
  }
  
  private void initializeDeviceService() {
      mConnection = new DeviceServiceConnection();
      Intent i = new Intent(this, DeviceService.class);
      boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE);
      Log.d(TAG, "initService() bound with " + ret);
  }

  // interface to make requests from the device service. data is returned via broadcast receivers.
    private class DeviceServiceConnection implements ServiceConnection {

        public void onServiceConnected(ComponentName name, IBinder boundService) {
            mService = IDeviceCommand.Stub.asInterface((IBinder) boundService);
            Log.d(TAG, "onServiceConnected() connected");

          //mService.getCharacteristics(mDevice.getAddress(), mServiceProfile.mServiceUUID.toString());
          SharedPreferences settings = getSharedPreferences(PREFS, 0);
          mRSSITimerTask = new RSSITimerTask();
             long rssiRefreshMsec = settings.getLong(RSSI_REFRESH_MSEC, RSSI_REFRESH_MSEC_DEFAULT);
            mRSSITimer.schedule(mRSSITimerTask, 0, rssiRefreshMsec);
        }
            
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            Log.d(TAG, "onServiceDisconnected() disconnected");
        }
    }
   
    public class CharactersticsReceiver extends com.ratio.deviceService.receivers.CharactertisticListReceiver {
      @Override
      public void onCharacteristicList(String               deviceAddress, 
                       BluetoothGattService        service,
                       List<BluetoothGattCharacteristic>   charList) {
        Log.d(TAG, "ServicesDiscoveredReceiver address = " + deviceAddress);
           mAdapter = new CharacteristicsListAdapter(CharacteristicsActivity.this, charList);
           ListView characteristicsListView = (ListView) CharacteristicsActivity.this.findViewById(R.id.services_list);
           characteristicsListView.setAdapter(mAdapter);
           characteristicsListView.setOnItemClickListener(new CharacteristicItemClickListener());         
        }
    }
 
    public class RSSIReceiver extends com.ratio.deviceService.receivers.RSSIReceiver {
      @Override
      public void onRSSI(String deviceAddress, int rssi, int status) {
         TextView tvRssi = (TextView) findViewById(R.id.peripheral_rssi);
        tvRssi.setText(Integer.toString(rssi) + " db");       
       
      }
    }
    
    public class ConnectionStateReceiver extends com.ratio.deviceService.receivers.ConnectionStateReceiver {
      @Override
      public void onConnectionState(String deviceAddress, int state) {
           TextView tvState = (TextView) CharacteristicsActivity.this.findViewById(R.id.peripheral_status);
           tvState.setText(Integer.toString(state));      

      }
    }
    
    protected class RSSITimerTask extends TimerTask {
    @Override
    public void run() {
      try {
        mService.readRemoteRSSI(mDevice.getAddress());  
      } catch (RemoteException rex) {
        rex.printStackTrace();
      }
    }

    }
    
    public class CharacteristicItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
      Intent i = new Intent(CharacteristicsActivity.this, CharacteristicDetailsActivity.class);
      i.putExtra(CharacteristicDetailsActivity.EXTRA_DEVICE, mDevice);
      i.putExtra(CharacteristicDetailsActivity.EXTRA_SERVICE, mServiceProfile);
      BTCharacteristicProfile btCharProfile = new BTCharacteristicProfile(mAdapter.getCharacteristic(position));
      i.putExtra(CharacteristicDetailsActivity.EXTRA_CHARACTERISTIC, btCharProfile);
      startActivity(i);
    }
      
    }

  }




Java Source Code List

com.ratio.btdemo.BleDefinedUUIDs.java
com.ratio.btdemo.BleNamesResolver.java
com.ratio.btdemo.CharacteristicDetailsActivity.java
com.ratio.btdemo.CharacteristicsActivity.java
com.ratio.btdemo.ScanActivity.java
com.ratio.btdemo.ServicesActivity.java
com.ratio.btdemo.adapters.CharacteristicsListAdapter.java
com.ratio.btdemo.adapters.ServicesListAdapter.java
com.ratio.deviceService.BTCharacteristicProfile.java
com.ratio.deviceService.BTDescriptorProfile.java
com.ratio.deviceService.BTDeviceProfile.java
com.ratio.deviceService.BTLEDeviceManager.java
com.ratio.deviceService.BTServiceProfile.java
com.ratio.deviceService.BTUUID.java
com.ratio.deviceService.BluetoothBroadcastReceiver.java
com.ratio.deviceService.DeviceErrorCodes.java
com.ratio.deviceService.DeviceService.java
com.ratio.deviceService.command.BTLECommandDisconnect.java
com.ratio.deviceService.command.BTLECommandDiscoverServices.java
com.ratio.deviceService.command.BTLECommandReadCharacteristic.java
com.ratio.deviceService.command.BTLECommandReadDescriptor.java
com.ratio.deviceService.command.BTLECommandSetCharacteristicNotification.java
com.ratio.deviceService.command.BTLECommandWriteCharacteristic.java
com.ratio.deviceService.command.BTLECommandWriteDescriptor.java
com.ratio.deviceService.command.BTLECommand.java
com.ratio.deviceService.receivers.CharacteristicReceiver.java
com.ratio.deviceService.receivers.CharactertisticListReceiver.java
com.ratio.deviceService.receivers.ConnectionStateReceiver.java
com.ratio.deviceService.receivers.DescriptorReceiver.java
com.ratio.deviceService.receivers.DeviceDiscoveredRecevier.java
com.ratio.deviceService.receivers.RSSIReceiver.java
com.ratio.deviceService.receivers.ServicesDiscoveredReceiver.java
com.ratio.exceptions.DeviceManagerException.java
com.ratio.exceptions.DeviceNameNotFoundException.java
com.ratio.util.BitUtils.java
com.ratio.util.StringUtil.java
com.ratio.util.UUIDUtils.java
org.bluetooth.bledemo.BleDefinedUUIDs.java
org.bluetooth.bledemo.BleNamesResolver.java
org.bluetooth.bledemo.BleWrapperUiCallbacks.java
org.bluetooth.bledemo.BleWrapper.java
org.bluetooth.bledemo.CharacteristicDetailsAdapter.java
org.bluetooth.bledemo.CharacteristicsListAdapter.java
org.bluetooth.bledemo.DeviceListAdapter.java
org.bluetooth.bledemo.HRDemoActivity.java
org.bluetooth.bledemo.PeripheralActivity.java
org.bluetooth.bledemo.ScanningActivity.java
org.bluetooth.bledemo.ServicesListAdapter.java