0

Are there some good MetaWatch code samples? Tutorials? Blog articles?

I found https://github.com/Pedlar/MetaWatch, but it is a real project, not a code sample. I would like something less complicated.

UPD: one more project https://github.com/cicada-dev/cicada and one more metawatch question (unfortunately, the factoring deserves severe criticism): Sending data via Bluetooth with Android.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127

1 Answers1

0

Here is an example of MetaWatch programming. Sort of, introduction to Bluetooth and MetaWatch, something one can start with.

It sends one command and receives one reply; may be called from onCreate().

static byte[] getDevTypeMessage = new byte[] {
    0x01, 0x06, 0x01, 0x00, 0x0B, (byte) 0xD9  //The CRC is 0xD90B
};
BluetoothSocket socket = null;
void helloMetaWatch() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Log.e("~~~","no bluetooth");
    } else {
        Log.e("~~~","found bluetooth");
        if (!mBluetoothAdapter.isEnabled()) {
            Log.e("~~~","bluetooth not enabled");
        } else {
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            // If there are paired devices
            if (pairedDevices.size() > 0) {
                Log.d("~~~","paired devices: "+pairedDevices.size());
                // Loop through paired devices
                for (BluetoothDevice device : pairedDevices) {
                    Log.d("~~~","device: ["+device+"] addr="+device.getAddress()+" name=["+device.getName()+"] type:"+getBTType(device)+" class:"+device.getBluetoothClass());
                    if(device.getName().contains("MetaWatch")) {
                        BluetoothSocket temp = null;
                        try
                        {
                            //temp = btDevice.createRfcommSocketToServiceRecord(myUUID);

                            // calling an undocumented method:
                            Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                            temp = (BluetoothSocket) m.invoke(device, 1);

                        } //catch(IOException e) {  }
                         catch (SecurityException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                        socket = temp;
                        Log.d("~~~","socket="+socket);
                        try {
                            socket.connect();
                        } catch (IOException e) {
                            Log.e("~~~","~~~ could not connect:");
                            e.printStackTrace();
                        }
                        InputStream tmpIn = null;
                        OutputStream tmpOut = null;

                        try
                        {
                            tmpIn = socket.getInputStream();
                            tmpOut = socket.getOutputStream();

                            tmpOut.write(getDevTypeMessage);

                            // read response
                            long start = System.currentTimeMillis();
                            while (System.currentTimeMillis() - start <= 1000) {
                                if (0 != tmpIn.available()) {
                                    int readByte = tmpIn.read();
                                    Log.d("~~~","Read: " + readByte);
                                } else {
                                    Log.d("~~~","waiting");
                                    try {
                                        Thread.sleep(10,0);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            System.out.println("Complete in " + (System.currentTimeMillis() - start) + "ms");
                        }
                        catch(IOException e) {
                            e.printStackTrace();
                        }

                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else {
                Log.d("~~~","no paired devices");
            }
        }
    }
}
@SuppressLint("NewApi")
String getBTType(BluetoothDevice device) {
    try {
        return ""+device.getType();
    } catch (Throwable x) {
        return null;
    }
}

The output (in adb logcat) liiks like:

D/~~~     (26862): waiting
D/~~~     (26862): Read: 1
D/~~~     (26862): Read: 7
D/~~~     (26862): Read: 2
D/~~~     (26862): Read: 0
D/~~~     (26862): Read: 2
D/~~~     (26862): Read: 95
D/~~~     (26862): Read: 226
D/~~~     (26862): waiting
D/~~~     (26862): waiting
D/~~~     (26862): waiting

(7 lines of "waiting" before the result is available, and 89 after it is read, you can guess the timing.)

If MetaWatch is not ready to accept the connection, it will show

E/~~~     (26736): ~~~ could not connect:
W/System.err(26736): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127