2016-12-29 54 views
0

相信我我花了很多时间寻找解决方案,但它似乎并没有为我工作。我有一个非常简单的代码,我希望我的手机能够发现蓝牙设备(还)。这是代码(处理)。注册接收器处理和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"

任何帮助将不胜感激。

+0

你在哪里实例化类中的函数'setup'所在?只需在构造函数中传递上下文派生类即活动 – 0xDEADC0DE

+0

您应该自行实例化“活动” – 0xDEADC0DE

+0

我不知道该怎么做,我需要一个非常详细的指令。 – xchan

回答

0

试试这个

在你的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(); 
    } 
} 

现在,在您使用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 
     } 
    } 
} 
+0

这将在Processing IDE上工作吗? – xchan

+0

无论IDE如何,这都可以工作。您的Android设备正在执行代码,而不是IDE – 0xDEADC0DE

+0

处理IDE的含义是什么? – rafsanahmad007