首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

针对 Android Wear 应用程序的 Bluemix IoT 实时仪表板-3

针对 Android Wear 应用程序的 Bluemix IoT 实时仪表板-3

向 IoT Fundation 发布心率在 IoT Fundation 服务中创建一个设备
  • 连接到您的 IBM Bluemix 帐户
  • 在 Catalog 选项卡中,选择“Internet of Things Fundation Starter”
  • 在 Name 字段中输入“HeartRate”并单击“Create”按钮
  • 在 Catalog 选项卡中,选择“Internet of Things”
  • 单击“Create”按钮
  • 启动 Internet of Things Foundation 仪表板
  • 选择“Devices”选项卡,然后选择“Add Device”
  • 在 Device Type 字段中键入“Wear”,并在 Device ID 字段中键入“myWatch”,然后单击“Continue”
  • 保存设备凭证(Organization ID、Device type and ID 和 Authentication Token)
在您的移动模块中导入 mqtt 库
  • 在 HeartRatemobilelibs org.eclipse.paho.android.service.jar 和 org.eclipse.paho.client.mqttv3.jar 文件中填写相应的内容
  • 关闭并重新打开项目
更新移动清单添加 mqtt 服务:
1
<service android:name="org.eclipse.paho.android.service.MqttService" />




添加 WAKE_LOCK 权限:
1
<uses-permission android:name="android.permission.WAKE_LOCK" />




创建 MQTTHandler创建新的 MqttHandler 类,复制以下代码并更改组织和设备令牌:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class MqttHandler implements MqttCallback {
  
private static MqttHandler instance;
private MqttAndroidClient mqttClient;
Context context;
  
private static String ORG = "YOUR_IOT_ORGANISATION";
private static String DEVICE_TYPE = "Wear";
private static String DEVICE_ID = "myWatch";
private static String TOKEN = "YOUR_DEVICE_TOKEN";
private static String TOPIC = "iot-2/evt/hr/fmt/json";
  
private MqttHandler(Context context) {
this.context = context;
}
  
  
public static MqttHandler getInstance(Context context) {
if (instance == null) {
instance = new MqttHandler(context);
}
return instance;
}
  
  
@Override
public void connectionLost(Throwable throwable) {
}
  
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
}
  
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
  
public void connect(IMqttActionListener listener) {
if (!isConnected()) {
String iotPort = "1883";
String iotHost = ORG+".messaging.internetofthings.ibmcloud.com";
String iotClientId = "d:"+ORG+":"+DEVICE_TYPE+":"+DEVICE_ID;
  
String connectionUri = "tcp://" + iotHost + ":" + iotPort;
  
if (mqttClient != null) {
mqttClient.unregisterResources();
mqttClient = null;
}
  
mqttClient = new MqttAndroidClient(context, connectionUri, iotClientId);
mqttClient.setCallback(this);
  
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName("use-token-auth");
options.setPassword(TOKEN.toCharArray());
  
try {
mqttClient.connect(options, context, listener);
} catch (MqttException e) {
  
}
}
}
  
public void disconnect(IMqttActionListener listener) {
if (isConnected()) {
try {
mqttClient.disconnect(context, listener);
mqttClient = null;
} catch (MqttException e) {
e.printStackTrace();
}
}
}
  
public void publish(long timestamp, float heartRateValue) {
if (isConnected()) {
String msg = "{'timestamp':"+timestamp+",'heartRate':"+heartRateValue+"}";
MqttMessage mqttMsg = new MqttMessage(msg.getBytes());
mqttMsg.setRetained(false);
mqttMsg.setQos(0);
try {
mqttClient.publish(TOPIC, mqttMsg);
} catch (Exception e) {
  
}
}
}
  
private boolean isConnected() {
if (mqttClient != null) {
return mqttClient.isConnected();
}
return false;
}
}




连接到 IoT foundation打开 HeartRateMobileActivity,添加此属性:
1
private boolean connected = false;




在 onStart 函数的末尾处添加此代码:
1
2
3
4
5
6
7
8
9
10
MqttHandler.getInstance(this).connect(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
connected = true;
}

@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
}
});




在 onStop 函数的开头处添加此代码:
1
2
3
4
5
6
7
8
9
10
MqttHandler.getInstance(this).disconnect(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken iMqttToken) {
connected = false;
}

@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
}
});




向 IoT Fundation 发送心率使用下列代码更新 onReceive 函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void onReceive(Context arg0, Intent arg1) {
// ahr
Log.v(this.getClass().getName(), "Value Recieved");
if (arg1.getAction().equals("heartRateAction")) {
float hr = arg1.getFloatExtra("HeartRate", 0);
long ts = arg1.getLongExtra("timestamp", 0);
((TextView)HeartRateMobileActivity.this.findViewById(R.id.heartRateTextView)).setText("Heart Rate : " + hr);

if (connected)
MqttHandler.getInstance(HeartRateMobileActivity.this).publish(ts, hr);

}
}

返回列表