Monday, December 7, 2015

App of the rings: Neyya, Android SDK and BLE

My great friend Wim Wepster gave me this interesting Neyya ring. Fortunately it did not come with a proposal. Instead it was a great opportunity to explore Bluetooth and alternative wearables, such as this ring.

Neyya is a ring that can send gestures such as taps and swipes to a mobile or another device that is supporting BLE. You can use it for presentations or games, although you can just use your mouse, watch or phone for that as well. So I wondered how it works and what could be an interesting use case for it.

Neyya comes with an iOS and Android app but also with an Android SDK. I was having some troubles with it as it was not able to detect my Neyya ring at all. For the time being (as there must be a more elegant solution for it) I have fixed this by modifying the NeyyaBaseService class within the NeyyaAndroidSDK project.



I removed the check to determine whether the Bluetooth device is a Neyya ring or not from the onLeScan method.

 private BluetoothAdapter.LeScanCallback mLeScanCallback =
   new BluetoothAdapter.LeScanCallback() {
     @Override
     public void onLeScan(final BluetoothDevice device, int rssi,
       byte[] scanRecord) {
         String deviceAddress = 
           device.getAddress().substring(0, 13);

         // if (neyyaMacSeries.equals(deviceAddress)) {
              NeyyaDevice neyyaDevice = new NeyyaDevice(
               device.getName(), device.getAddress());
              if (!mNeyyaDevices.contains(neyyaDevice)) {
                   logd("Device found - " + device.getAddress()+ 
                    " Name - " + device.getName());
                   mNeyyaDevices.add(neyyaDevice);
                   broadcastDevices();
              }
         //}
     }
  };
I did the same thing for the isNeyyaDevice method, like this.
public boolean isNeyyaDevice(NeyyaDevice device) {
  String deviceAddress = 
    device.getAddress().substring(0, 13);

  /* if (!neyyaMacSeries.equals(deviceAddress)) {
       logd("Not a neyya device");
       broadcastError(ERROR_NOT_NEYYA);
       mCurrentStatus = STATE_DISCONNECTED;
       broadcastState();
       return false;
     }
   */
    return true;
  }

Okay, I have to be more careful which Bluetooth device I pick from the list of available devices but at least I am able to continue my journey. Let's connect to the device.


Great! but what is it that we are trying to solve here?

The supported gestures are taps, double and triple taps, swipe left, right, up and down. Now, what problem could this ring solve other than the things that come with the Neyya app already, such as capturing a picture, turning the volume of a device up and down and moving to the next or previous song?

One of the problems that the Neyya ring could help me with is something that most people will recognize. Your best ideas come up when you are not able to write them down immediately, for example while taking a shower or driving a car. In such cases it would be great to just rub your ring to create an audio note. That is easy to implement.

To create a prototype I have modified the ConnectActivity class a little. First in the onReceive method of the BroadCastReceiver implementation I do call a new method actOnGesture

 ...
 else if (MyService.BROADCAST_GESTURE.equals(action)) {
   int gesture = intent.getIntExtra(MyService.DATA_GESTURE, 0);
   showData(Gesture.parseGesture(gesture));

   actOnGesture(gesture);
 ...

That method goes like this

    private String actOnGesture(int gesture){
        Gesture.parseGesture(gesture);
        switch (gesture) {
            case Gesture.SWIPE_UP:
                stopRecorder();
                return "SWIPE_UP";
            case Gesture.DOUBLE_TAP:
                startRecorder();
                return "DOUBLE_TAP";
        }
        return "";
    }

To start recording we will create a MediaRecorder instance and create an output file for it in the m4a format. in the stopRecorder method we will stop the recording.

   private static String mFileName = null;
   private MediaRecorder mRecorder = null;

   private void startRecorder() {
        if (mRecorder!=null){
            return;
        } 
        String timeStamp = "/" + System.currentTimeMillis() +
          ".m4a";
        mFileName = Environment.getExternalStorageDirectory().
          getAbsolutePath();
        mFileName += timeStamp;
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(
         MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(.
         MediaRecorder.AudioEncoder.AAC);
        mRecorder.setAudioSamplingRate(16000);
        mRecorder.setAudioChannels(1);
        mRecorder.setAudioEncodingBitRate(16);

        try {
          mRecorder.prepare();
        } 
        catch (IOException e) {           
        }
        mRecorder.start();
    }

    private void stopRecorder() {
        if (mRecorder==null){
            return;
        }  
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

Do not forget to add the right permissions to the AndroidManifest.xml file before you test the app.

  <uses-permission 
    android:name="android.permission.BLUETOOTH" />
  <uses-permission 
    android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission 
    android:name="android.permission.RECORD_AUDIO"/>

As soon as the app is running and the ring is connected, double tap on it to start recording and swipe up to stop the recording. You can use an app, such as the Astro app, to locate and play the audio file that you have recorded.


Conclusion

Later I will release the code of this POC on GitHub. I need to work out the concept a little bit more. Anyway, I am not fully convinced yet whether the Neyya ring is a useful wearable device or not, but by examing the code I have learned something about Android and Bluetooth and I do have a memo recorder that I can use just anywhere now.

Do not forget to take your phone with you as well, wherever you go. Wait for my first shower-voice-memos to arrive ;) Of course you can use the ring plus app as a spy tool if you want or find other purposes for it. My precious...

Further reading

4 comments:

  1. I’m not a programmer, so I’m sorry if what I say makes no sense to you. From your article it sounds like you’re are getting a handle on the programing side of the Neyya ring. I think there is a possibility to have the ring preform closer to what the investors had planned then what they finally came up with (a mouse ring). I don’t know if you are familiar with an android app called Tasker, but I believe with it and what you have been working on it could be done. In your example, two task could be created, one to start the recording and one to stop it. One could be triggered by “SINGLE_TAP = 5” and the other by “DOUBLE_TAP = 6”. To use the part that you have been working on, I believe it would need to be turned into an Intent for Tasker to understand it. That is the part I can’t wrap my head around, is how to create the Intents. So what I’m trying to say, is this a project you would be interested in? Thank you for your time.

    Phil

    ReplyDelete
    Replies
    1. Hi Philip, Thank you for your response. Tasker looks interesting but actually I am pretty busy with too many things already ;)

      Delete
  2. I saw the ring on Amazon, but it says it support only iOS and Mac, does it works on Android?

    ReplyDelete
    Replies
    1. It works fine with the Android SDK and the sample Android app.

      It communicates through BLE so I assume there should be no problem with the Android app that you can find in the Play Store.

      Delete