Metawatch编程的代码示例?

时间:2014-09-23 11:47:51

标签: android bluetooth metawatch

是否有一些好的MetaWatch代码示例?教程?博客文章?

我找到了https://github.com/Pedlar/MetaWatch,但这是一个真实的项目,而不是代码示例。我想要一些不太复杂的东西。

UPD:还有一个项目 https://github.com/cicada-dev/cicada 还有一个元话题问题(不幸的是,因子得到了严厉的批评):Sending data via Bluetooth with Android

1 个答案:

答案 0 :(得分:0)

以下是MetaWatch编程的示例。可以从蓝牙和MetaWatch的介绍开始。

它发送一个命令并收到一个回复​​;可以从 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;
    }
}

输出(在adb logcat中)如:

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行&#34;等待&#34;在结果可用之前,89行在读取之后,你可以猜测时间。)

如果MetaWatch尚未准备好接受连接,则会显示

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