[英]register receiver on processing and android
Trust me I have spent so much time searching for solutions but it doesnt seem to work for me. 相信我,我已经花了很多时间寻找解决方案,但是它似乎对我没有用。 I have a really simple code where i would like my phone to discover bluetooth devices (yet).
我有一个非常简单的代码,我想让我的手机发现蓝牙设备(尚未)。 Here is the code (processing).
这是代码(处理)。
import android.app.Application;
import android.app.Activity;
import android.os.Bundle;
import android.content.IntentFilter;
import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = BT.getBondedDevices();
BroadcastReceiver BCR = new BroadcastReceiver2();
void setup(){
BT.enable();
registerReceiver(BCR, new IntentFilter(BluetoothDevice.ACTION_FOUND));
if(!BT.isDiscovering()) BT.startDiscovery();
}
void draw(){
//
}
public class BroadcastReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
disDevName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
print("Discovered: ");
println(disDevName);
if(disDevName == "XCHAN_PC") BTFound = true;
}
}
Here is the error: 这是错误:
The function "registerReceiver(BroadcastReceiver, IntentFilter)" does not exist
This is because PApplet has no registerReceiver method but instead, in Context. 这是因为PApplet没有Context的registerReceiver方法。 But I do not know how to get the context.
但是我不知道如何获得上下文。 I tried puting this code:
我试着把这段代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
and get the context out using this: 并使用以下内容获取上下文:
MainActivity act = new MainActivity();
con = act.getApplicationContext();
//...
//then this
con.registerReceiver(BCR, new IntentFilter(BluetoothDevice.ACTION_FOUND));
of it but it would say "Null Context"
. 它,但它会说
"Null Context"
。
Any help would be very appreciated. 任何帮助将不胜感激。
try this 尝试这个
In your Activity class: 在您的Activity类中:
public class MyActivity extends Activity {
private MyClass myClass = null; //MyClass is your bluetooth utility class
@Override
public void onResume(Bundle savedInstanceState) {
super.onResume(savedInstanceState);
// ...pass context
myClass = new MyClass(this);
}
@Override
public void onPause() {
if (myClass != null) {
myClass.unregisterReceiver(this);
myClass = null;
}
super.onPause();
}
}
Now in your MyClass
use: 现在在您的
MyClass
使用:
public class MyClass {
private BroadcastReceiver myReceiver = null;
public MyClass(Context context) {
myReceiver = new MyBroadcastReceiver();
context.registerReceiver(myReceiver, new IntentFilter(
BluetoothDevice.ACTION_ACL_CONNECTED));
context.registerReceiver(myReceiver, new IntentFilter(
BluetoothDevice.ACTION_ACL_DISCONNECTED));
context.registerReceiver(myReceiver, new IntentFilter(
BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
}
public void unregisterReceiver(Context context) {
context.unregisterReceiver(this);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// do some stuff
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.