Difference between revisions of "DeviceScanner (Java)"

From Neurotech Software Development Kit
Jump to: navigation, search
(Constructor Summary)
(Constructor Summary)
 
(5 intermediate revisions by the same user not shown)
Line 38: Line 38:
 
{| class="wikitable"
 
{| class="wikitable"
 
|- style="background-color: #dee3e9;"
 
|- style="background-color: #dee3e9;"
|'''Constructors'''
+
|'''Constructor'''
|'''Constructor and Description'''
+
|'''Description'''
 
|- valign="top"
 
|- valign="top"
|[[#DeviceScanner|DeviceScanner]](Context context)
+
|[[#DeviceScanner|DeviceScanner]]([https://developer.android.com/reference/android/content/Context Context] context)
 
|Creates new instance of DeviceScanner class with current application context
 
|Creates new instance of DeviceScanner class with current application context
 
|}
 
|}
Line 59: Line 59:
 
Stops scanning
 
Stops scanning
 
|- valign="top"
 
|- valign="top"
|[[Device (java)|Device]][]
+
|[[Device (Java)|Device]][]
 
|[[#getDeviceByAddress|getDeviceByAddress]](String address)
 
|[[#getDeviceByAddress|getDeviceByAddress]](String address)
 
Looks for device with specified address and returns Device object for it if found, otherwise returns null.
 
Looks for device with specified address and returns Device object for it if found, otherwise returns null.
Line 76: Line 76:
  
 
==Method Detail==
 
==Method Detail==
<h3>connect</h3>
+
<h3>startScan</h3>
<code><span style="color:#0066FF;">'''public void'''</span> connect()</code>
+
<code><span style="color:#0066FF;">'''public void'''</span> startScan(<span style="color:#0066FF;">'''int'''</span> timeout)</code>
  
Tries to establish connection with device. Check DeviceState parameter or subscribe parameterChanged event for operation result.
+
Starts scanning process. For Android versions 6.0 and higher geolocation module should be enabled due to security requirement of Android OS
 
 
<h3>disconnect</h3>
 
<code><span style="color:#0066FF;">'''public void'''</span> disconnect()</code>
 
 
 
Disconnects from device. Check DeviceState parameter or subscribe parameterChanged event for operation result
 
 
 
<h3>channels</h3>
 
<code><span style="color:#0066FF;">'''public'''</span> [[ChannelInfo (Java)|ChannelInfo]][] channels()</code>
 
 
 
Returns information about supported channels.
 
 
 
Check this information before creation of channel. If device does not support channel,
 
channel object won't be initialized with it.
 
<dl>
 
<dt><span>Returns:</span></dt>
 
<dd>Array of channel info objects</dd>
 
</dl>
 
 
 
<h3>commands</h3>
 
<code><span style="color:#0066FF;">'''public'''</span> [[Command (Java)|Command]][] commands()</code>
 
 
 
Returns supported commands of device
 
<dl>
 
<dt><span>Returns:</span></dt>
 
<dd>Array of supported commands</dd>
 
</dl>
 
 
 
<h3>parameters</h3>
 
<code><span style="color:#0066FF;">'''public'''</span> [[Parameter (Java)|Parameter]][] parameters()</code>
 
 
 
Returns all available parameters of device, their types and access rights
 
<dl>
 
<dt><span>Returns:</span></dt>
 
<dd>Array of available parameters</dd>
 
</dl>
 
 
 
<h3>execute</h3>
 
<code><span style="color:#0066FF;">'''public boolean'''</span>&nbsp;execute([[Command (Java)|Command]] cmd)</code>
 
 
 
Tries to execute command and returns value indicating operations success.
 
 
 
Will throw if device does not support specified command. To get supported commands call commands() method.
 
 
<dl>
 
<dl>
<dt><span>Parameters:</span></dt>
 
<dd>cmd - Command to execute</dd>
 
 
<dt><span>Returns:</span></dt>
 
<dt><span>Returns:</span></dt>
<dd>Operation success indicator</dd>
+
<dd>timeout - Duration of scanning process in milliseconds. Zero for infinity</dd>
 
<dt><span>Throws:</span></dt>
 
<dt><span>Throws:</span></dt>
<dd>java.lang.UnsupportedOperationException</dd>
+
<dd>BluetoothAdapterException - if Bluetooth adapter is disabled, this exeption will be thrown</dd>
 +
<dd>BluetoothPermissionsException - is thrown when Bluetooth an Geolocations permissions was not granted</dd>
 
</dl>
 
</dl>
  
<h3>readParam</h3>
+
<h3>stopScan</h3>
<code><span style="color:#0066FF;">'''public'''</span><ParamType> ParamType readParam([[ParameterName (Java)|ParameterName]] param)</code>
+
<code><span style="color:#0066FF;">'''public void'''</span> stopScan()</code>
  
Return value of specified parameter of device.
+
Stops scanning process
  
Will throw if parameter does not present in device. To get supported parameters and type information for parameter call parameters()
+
<h3>getDeviceByAddress</h3>
method. It returns Parameter object which consists of parameter name, type and access mode.
+
<code><span style="color:#0066FF;">'''public'''</span> [[Device (Java)|Device]][] getDeviceByAddress(String address)</code>
  
<dl>
+
Looks for device with specified address and returns Device object for it if found, or null otherwise.
<dt><span>Parameters:</span></dt>
 
<dd>param - ParameterName to read</dd>
 
<dt><span>Returns:</span></dt>
 
<dd>Parameter value</dd>
 
<dt><span>Throws:</span></dt>
 
<dd>java.lang.UnsupportedOperationException</dd>
 
</dl>
 
  
<h3>setParam</h3>
 
<code><span style="color:#0066FF;">'''public boolean'''</span>&nbsp;setParam([[ParameterName (Java)|ParameterName]] param, Object value)</code>
 
 
Sets value for specified parameter and returns value indicating success of operation.
 
 
Will throw if parameter does not present in device or has only Read access mode. To get supported
 
parameters and type information for parameter call parameters() method. It returns Parameter
 
object which consists of parameter name, type and access mode
 
 
<dl>
 
<dl>
<dt><span>Parameters:</span></dt>
 
<dd>param - Name of parameter to set</dd>
 
<dd>value - Parameter value</dd>
 
 
<dt><span>Returns:</span></dt>
 
<dt><span>Returns:</span></dt>
<dd>Operation success indicator</dd>
+
<dd>[[Device (Java)|Device]] object for device with specified address</dd>
<dt><span>Throws:</span></dt>
 
<dd>java.lang.UnsupportedOperationException</dd>
 
 
</dl>
 
</dl>
  
Line 336: Line 273:
 
[[NeuroMD SDK Manual]]
 
[[NeuroMD SDK Manual]]
  
[[Device (C++)]]
+
[[DeviceScanner (C++)]]

Latest revision as of 12:07, 6 June 2018

ru.neuromd.neurosdk

Class DeviceScanner

Related Classes

ru.neuromd.neurosdk.Device

Classes hierarchy

  • java.lang.Object
    • ru.neuromd.neurosdk.DeviceScanner

Class definition

public class DeviceScanner

DeviceScanner class provides simple interface for searching or getting by BLE address of Callibri and Brainbit devices and for acquiring Device abstraction for them.

Fields Summary

Modifier and Type Field and Description
SubscribersNotifier<Device> deviceFound

Subscribe this event to receive notifications about found devices

SubscribersNotifier<Boolean> scanStateChanged

Notifies about changes in device scanning state

Constructor Summary

Constructor Description
DeviceScanner(Context context) Creates new instance of DeviceScanner class with current application context

Methods Summary

Modifier and Type Method and Description
void startScan(int timeout)

Starts scanning for devices

void stopScan()

Stops scanning

Device[] getDeviceByAddress(String address)

Looks for device with specified address and returns Device object for it if found, otherwise returns null.

Field Detail

deviceFound

public final SubscribersNotifier<Device> deviceFound

Subscribe this event to receive notifications about found devices

scanStateChanged

public final SubscribersNotifier<Boolean> scanStateChanged

Notifies about changes in device scanning state. True - scanning is in process, false otherwise.

Method Detail

startScan

public void startScan(int timeout)

Starts scanning process. For Android versions 6.0 and higher geolocation module should be enabled due to security requirement of Android OS

Returns:
timeout - Duration of scanning process in milliseconds. Zero for infinity
Throws:
BluetoothAdapterException - if Bluetooth adapter is disabled, this exeption will be thrown
BluetoothPermissionsException - is thrown when Bluetooth an Geolocations permissions was not granted

stopScan

public void stopScan()

Stops scanning process

getDeviceByAddress

public Device[] getDeviceByAddress(String address)

Looks for device with specified address and returns Device object for it if found, or null otherwise.

Returns:
Device object for device with specified address

Example

This example shows how to find devices and list their features

import com.neuromd.common.INotificationCallback;
import com.neuromd.neurosdk.Device;
import com.neuromd.neurosdk.DeviceScanner;
import com.neuromd.neurosdk.channels.ChannelInfo;
import com.neuromd.neurosdk.parameters.Command;
import com.neuromd.neurosdk.parameters.Parameter;
import com.neuromd.neurosdk.parameters.ParameterName;
import com.neuromd.neurosdk.parameters.types.DeviceState;

public class DeviceInfoActivity extends AppCompatActivity
{
    private DeviceScanner mScanner;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_info);
    
        requestPermissions();
        enableBtAndGeolocation();
        initScanner();
        initButtons();
    }
    
    private void initScanner()
    {
        mScanner = new DeviceScanner(getApplicationContext());
        mScanner.scanStateChanged.subscribe(new INotificationCallback<Boolean>()
        {
            @Override
            public void onNotify(Object o, final Boolean isScanning)
            {
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Button startButton = findViewById(R.id.startScanButton);
                        Button stopButton = findViewById(R.id.stopScanButton);
                        startButton.setEnabled(!isScanning);
                        stopButton.setEnabled(isScanning);
                    }
                });
            }
        });
        mScanner.deviceFound.subscribe(new INotificationCallback<Device>()
        {
            @Override
            public void onNotify(Object o, Device device)
            {
                onDeviceFound(device);
            }
        });
    }
    
    private void requestPermissions(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }
    
    private void enableBtAndGeolocation(){
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
            if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                Intent enableGeoIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(enableGeoIntent, 1);
            }
        }
    }
    
    private void initButtons()
    {
        Button startButton = findViewById(R.id.startScanButton);
        startButton.setEnabled(true);
        startButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mScanner.startScan(0); //zero is for infinity
            }
        });
    
        Button stopButton = findViewById(R.id.stopScanButton);
        stopButton.setEnabled(false);
        stopButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mScanner.stopScan();
            }
        });
    }
    
    private void onDeviceFound(final Device device)
    {
        device.parameterChanged.subscribe(new INotificationCallback<ParameterName>()
        {
            @Override
            public void onNotify(Object o, ParameterName parameterName)
            {
                if (parameterName == ParameterName.State){
                    DeviceState state = device.readParam(ParameterName.State);
                    if (state == DeviceState.Connected){
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                onDeviceConnected(device);
                            }
                        });
                    }
                }
            }
        });
        device.connect();
    }
    
    private void onDeviceConnected(Device device)
    {
        EditText deviceInfoText = findViewById(R.id.infoText);
        
        String deviceName = device.readParam(ParameterName.Name);
        String deviceAddress = device.readParam(ParameterName.Address);
        deviceInfoText.append(String.format("Found device: %s [%s]\n", deviceName, deviceAddress));
    
        deviceInfoText.append("Supported params:\n");
        Parameter[] deviceParams = device.parameters();
        for (Parameter param : deviceParams)
        {
            String paramName = param.getName().toString();
            String accessMode = param.getAccess().toString();
            deviceInfoText.append(String.format("-%s {%s}\n", paramName, accessMode));
        }
    
        deviceInfoText.append("\nSupported commands:\n");
        Command[] deviceCommands = device.commands();
        for (Command cmd : deviceCommands)
        {
            deviceInfoText.append(String.format("-%s \n", cmd.toString()));
        }
    
        deviceInfoText.append("\nSupported channels:\n");
        ChannelInfo[] deviceChannels = device.channels();
        for (ChannelInfo channel : deviceChannels)
        {
            String channelName = channel.getName();
            deviceInfoText.append(String.format("-%s \n", channelName));
        }
    }
}

Possible output:

Android deviceinfo example 1.png

See also

NeuroMD SDK Manual

DeviceScanner (C++)