Discover SCiO Sensor
July 23, 2015

The SCiO Sensor uses Bluetooth Low Energy (BLE) to communicate with the Android device. Therefore, your users mobile device must be paired to the SCiO Sensor to enable the two devices to communicate with each other. To pair the Android device with the SCiO, your will need to ensure that the Bluetooth settings on their mobile device are turned on and then search for the Bluetooth device and retrieve its address. The BLE API provides standard functions for connecting the two devices together.

The following code examples detail how to activate and connect to Bluetooth devices. We recommend that you familiarize yourself with the Android Bluetooth Low Energy API.

  1. Check that Bluetooth is enabled on the Android device:
    // Checking that Bluetooth is turned on, on the phone. if Bluetooth is off this turns it on.
    
    final static int REQUEST_ENABLE_BT = 1;
    BluetoothAdapter btAdapt;
    
    btAdapt = BLEUtils.getBluetoothAdapter(this);
    if (btAdapt != null && !btAdapt.isEnabled()) {
         Intent btEnableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    
    // startActivityForResult calls onActivityResult. 
    // The onActivityResult function receives the resultCode parameter.
    // resultCode and REQUEST_ENABLE_BT should have the same values. 
    
         startActivityForResult(btEnableIntent, REQUEST_ENABLE_BT);
    }
    
    // You must implement the onActivityResult function
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == RESULT_OK) {
             // do something
             if (resultCode == Activity.RESULT_CANCELED) {
                 finish();
                 return;
             }
         }
         super.onActivityResult(requestCode, resultCode, data);
    }
    
  2. Find the available (Bluetooth and) SCiO Sensors and store their identification data. The address is the Bluetooth device’s unique identifier. The unique identifier is used to communicate with the Bluetooth device:
    // The IntentFilter defines which type of program is capable of opening the message sent. 
    // This value is passed to the BroadcastReceiver function.
    
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    public void findBTDevice() {
    // checks whether Android device has begun searching for a Bluetooth device
        if (btAdapt.isDiscovering()) {
                btAdapt.cancelDiscovery();
           } else {
                boolean btAdapterResult = btAdapt.startDiscovery();
                // Registers the broadcast receiver.
                registerReceiver(btReciever, filter);
           }
        }
    }
    // Locates all Bluetooth devices available to the Android device. 
    // Creates a list with the Bluetooth device data such as device name and address.
    
    private final BroadcastReceiver btReciever = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
           // Connect to the Bluetooth device 
           if (BluetoothDevice.ACTION_FOUND.equals(action)) {
               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
               String deviceName = device.getName() + "";
    
               // You can choose to only register the SCiO sensor by searching for a Bluetooth device starting with SCiO
            if (!deviceName.startsWith("SCiO")) {
               Log.d("MyScioApp","Found a non SCiO Bluetooth device: " + deviceName + ", " + device.getAddress());
            } else {
            // Once you have discovered the SCiO sensor, store the information in a collection 
            btListArrayAdapter.add( device.getAddress()+ " " + device.getName());
    
            // The Bluetooth device is identified with its address. Make sure you store the address for later use.
    
            String btAddress=btAdapt.getAddress();
            btListArrayAdapter.notifyDataSetChanged();
            Log.d("MyScioApp","SCiO sensor found: " + deviceName + ", " + device.getAddress());
           }
        }
      }
    };
    

Once you have the list of all detectable SCiO Sensors your end user can select the SCiO Sensor to connect to.

Leave a Reply