相信我我花了很多时间寻找解决方案,但它似乎并没有为我工作。我有一个非常简单的代码,我希望我的手机能够发现蓝牙设备(还)。这是代码(处理)。注册接收器处理和android
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;
}
}
以下是错误:
The function "registerReceiver(BroadcastReceiver, IntentFilter)" does not exist
这是因为PApplet没有registerReceiver方法,而是在语境。但我不知道如何获得上下文。我试图puting验证码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
,并获得上下文进行使用此:
MainActivity act = new MainActivity();
con = act.getApplicationContext();
//...
//then this
con.registerReceiver(BCR, new IntentFilter(BluetoothDevice.ACTION_FOUND));
它
,但它会说"Null Context"
。
任何帮助将不胜感激。
你在哪里实例化类中的函数'setup'所在?只需在构造函数中传递上下文派生类即活动 – 0xDEADC0DE
您应该自行实例化“活动” – 0xDEADC0DE
我不知道该怎么做,我需要一个非常详细的指令。 – xchan