首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Metawatch编程的代码示例?

Metawatch编程的代码示例?
EN

Stack Overflow用户
提问于 2014-09-23 19:47:51
回答 1查看 226关注 0票数 0

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

我找到了https://github.com/Pedlar/MetaWatch,但它是一个真正的项目,而不是一个代码样本。我想要简单一点的。

更新:又一个项目https://github.com/cicada-dev/cicada和一个metawatch问题(不幸的是,因子分解值得严厉批评):Sending data via Bluetooth with Android

EN

回答 1

Stack Overflow用户

发布于 2014-09-24 15:27:42

下面是MetaWatch编程的一个示例。在某种程度上,蓝牙和MetaWatch的介绍,可以从这里开始。

它发送一个命令并接收一个应答;可以从onCreate()调用。

代码语言:javascript
复制
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中)类似于:

代码语言:javascript
复制
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行“等待”,读取后有89行,你可以猜测时间。)

如果MetaWatch没有准备好接受连接,它将显示

代码语言:javascript
复制
E/~~~     (26736): ~~~ could not connect:
W/System.err(26736): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25994323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档