我对Android使用MQTT完全不熟悉,我正在尝试这段代码:
public class MQTTActivity extends AppCompatActivity {
private HistoryAdapter mAdapter;
MqttAndroidClient mqttAndroidClient;
final String serverUri = "tcp://iot.eclipse.org:1883";
String clientId = "ExampleAndroidClient";
final String subscriptionTopic = "exampleAndroidTopic";
final String publishTopic = "exampleAndroidPublishTopic";
final String publishMessage = "Hello World!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
publishMessage();
}
});
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.history_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new HistoryAdapter(new ArrayList<String>());
mRecyclerView.setAdapter(mAdapter);
clientId = clientId + System.currentTimeMillis();
mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId);
mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
if (reconnect) {
addToHistory("Reconnected to : " + serverURI);
// Because Clean Session is true, we need to re-subscribe
subscribeToTopic();
} else {
addToHistory("Connected to: " + serverURI);
}
}
@Override
public void connectionLost(Throwable cause) {
addToHistory("The Connection was lost.");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
addToHistory("Incoming message: " + new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(false);
try {
//addToHistory("Connecting to " + serverUri);
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
addToHistory("Failed to connect to: " + serverUri);
}
});
} catch (MqttException ex){
ex.printStackTrace();
}
}
private void addToHistory(String mainText){
System.out.println("LOG: " + mainText);
mAdapter.add(mainText);
Snackbar.make(findViewById(android.R.id.content), mainText, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public void subscribeToTopic(){
try {
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
addToHistory("Subscribed!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
addToHistory("Failed to subscribe");
}
});
// THIS DOES NOT WORK!
mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// message Arrived!
System.out.println("Message: " + topic + " : " + new String(message.getPayload()));
}
});
} catch (MqttException ex){
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
public void publishMessage(){
try {
MqttMessage message = new MqttMessage();
message.setPayload(publishMessage.getBytes());
mqttAndroidClient.publish(publishTopic, message);
addToHistory("Message Published");
if(!mqttAndroidClient.isConnected()){
addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer.");
}
} catch (MqttException e) {
System.err.println("Error Publishing: " + e.getMessage());
e.printStackTrace();
}
}}
我得到了这个例外:
FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver@41c79f18
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:810)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Neither user 10083 nor current process has android.permission.WAKE_LOCK.
at android.os.Parcel.readException(Parcel.java:1428)
at android.os.Parcel.readException(Parcel.java:1382)
at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:301)
at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:746)
at android.os.PowerManager$WakeLock.acquire(PowerManager.java:715)
at org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver.onReceive(MqttService.java:821)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:800)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
at dalvik.system.NativeStart.main(Native Method)
我已经添加了网络和Internet连接渗透。 我将mqtt服务添加到我的清单中。
我似乎无法找到代码出错的地方?
答案 0 :(得分:2)
问题是来自堆栈跟踪的这一行:
Caused by: java.lang.SecurityException: Neither user 10083 nor current process has android.permission.WAKE_LOCK.
您需要在清单中请求WAKE_LOCK
以及网络访问。
答案 1 :(得分:0)
问题与 MQTT 协议无关,但与应用正在做的其他事情有关......
看起来您正试图通知用户某些事情(可能是新的消息)
为了做到这一点,你需要在android的清单中声明使用permisions,在你的情况下如下:
<uses-permission android:name="android.permission.WAKE_LOCK" />
答案 2 :(得分:0)
使用此 在清单文件中添加权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
并添加内部应用程序标记
<service android:name="org.eclipse.paho.android.service.MqttService" >
</service>
在build.gradle
中repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
exclude module: 'support-v4'
}
并在您的活动中使用代码
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(MainActivity.this, "tcp://broker.hivemq.com:1883",clientId);
try {
token = client.connect(options);
token.setActionCallback(new IMqttActionListener(){
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
Log.d("connect", "onSuccess");
publishmessage();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
Log.d("connect", "onFailure");
}
});
} catch (MqttException e) {
e.printStackTrace();
}