0
AndroidデバイスとArduinoの間でデータを転送するアプリをまとめようとしています。 10.Android 3、registerReceiverおよびgetApplicationContext関数の処理が存在しません
私はAPIのために開発し、処理3を使用しています15.
これは、コードでAPIのために開発し、
元のコードでは、チュートリアルから取られ、処理2.0Bのために書かれました:
述べ/* DiscoverBluetooth: Written by ScottC on 18 March 2013 using
Processing version 2.0b8
Tested on a Samsung Galaxy SII, with Android version 2.3.4
Android ADK - API 10 SDK platform */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import android.view.Gravity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
boolean foundDevice=false; //When this is true, the screen turns green.
//Get the default Bluetooth adapter
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
/*The startActivityForResult() within setup() launches an
Activity which is used to request the user to turn Bluetooth on.
The following onActivityResult() method is called when this
Activity exits. */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==0){
if(resultCode == -1){
ToastMaster("Bluetooth has been switched ON");
} else {
ToastMaster("You need to turn Bluetooth ON !!!");
}
}
}
/* Create a Broadcast Receiver that will later be used to
receive the names of Bluetooth devices in range. */
BroadcastReceiver myDiscoverer = new myOwnBroadcastReceiver();
void setup(){
orientation(LANDSCAPE);
/*IF Bluetooth is NOT enabled, then ask user permission to enable it */
if (!bluetooth.isEnabled()) {
Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(requestBluetooth, 0);
}
/*If Bluetooth is now enabled, then register a broadcastReceiver to report any
discovered Bluetooth devices, and then start discovering */
if (bluetooth.isEnabled()) {
registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
//Start bluetooth discovery if it is not doing so already
if (!bluetooth.isDiscovering()){
bluetooth.startDiscovery();
}
}
}
void draw(){
//Display a green screen if a device has been found
if(foundDevice){
background(10,255,10);
}
}
/* This BroadcastReceiver will display discovered Bluetooth devices */
public class myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
//Display the name of the discovered device
ToastMaster("Discovered: " + discoveredDeviceName);
//Change foundDevice to true which will make the screen turn green
foundDevice=true;
}
}
/* My ToastMaster function to display a messageBox on the screen */
void ToastMaster(String textToDisplay){
Toast myMessage = Toast.makeText(getApplicationContext(),
textToDisplay,
Toast.LENGTH_LONG);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}
私はライン57上のコンパイルエラーを取得しています
と93:
The function "registerRevceiver(BroadcastReceiver, IntentFilter)" does not exist
The function "getApplicationContext()" does not exist
このコード例をどのように動作させるかについては、誰にも分かりますか?私はそれがAPIレベルといくつかのライブラリの種類の不一致と関係があると思う。
ご協力いただければ幸いです!
代わりgetApplicationContext()
使用ActivityName.this
の城野
ご回答ありがとうございます。私はそれを試してみましたが、別のコンパイラエラーが発生します: "クラス" ActivityName "が存在しません"。私が紛失しているものがありますか?ありがとう – makepeace
私はあなたの活動のためのクラスの名前をそこに置くことを意味しました。 MainActivityまたはそれが呼び出されたもの。 – Jantol
私はそれが何と呼ばれているのか分かりません。あなたはコードを見て、それを見つける場所を教えてください。それは助けになるだろう。また、getApplicationContext()を使用できないのはなぜですか? – makepeace